~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Source/LibOpenJPEG/j2k.c

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-07-20 13:42:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100720134215-xt1454zaedv3b604
Tags: 3.13.1-0ubuntu1
* New upstream release. Closes: (LP: #607800)
 - Updated debian/freeimage-get-orig-source script.
 - Removing no longer necessary debian/patches/* and
   the patch system in debian/rules.
 - Updated debian/rules to work with the new Makefiles.
 - Drop from -O3 to -O2 and use lzma compression saves
   ~10 MB of free space. 
* lintian stuff
 - fixed debhelper-but-no-misc-depends
 - fixed ldconfig-symlink-missing-for-shlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
258
258
        return po->str_prog;
259
259
}
260
260
 
261
 
static void j2k_check_poc_val(opj_cparameters_t *parameters, int numcomps, int numlayers){
262
 
        int index, resno, compno, layno, i;
263
 
        char loss = 0;
264
 
        int step_c = 1;
265
 
        int step_r = numcomps * step_c;
266
 
        int step_l = parameters->numresolution * step_r;
267
 
        int array_size = step_l * numlayers * sizeof(int);
268
 
        int *packet_array = (int *) opj_malloc(array_size);
269
 
        
270
 
        for (i = 0; i < parameters->numpocs ; i++) {
271
 
                int layno0 = 0;
272
 
                if(i > 0)
273
 
                        layno0 = (parameters->POC[i].layno1 > parameters->POC[i-1].layno1 )? parameters->POC[i-1].layno1 : 0;
274
 
                for (resno = parameters->POC[i].resno0 ; resno < parameters->POC[i].resno1 ; resno++) {
275
 
                        for (compno = parameters->POC[i].compno0 ; compno < parameters->POC[i].compno1 ; compno++) {
276
 
                                for (layno = layno0; layno < parameters->POC[i].layno1 ; layno++) {
277
 
                                        index = step_r * resno + step_c * compno + step_l * layno;
278
 
                                        packet_array[index]= 1;
279
 
                                }
280
 
                        }
281
 
                }
282
 
        }
283
 
        for (resno = 0; resno < parameters->numresolution; resno++) {
284
 
                for (compno = 0; compno < numcomps; compno++) {
285
 
                        for (layno = 0; layno < numlayers ; layno++) {
286
 
                                index = step_r * resno + step_c * compno + step_l * layno;
287
 
                                if(!(   packet_array[index]== 1)){
288
 
                                        loss = 1;
289
 
                                }
290
 
                        }
291
 
                }
292
 
        }
293
 
        if(loss == 1)
294
 
                fprintf(stdout,"Missing packets possible loss of data\n");
295
 
        opj_free(packet_array);
296
 
}
297
 
 
298
261
void j2k_dump_image(FILE *fd, opj_image_t * img) {
299
262
        int compno;
300
263
        fprintf(fd, "image {\n");
406
369
 
407
370
/**     mem allocation for TLM marker*/
408
371
int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
409
 
        int pino,tileno,maxres=0,totnum_tp=0;
 
372
        int pino,tileno,totnum_tp=0;
410
373
        j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
411
374
        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
412
375
                int cur_totnum_tp = 0;
421
384
                        pi_destroy(pi, cp, tileno);
422
385
                }
423
386
                j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
 
387
                /* INDEX >> */
 
388
                if (j2k->cstr_info) {
 
389
                        j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
 
390
                        j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
 
391
                }
 
392
                /* << INDEX */
424
393
        }
425
394
        return totnum_tp;
426
395
}
428
397
static void j2k_write_soc(opj_j2k_t *j2k) {
429
398
        opj_cio_t *cio = j2k->cio;
430
399
        cio_write(cio, J2K_MS_SOC, 2);
 
400
 
 
401
/* UniPG>> */
 
402
#ifdef USE_JPWL
 
403
 
 
404
        /* update markers struct */
 
405
        j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
 
406
 
 
407
#endif /* USE_JPWL */
 
408
/* <<UniPG */
431
409
}
432
410
 
433
 
static void j2k_read_soc(opj_j2k_t *j2k) {
 
411
static void j2k_read_soc(opj_j2k_t *j2k) {      
434
412
        j2k->state = J2K_STATE_MHSIZ;
 
413
        /* Index */
 
414
        if (j2k->cstr_info) {
 
415
                j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
 
416
                j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
 
417
        }
435
418
}
436
419
 
437
420
static void j2k_write_siz(opj_j2k_t *j2k) {
484
467
        cp->tx0 = cio_read(cio, 4);             /* XT0siz */
485
468
        cp->ty0 = cio_read(cio, 4);             /* YT0siz */
486
469
        
 
470
        if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
 
471
                opj_event_msg(j2k->cinfo, EVT_ERROR,
 
472
                                                                        "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
 
473
                                                                        image->x0,image->x1,image->y0,image->y1);
 
474
                return;
 
475
        }
 
476
        
487
477
        image->numcomps = cio_read(cio, 2);     /* Csiz */
488
478
 
489
479
#ifdef USE_JPWL
525
515
        }
526
516
#endif /* USE_JPWL */
527
517
 
528
 
        image->comps = (opj_image_comp_t *) opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
 
518
        image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
529
519
        for (i = 0; i < image->numcomps; i++) {
530
520
                int tmp, w, h;
531
521
                tmp = cio_read(cio, 1);         /* Ssiz_i */
563
553
                }
564
554
#endif /* USE_JPWL */
565
555
 
566
 
 
567
556
                /* TODO: unused ? */
568
557
                w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
569
558
                h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
570
559
 
571
560
                image->comps[i].resno_decoded = 0;      /* number of resolution decoded */
572
 
                image->comps[i].factor = 0;                     /* reducing factor per component */
 
561
                image->comps[i].factor = cp->reduce; /* reducing factor per component */
573
562
        }
574
563
        
575
564
        cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
615
604
        }
616
605
#endif /* USE_JPWL */
617
606
 
618
 
        cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
619
 
        cp->tileno = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
 
607
        cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
 
608
        cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
620
609
        cp->tileno_size = 0;
621
610
        
622
611
#ifdef USE_JPWL
644
633
        cp->ppm_data_first = NULL;
645
634
        cp->ppm_previous = 0;
646
635
        cp->ppm_store = 0;
647
 
        
648
 
        j2k->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
 
636
 
 
637
        j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
649
638
        for (i = 0; i < cp->tw * cp->th; i++) {
650
 
                cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
651
 
        }
652
 
        j2k->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * sizeof(unsigned char *));
653
 
        j2k->tile_len = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
 
639
                cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
 
640
        }       
 
641
        j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
 
642
        j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
654
643
        j2k->state = J2K_STATE_MH;
 
644
 
 
645
        /* Index */
 
646
        if (j2k->cstr_info) {
 
647
                opj_codestream_info_t *cstr_info = j2k->cstr_info;
 
648
                cstr_info->image_w = image->x1 - image->x0;
 
649
                cstr_info->image_h = image->y1 - image->y0;
 
650
                cstr_info->numcomps = image->numcomps;
 
651
                cstr_info->tw = cp->tw;
 
652
                cstr_info->th = cp->th;
 
653
                cstr_info->tile_x = cp->tdx;    
 
654
                cstr_info->tile_y = cp->tdy;    
 
655
                cstr_info->tile_Ox = cp->tx0;   
 
656
                cstr_info->tile_Oy = cp->ty0;                   
 
657
                cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
 
658
        }
655
659
}
656
660
 
657
661
static void j2k_write_com(opj_j2k_t *j2k) {
665
669
                cio_write(cio, J2K_MS_COM, 2);
666
670
                lenp = cio_tell(cio);
667
671
                cio_skip(cio, 2);
668
 
                cio_write(cio, 0, 2);
 
672
                cio_write(cio, 1, 2);           /* General use (IS 8859-15:1999 (Latin) values) */
669
673
                for (i = 0; i < strlen(comment); i++) {
670
674
                        cio_write(cio, comment[i], 1);
671
675
                }
716
720
 
717
721
        tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
718
722
 
719
 
        /* check the reduce value */
720
 
        cp->reduce = int_min((tccp->numresolutions)-1, cp->reduce);
 
723
        // If user wants to remove more resolutions than the codestream contains, return error
 
724
        if (cp->reduce >= tccp->numresolutions) {
 
725
                opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
 
726
                                        "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
 
727
                j2k->state |= J2K_STATE_ERR;
 
728
        }
 
729
 
721
730
        tccp->cblkw = cio_read(cio, 1) + 2;     /* SPcox (E) */
722
731
        tccp->cblkh = cio_read(cio, 1) + 2;     /* SPcox (F) */
723
732
        tccp->cblksty = cio_read(cio, 1);       /* SPcox (G) */
729
738
                        tccp->prch[i] = tmp >> 4;
730
739
                }
731
740
        }
 
741
 
 
742
        /* INDEX >> */
 
743
        if(j2k->cstr_info && compno == 0) {
 
744
                for (i = 0; i < tccp->numresolutions; i++) {
 
745
                        if (tccp->csty & J2K_CP_CSTY_PRT) {
 
746
                                j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
 
747
                                j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
 
748
                        }
 
749
                        else {
 
750
                                j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
 
751
                                j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
 
752
                        }
 
753
                }
 
754
        }
 
755
        /* << INDEX */
732
756
}
733
757
 
734
758
static void j2k_write_cod(opj_j2k_t *j2k) {
778
802
                cio_seek(cio, pos);
779
803
                j2k_read_cox(j2k, i);
780
804
        }
 
805
 
 
806
        /* Index */
 
807
        if (j2k->cstr_info) {
 
808
                opj_codestream_info_t *cstr_info = j2k->cstr_info;
 
809
                cstr_info->prog = tcp->prg;
 
810
                cstr_info->numlayers = tcp->numlayers;
 
811
                cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
 
812
                for (i = 0; i < image->numcomps; i++) {
 
813
                        cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
 
814
                }
 
815
        }
781
816
}
782
817
 
783
818
static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
1018
1053
        
1019
1054
        opj_cp_t *cp = j2k->cp;
1020
1055
        opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1021
 
        opj_tccp_t *tccp = &tcp->tccps[0];
1022
1056
        opj_cio_t *cio = j2k->cio;
1023
1057
        
1024
1058
        old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1149
1183
                                                "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1150
1184
                                                cio_tell(cio));
1151
1185
                                        if (!JPWL_ASSUME || JPWL_ASSUME) {
1152
 
                                                free(cp->ppm_data);
 
1186
                                                opj_free(cp->ppm_data);
1153
1187
                                                opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1154
1188
                                                return;
1155
1189
                                        }
1229
1263
        cio_seek(cio, lenp);
1230
1264
        cio_write(cio, len, 2);                         /* Lsot */
1231
1265
        cio_seek(cio, lenp + len);
 
1266
 
 
1267
        /* UniPG>> */
 
1268
#ifdef USE_JPWL
 
1269
        /* update markers struct */
 
1270
        j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
 
1271
#endif /* USE_JPWL */
 
1272
        /* <<UniPG */
1232
1273
}
1233
1274
 
1234
1275
static void j2k_read_sot(opj_j2k_t *j2k) {
1238
1279
 
1239
1280
        opj_cp_t *cp = j2k->cp;
1240
1281
        opj_cio_t *cio = j2k->cio;
1241
 
        
 
1282
 
1242
1283
        len = cio_read(cio, 2);
1243
1284
        tileno = cio_read(cio, 2);
1244
1285
 
1267
1308
                backup_tileno++;
1268
1309
        };
1269
1310
#endif /* USE_JPWL */
1270
 
 
1271
1311
        
1272
1312
        if (cp->tileno_size == 0) {
1273
1313
                cp->tileno[cp->tileno_size] = tileno;
1315
1355
        numparts = cio_read(cio, 1);
1316
1356
        
1317
1357
        j2k->curtileno = tileno;
 
1358
        j2k->cur_tp_num = partno;
1318
1359
        j2k->eot = cio_getbp(cio) - 12 + totlen;
1319
1360
        j2k->state = J2K_STATE_TPH;
1320
1361
        tcp = &cp->tcps[j2k->curtileno];
 
1362
 
 
1363
        /* Index */
 
1364
        if (j2k->cstr_info) {
 
1365
                if (tcp->first) {
 
1366
                        if (tileno == 0) 
 
1367
                                j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
 
1368
                        j2k->cstr_info->tile[tileno].tileno = tileno;
 
1369
                        j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
 
1370
                        j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;                             
 
1371
                        j2k->cstr_info->tile[tileno].num_tps = numparts;
 
1372
                        if (numparts)
 
1373
                                j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
 
1374
                        else
 
1375
                                j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
 
1376
                }
 
1377
                else {
 
1378
                        j2k->cstr_info->tile[tileno].end_pos += totlen;
 
1379
                }               
 
1380
                j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
 
1381
                j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
 
1382
                        j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
 
1383
        }
1321
1384
        
1322
 
        if (tcp->first == 1) {
1323
 
                
 
1385
        if (tcp->first == 1) {          
1324
1386
                /* Initialization PPT */
1325
1387
                opj_tccp_t *tmp = tcp->tccps;
1326
1388
                memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1340
1402
        int l, layno;
1341
1403
        int totlen;
1342
1404
        opj_tcp_t *tcp = NULL;
1343
 
        opj_image_info_t *image_info = NULL;
 
1405
        opj_codestream_info_t *cstr_info = NULL;
1344
1406
        
1345
1407
        opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
1346
1408
        opj_cp_t *cp = j2k->cp;
1348
1410
 
1349
1411
        tcd->tp_num = j2k->tp_num ;
1350
1412
        tcd->cur_tp_num = j2k->cur_tp_num;
1351
 
        tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
1352
1413
        
1353
1414
        cio_write(cio, J2K_MS_SOD, 2);
1354
1415
        if (j2k->curtileno == 0) {
1356
1417
        }
1357
1418
 
1358
1419
        /* INDEX >> */
1359
 
        image_info = j2k->image_info;
1360
 
        if (image_info && image_info->index_on) {
1361
 
                if (!j2k->cur_tp_num ){
1362
 
                        image_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1363
 
                }else{
1364
 
                        if(image_info->tile[j2k->curtileno].packet[image_info->num - 1].end_pos < cio_tell(cio))
1365
 
                                image_info->tile[j2k->curtileno].packet[image_info->num].start_pos = cio_tell(cio);
1366
 
                }
 
1420
        cstr_info = j2k->cstr_info;
 
1421
        if (cstr_info) {
 
1422
                if (!j2k->cur_tp_num ) {
 
1423
                        cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
 
1424
                        j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
 
1425
                }
 
1426
                else{
 
1427
                        if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
 
1428
                                cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
 
1429
                }
 
1430
                /* UniPG>> */
 
1431
#ifdef USE_JPWL
 
1432
                /* update markers struct */
 
1433
                j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
 
1434
#endif /* USE_JPWL */
 
1435
                /* <<UniPG */
1367
1436
        }
1368
1437
        /* << INDEX */
1369
1438
        
1370
1439
        tcp = &cp->tcps[j2k->curtileno];
1371
1440
        for (layno = 0; layno < tcp->numlayers; layno++) {
1372
 
                tcp->rates[layno] -= tcp->rates[layno] ? (j2k->sod_start / (cp->th * cp->tw)) : 0;
 
1441
                if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
 
1442
                        tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
 
1443
                } else if (tcp->rates[layno]) {
 
1444
                        tcp->rates[layno]=1;
 
1445
                }
1373
1446
        }
1374
 
        if(image_info && (j2k->cur_tp_num == 0)) {
1375
 
                image_info->num = 0;
 
1447
        if(j2k->cur_tp_num == 0){
 
1448
                tcd->tcd_image->tiles->packno = 0;
 
1449
                if(cstr_info)
 
1450
                        cstr_info->packno = 0;
1376
1451
        }
1377
1452
        
1378
 
        l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, image_info);
 
1453
        l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
1379
1454
        
1380
1455
        /* Writing Psot in SOT marker */
1381
1456
        totlen = cio_tell(cio) + l - j2k->sot_start;
1397
1472
 
1398
1473
        opj_cio_t *cio = j2k->cio;
1399
1474
        int curtileno = j2k->curtileno;
 
1475
 
 
1476
        /* Index */
 
1477
        if (j2k->cstr_info) {
 
1478
                j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
 
1479
                        cio_tell(cio) + j2k->pos_correction - 1;
 
1480
                if (j2k->cur_tp_num == 0)
 
1481
                        j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
 
1482
                j2k->cstr_info->packno = 0;
 
1483
        }
1400
1484
        
1401
1485
        len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1402
 
        
 
1486
 
1403
1487
        if (len == cio_numbytesleft(cio) + 1) {
1404
1488
                truncate = 1;           /* Case of a truncate codestream */
1405
 
        }
1406
 
        
1407
 
        data = (unsigned char *) opj_malloc((j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
 
1489
        }       
1408
1490
 
1409
 
        for (i = 0; i < j2k->tile_len[curtileno]; i++) {
1410
 
                data[i] = j2k->tile_data[curtileno][i];
1411
 
        }
 
1491
        data = j2k->tile_data[curtileno];
 
1492
        data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1412
1493
 
1413
1494
        data_ptr = data + j2k->tile_len[curtileno];
1414
1495
        for (i = 0; i < len; i++) {
1415
1496
                data_ptr[i] = cio_read(cio, 1);
1416
1497
        }
1417
 
        
 
1498
 
1418
1499
        j2k->tile_len[curtileno] += len;
1419
 
        opj_free(j2k->tile_data[curtileno]);
1420
1500
        j2k->tile_data[curtileno] = data;
1421
1501
        
1422
1502
        if (!truncate) {
1424
1504
        } else {
1425
1505
                j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
1426
1506
        }
 
1507
        j2k->cur_tp_num++;
1427
1508
}
1428
1509
 
1429
1510
static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1430
 
        
1431
1511
        opj_cp_t *cp = j2k->cp;
1432
1512
        opj_tcp_t *tcp = &cp->tcps[tileno];
1433
1513
        opj_cio_t *cio = j2k->cio;
1474
1554
        opj_cio_t *cio = j2k->cio;
1475
1555
        /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1476
1556
        cio_write(cio, J2K_MS_EOC, 2);
 
1557
 
 
1558
/* UniPG>> */
 
1559
#ifdef USE_JPWL
 
1560
        /* update markers struct */
 
1561
        j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
 
1562
#endif /* USE_JPWL */
 
1563
/* <<UniPG */
1477
1564
}
1478
1565
 
1479
1566
static void j2k_read_eoc(opj_j2k_t *j2k) {
1480
1567
        int i, tileno;
 
1568
        bool success;
1481
1569
 
1482
1570
        /* if packets should be decoded */
1483
1571
        if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1484
1572
                opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1485
1573
                tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1486
1574
                for (i = 0; i < j2k->cp->tileno_size; i++) {
1487
 
                        tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i);
 
1575
                        tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1488
1576
                        tileno = j2k->cp->tileno[i];
1489
 
                        tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno);
 
1577
                        success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
1490
1578
                        opj_free(j2k->tile_data[tileno]);
1491
1579
                        j2k->tile_data[tileno] = NULL;
1492
1580
                        tcd_free_decode_tile(tcd, i);
 
1581
                        if (success == false) {
 
1582
                                j2k->state |= J2K_STATE_ERR;
 
1583
                                break;
 
1584
                        }
1493
1585
                }
1494
1586
                tcd_free_decode(tcd);
1495
1587
                tcd_destroy(tcd);
1501
1593
                        opj_free(j2k->tile_data[tileno]);
1502
1594
                        j2k->tile_data[tileno] = NULL;
1503
1595
                }
1504
 
        }
1505
 
        
1506
 
        j2k->state = J2K_STATE_MT;
 
1596
        }       
 
1597
        if (j2k->state & J2K_STATE_ERR)
 
1598
                j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
 
1599
        else
 
1600
                j2k->state = J2K_STATE_MT; 
1507
1601
}
1508
1602
 
1509
1603
typedef struct opj_dec_mstabent {
1542
1636
  {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1543
1637
  {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1544
1638
#endif /* USE_JPWL */
 
1639
#ifdef USE_JPSEC
 
1640
  {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
 
1641
  {J2K_MS_INSEC, 0, j2k_read_insec},
 
1642
#endif /* USE_JPSEC */
1545
1643
 
1546
1644
  {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1547
1645
};
1627
1725
/* ----------------------------------------------------------------------- */
1628
1726
 
1629
1727
opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1630
 
        opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1631
 
        if(j2k) {
1632
 
                j2k->cinfo = cinfo;
1633
 
                j2k->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1634
 
                if(!j2k->default_tcp) {
1635
 
                        opj_free(j2k);
1636
 
                        return NULL;
1637
 
                }
 
1728
        opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
 
1729
        if(!j2k)
 
1730
                return NULL;
 
1731
 
 
1732
        j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
 
1733
        if(!j2k->default_tcp) {
 
1734
                opj_free(j2k);
 
1735
                return NULL;
1638
1736
        }
 
1737
 
 
1738
        j2k->cinfo = cinfo;
 
1739
        j2k->tile_data = NULL;
 
1740
 
1639
1741
        return j2k;
1640
1742
}
1641
1743
 
1683
1785
 
1684
1786
                opj_free(cp);
1685
1787
        }
1686
 
 
1687
1788
        opj_free(j2k);
1688
1789
}
1689
1790
 
1690
1791
void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1691
1792
        if(j2k && parameters) {
1692
1793
                /* create and initialize the coding parameters structure */
1693
 
                opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
 
1794
                opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1694
1795
                cp->reduce = parameters->cp_reduce;     
1695
1796
                cp->layer = parameters->cp_layer;
1696
1797
                cp->limit_decoding = parameters->cp_limit_decoding;
1707
1808
        }
1708
1809
}
1709
1810
 
1710
 
opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio) {
 
1811
opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1711
1812
        opj_image_t *image = NULL;
1712
1813
 
1713
 
        opj_common_ptr cinfo = j2k->cinfo;
 
1814
        opj_common_ptr cinfo = j2k->cinfo;      
1714
1815
 
1715
1816
        j2k->cio = cio;
 
1817
        j2k->cstr_info = cstr_info;
 
1818
        if (cstr_info)
 
1819
                memset(cstr_info, 0, sizeof(opj_codestream_info_t));
1716
1820
 
1717
1821
        /* create an empty image */
1718
1822
        image = opj_image_create0();
1724
1828
                opj_dec_mstabent_t *e;
1725
1829
                int id = cio_read(cio, 2);
1726
1830
 
1727
 
 
1728
1831
#ifdef USE_JPWL
1729
1832
                /* we try to honor JPWL correction power */
1730
1833
                if (j2k->cp->correct) {
1784
1887
                if (e->handler) {
1785
1888
                        (*e->handler)(j2k);
1786
1889
                }
 
1890
                if (j2k->state & J2K_STATE_ERR) 
 
1891
                        return NULL;    
 
1892
 
1787
1893
                if (j2k->state == J2K_STATE_MT) {
1788
1894
                        break;
1789
1895
                }
1806
1912
* Read a JPT-stream and decode file
1807
1913
*
1808
1914
*/
1809
 
opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio) {
 
1915
opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
1810
1916
        opj_image_t *image = NULL;
1811
1917
        opj_jpt_msg_header_t header;
1812
1918
        int position;
1817
1923
 
1818
1924
        /* create an empty image */
1819
1925
        image = opj_image_create0();
 
1926
        j2k->image = image;
1820
1927
 
1821
1928
        j2k->state = J2K_STATE_MHSOC;
1822
1929
        
1889
1996
/* ----------------------------------------------------------------------- */
1890
1997
 
1891
1998
opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1892
 
        opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
 
1999
        opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1893
2000
        if(j2k) {
1894
2001
                j2k->cinfo = cinfo;
1895
2002
        }
1900
2007
        int tileno;
1901
2008
 
1902
2009
        if(!j2k) return;
1903
 
 
1904
 
        if(j2k->image_info != NULL) {
1905
 
                opj_image_info_t *image_info = j2k->image_info;
1906
 
                if (image_info->index_on && j2k->cp) {
1907
 
                        opj_cp_t *cp = j2k->cp;
1908
 
                        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1909
 
                                opj_tile_info_t *tile_info = &image_info->tile[tileno];
1910
 
                                opj_free(tile_info->thresh);
1911
 
                                opj_free(tile_info->packet);
1912
 
                        }
1913
 
                        opj_free(image_info->tile);
1914
 
                }
1915
 
                opj_free(image_info);
1916
 
        }
1917
2010
        if(j2k->cp != NULL) {
1918
2011
                opj_cp_t *cp = j2k->cp;
1919
2012
 
1942
2035
        }
1943
2036
 
1944
2037
        /* create and initialize the coding parameters structure */
1945
 
        cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
 
2038
        cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1946
2039
 
1947
2040
        /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1948
2041
        j2k->cp = cp;
1968
2061
                memcpy(cp->matrice, parameters->cp_matrice, array_size);
1969
2062
        }
1970
2063
 
1971
 
        /* creation of an index file ? */
1972
 
        cp->index_on = parameters->index_on;
1973
 
        if(cp->index_on) {
1974
 
                j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t));
1975
 
        }
1976
 
 
1977
2064
        /* tiles */
1978
2065
        cp->tdx = parameters->cp_tdx;
1979
2066
        cp->tdy = parameters->cp_tdy;
2072
2159
 
2073
2160
        /* initialize the mutiple tiles */
2074
2161
        /* ---------------------------- */
2075
 
        cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
 
2162
        cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2076
2163
 
2077
2164
        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2078
2165
                opj_tcp_t *tcp = &cp->tcps[tileno];
2100
2187
                if (parameters->numpocs) {
2101
2188
                        /* initialisation of POC */
2102
2189
                        tcp->POC = 1;
2103
 
                        j2k_check_poc_val(parameters, image->numcomps, tcp->numlayers);
2104
2190
                        for (i = 0; i < parameters->numpocs; i++) {
2105
2191
                                if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2106
2192
                                        opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2119
2205
                        tcp->numpocs = 0;
2120
2206
                }
2121
2207
 
2122
 
                tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2123
 
                
 
2208
                tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
 
2209
 
2124
2210
                for (i = 0; i < image->numcomps; i++) {
2125
2211
                        opj_tccp_t *tccp = &tcp->tccps[i];
2126
2212
                        tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
2198
2284
        }
2199
2285
}
2200
2286
 
2201
 
/**
2202
 
Create an index file
2203
 
@param j2k
2204
 
@param cio
2205
 
@param image_info
2206
 
@param index Index filename
2207
 
@return Returns 1 if successful, returns 0 otherwise
2208
 
*/
2209
 
static int j2k_create_index(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_info_t *image_info, char *index) {
2210
 
        int tileno, compno, layno, resno, precno, pack_nb, x, y;
2211
 
        FILE *stream = NULL;
2212
 
        double total_disto = 0;
2213
 
 
2214
 
        image_info->codestream_size = cio_tell(cio) + j2k->pos_correction;      /* Correction 14/4/03 suite rmq de Patrick */
2215
 
 
2216
 
 
2217
 
#ifdef USE_JPWL
2218
 
        /* if JPWL is enabled and the name coincides with our own set
2219
 
           then discard the creation of the file: this was just done to
2220
 
           enable indexing, we do not want an index file
2221
 
        */
2222
 
        if (j2k->cp->epc_on && !strcmp(index, JPWL_PRIVATEINDEX_NAME))
2223
 
                return 1;
2224
 
#endif /* USE_JPWL */
2225
 
 
2226
 
 
2227
 
        stream = fopen(index, "w");
2228
 
        if (!stream) {
2229
 
                opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
2230
 
                return 0;
2231
 
        }
2232
 
        
2233
 
        fprintf(stream, "%d %d\n", image_info->image_w, image_info->image_h);
2234
 
        fprintf(stream, "%d\n", image_info->prog);
2235
 
        fprintf(stream, "%d %d\n", image_info->tile_x, image_info->tile_y);
2236
 
        fprintf(stream, "%d %d\n", image_info->tw, image_info->th);
2237
 
        fprintf(stream, "%d\n", image_info->comp);
2238
 
        fprintf(stream, "%d\n", image_info->layer);
2239
 
        fprintf(stream, "%d\n", image_info->decomposition);
2240
 
        
2241
 
        for (resno = image_info->decomposition; resno >= 0; resno--) {
2242
 
                fprintf(stream, "[%d,%d] ", 
2243
 
                        (1 << image_info->tile[0].pdx[resno]), (1 << image_info->tile[0].pdx[resno]));  /* based on tile 0 */
2244
 
        }
2245
 
        fprintf(stream, "\n");
2246
 
        fprintf(stream, "%d\n", image_info->main_head_end);
2247
 
        fprintf(stream, "%d\n", image_info->codestream_size);
2248
 
        
2249
 
        for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
2250
 
                fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n",
2251
 
                        image_info->tile[tileno].num_tile,
2252
 
                        image_info->tile[tileno].start_pos,
2253
 
                        image_info->tile[tileno].end_header,
2254
 
                        image_info->tile[tileno].end_pos,
2255
 
                        image_info->tile[tileno].distotile, image_info->tile[tileno].nbpix,
2256
 
                        image_info->tile[tileno].distotile / image_info->tile[tileno].nbpix);
2257
 
        }
2258
 
        
2259
 
        for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
2260
 
                int start_pos, end_pos;
2261
 
                double disto = 0;
2262
 
                pack_nb = 0;
2263
 
                                
2264
 
                if (image_info->prog == LRCP) { /* LRCP */
2265
 
 
2266
 
                        fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos disto\n");
2267
 
 
2268
 
                        for (layno = 0; layno < image_info->layer; layno++) {
2269
 
                                for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2270
 
                                        for (compno = 0; compno < image_info->comp; compno++) {
2271
 
                                                int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2272
 
                                                for (precno = 0; precno < prec_max; precno++) {
2273
 
                                                        start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2274
 
                                                        end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2275
 
                                                        disto = image_info->tile[tileno].packet[pack_nb].disto;
2276
 
                                                        fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
2277
 
                                                                pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2278
 
                                                        total_disto += disto;
2279
 
                                                        pack_nb++;
2280
 
                                                }
2281
 
                                        }
2282
 
                                }
2283
 
                        }
2284
 
                } /* LRCP */
2285
 
                else if (image_info->prog == RLCP) {    /* RLCP */
2286
 
 
2287
 
                        fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos disto\n");
2288
 
 
2289
 
                        for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2290
 
                                for (layno = 0; layno < image_info->layer; layno++) {
2291
 
                                        for (compno = 0; compno < image_info->comp; compno++) {
2292
 
                                                int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2293
 
                                                for (precno = 0; precno < prec_max; precno++) {
2294
 
                                                        start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2295
 
                                                        end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2296
 
                                                        disto = image_info->tile[tileno].packet[pack_nb].disto;
2297
 
                                                        fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2298
 
                                                                pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2299
 
                                                        total_disto += disto;
2300
 
                                                        pack_nb++;
2301
 
                                                }
2302
 
                                        }
2303
 
                                }
2304
 
                        }
2305
 
                } /* RLCP */
2306
 
                else if (image_info->prog == RPCL) {    /* RPCL */
2307
 
 
2308
 
                        fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos disto\n"); 
2309
 
 
2310
 
                        for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2311
 
                                /* I suppose components have same XRsiz, YRsiz */
2312
 
                                int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
2313
 
                                int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
2314
 
                                int x1 = x0 + image_info->tile_x;
2315
 
                                int y1 = y0 + image_info->tile_y;
2316
 
                                for (compno = 0; compno < image_info->comp; compno++) {
2317
 
                                        int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2318
 
                                        for (precno = 0; precno < prec_max; precno++) {
2319
 
                                                int pcnx = image_info->tile[tileno].pw[resno];
2320
 
                                                int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
2321
 
                                                int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
2322
 
                                                int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2323
 
                                                int precno_y = (int) floor( (float)precno/(float)pcnx );
2324
 
                                                for(y = y0; y < y1; y++) {                                                      
2325
 
                                                        if (precno_y*pcy == y ) {
2326
 
                                                                for (x = x0; x < x1; x++) {                                                                     
2327
 
                                                                        if (precno_x*pcx == x ) {
2328
 
                                                                                for (layno = 0; layno < image_info->layer; layno++) {
2329
 
                                                                                        start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2330
 
                                                                                        end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2331
 
                                                                                        disto = image_info->tile[tileno].packet[pack_nb].disto;
2332
 
                                                                                        fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2333
 
                                                                                                pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); 
2334
 
                                                                                        total_disto += disto;
2335
 
                                                                                        pack_nb++; 
2336
 
                                                                                }
2337
 
                                                                        }
2338
 
                                                                }/* x = x0..x1 */
2339
 
                                                        } 
2340
 
                                                }  /* y = y0..y1 */
2341
 
                                        } /* precno */
2342
 
                                } /* compno */
2343
 
                        } /* resno */
2344
 
                } /* RPCL */
2345
 
                else if (image_info->prog == PCRL) {    /* PCRL */
2346
 
                        /* I suppose components have same XRsiz, YRsiz */
2347
 
                        int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
2348
 
                        int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
2349
 
                        int x1 = x0 + image_info->tile_x;
2350
 
                        int y1 = y0 + image_info->tile_y;
2351
 
 
2352
 
                        fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos disto\n"); 
2353
 
 
2354
 
                        for (compno = 0; compno < image_info->comp; compno++) {
2355
 
                                for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2356
 
                                        int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2357
 
                                        for (precno = 0; precno < prec_max; precno++) {
2358
 
                                                int pcnx = image_info->tile[tileno].pw[resno];
2359
 
                                                int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
2360
 
                                                int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
2361
 
                                                int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2362
 
                                                int precno_y = (int) floor( (float)precno/(float)pcnx );
2363
 
                                                for(y = y0; y < y1; y++) {                                                      
2364
 
                                                        if (precno_y*pcy == y ) {
2365
 
                                                                for (x = x0; x < x1; x++) {                                                                     
2366
 
                                                                        if (precno_x*pcx == x ) {
2367
 
                                                                                for (layno = 0; layno < image_info->layer; layno++) {
2368
 
                                                                                        start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2369
 
                                                                                        end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2370
 
                                                                                        disto = image_info->tile[tileno].packet[pack_nb].disto;
2371
 
                                                                                        fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2372
 
                                                                                                pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); 
2373
 
                                                                                        total_disto += disto;
2374
 
                                                                                        pack_nb++; 
2375
 
                                                                                }
2376
 
                                                                        }
2377
 
                                                                }/* x = x0..x1 */
2378
 
                                                        } 
2379
 
                                                }  /* y = y0..y1 */
2380
 
                                        } /* precno */
2381
 
                                } /* resno */
2382
 
                        } /* compno */
2383
 
                } /* PCRL */
2384
 
                else {  /* CPRL */
2385
 
 
2386
 
                        fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos disto\n"); 
2387
 
 
2388
 
                        for (compno = 0; compno < image_info->comp; compno++) {
2389
 
                                /* I suppose components have same XRsiz, YRsiz */
2390
 
                                int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
2391
 
                                int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
2392
 
                                int x1 = x0 + image_info->tile_x;
2393
 
                                int y1 = y0 + image_info->tile_y;
2394
 
                                
2395
 
                                for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2396
 
                                        int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2397
 
                                        for (precno = 0; precno < prec_max; precno++) {
2398
 
                                                int pcnx = image_info->tile[tileno].pw[resno];
2399
 
                                                int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
2400
 
                                                int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
2401
 
                                                int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2402
 
                                                int precno_y = (int) floor( (float)precno/(float)pcnx );
2403
 
                                                for(y = y0; y < y1; y++) {
2404
 
                                                        if (precno_y*pcy == y ) {
2405
 
                                                                for (x = x0; x < x1; x++) {
2406
 
                                                                        if (precno_x*pcx == x ) {
2407
 
                                                                                for (layno = 0; layno < image_info->layer; layno++) {
2408
 
                                                                                        start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2409
 
                                                                                        end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2410
 
                                                                                        disto = image_info->tile[tileno].packet[pack_nb].disto;
2411
 
                                                                                        fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2412
 
                                                                                                pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); 
2413
 
                                                                                        total_disto += disto;
2414
 
                                                                                        pack_nb++; 
2415
 
                                                                                }
2416
 
                                                                        }
2417
 
                                                                }/* x = x0..x1 */
2418
 
                                                        }
2419
 
                                                } /* y = y0..y1 */
2420
 
                                        } /* precno */
2421
 
                                } /* resno */
2422
 
                        } /* compno */
2423
 
                } /* CPRL */   
2424
 
        } /* tileno */
2425
 
        
2426
 
        fprintf(stream, "%8e\n", image_info->D_max); /* SE max */
2427
 
        fprintf(stream, "%.8e\n", total_disto); /* SE totale */
2428
 
        fclose(stream);
2429
 
 
2430
 
        return 1;
2431
 
}
2432
 
 
2433
 
bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) {
 
2287
bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2434
2288
        int tileno, compno;
2435
 
        opj_image_info_t *image_info = NULL;
2436
2289
        opj_cp_t *cp = NULL;
2437
2290
 
2438
2291
        opj_tcd_t *tcd = NULL;  /* TCD component */
2445
2298
        /* j2k_dump_cp(stdout, image, cp); */
2446
2299
 
2447
2300
        /* INDEX >> */
2448
 
        image_info = j2k->image_info;
2449
 
        if (image_info && cp->index_on) {
2450
 
                image_info->index_on = cp->index_on;
2451
 
                image_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2452
 
                image_info->image_w = image->x1 - image->x0;
2453
 
                image_info->image_h = image->y1 - image->y0;
2454
 
                image_info->prog = (&cp->tcps[0])->prg;
2455
 
                image_info->tw = cp->tw;
2456
 
                image_info->th = cp->th;
2457
 
                image_info->tile_x = cp->tdx;   /* new version parser */
2458
 
                image_info->tile_y = cp->tdy;   /* new version parser */
2459
 
                image_info->tile_Ox = cp->tx0;  /* new version parser */
2460
 
                image_info->tile_Oy = cp->ty0;  /* new version parser */
2461
 
                image_info->comp = image->numcomps;
2462
 
                image_info->layer = (&cp->tcps[0])->numlayers;
2463
 
                image_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1;
2464
 
                image_info->D_max = 0;          /* ADD Marcela */
 
2301
        j2k->cstr_info = cstr_info;
 
2302
        if (cstr_info) {
 
2303
                int compno;
 
2304
                cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
 
2305
                cstr_info->image_w = image->x1 - image->x0;
 
2306
                cstr_info->image_h = image->y1 - image->y0;
 
2307
                cstr_info->prog = (&cp->tcps[0])->prg;
 
2308
                cstr_info->tw = cp->tw;
 
2309
                cstr_info->th = cp->th;
 
2310
                cstr_info->tile_x = cp->tdx;    /* new version parser */
 
2311
                cstr_info->tile_y = cp->tdy;    /* new version parser */
 
2312
                cstr_info->tile_Ox = cp->tx0;   /* new version parser */
 
2313
                cstr_info->tile_Oy = cp->ty0;   /* new version parser */
 
2314
                cstr_info->numcomps = image->numcomps;
 
2315
                cstr_info->numlayers = (&cp->tcps[0])->numlayers;
 
2316
                cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
 
2317
                for (compno=0; compno < image->numcomps; compno++) {
 
2318
                        cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
 
2319
                }
 
2320
                cstr_info->D_max = 0.0;         /* ADD Marcela */
 
2321
                cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
 
2322
                cstr_info->maxmarknum = 100;
 
2323
                cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
 
2324
                cstr_info->marknum = 0;
2465
2325
        }
2466
2326
        /* << INDEX */
2467
 
        
 
2327
 
2468
2328
        j2k_write_soc(j2k);
2469
2329
        j2k_write_siz(j2k);
2470
2330
        j2k_write_cod(j2k);
2471
2331
        j2k_write_qcd(j2k);
2472
 
        
 
2332
 
2473
2333
        if(cp->cinema){
2474
2334
                for (compno = 1; compno < image->numcomps; compno++) {
2475
2335
                        j2k_write_coc(j2k, compno);
2485
2345
        if (cp->comment != NULL) {
2486
2346
                j2k_write_com(j2k);
2487
2347
        }
2488
 
        /* INDEX >> */
2489
 
        if(image_info && image_info->index_on) {
2490
 
                image_info->main_head_end = cio_tell(cio) - 1;
2491
 
        }
2492
 
        /* << INDEX */
2493
2348
 
2494
2349
        j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2495
 
        /*       TLM Marker*/
 
2350
        /* TLM Marker*/
2496
2351
        if(cp->cinema){
2497
2352
                j2k_write_tlm(j2k);
2498
2353
                if (cp->cinema == CINEMA4K_24) {
2499
2354
                        j2k_write_poc(j2k);
2500
2355
                }
2501
2356
        }
 
2357
 
 
2358
        /* uncomment only for testing JPSEC marker writing */
 
2359
        /* j2k_write_sec(j2k); */
 
2360
 
 
2361
        /* INDEX >> */
 
2362
        if(cstr_info) {
 
2363
                cstr_info->main_head_end = cio_tell(cio) - 1;
 
2364
        }
 
2365
        /* << INDEX */
2502
2366
        /**** Main Header ENDS here ***/
2503
 
        
 
2367
 
2504
2368
        /* create the tile encoder */
2505
2369
        tcd = tcd_create(j2k->cinfo);
2506
2370
 
2507
2371
        /* encode each tile */
2508
 
 
2509
2372
        for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2510
2373
                int pino;
2511
2374
                int tilepartno=0;
 
2375
                /* UniPG>> */
 
2376
                int acc_pack_num = 0;
 
2377
                /* <<UniPG */
 
2378
 
2512
2379
 
2513
2380
                opj_tcp_t *tcp = &cp->tcps[tileno];
2514
2381
                opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2515
 
                
 
2382
 
2516
2383
                j2k->curtileno = tileno;
2517
2384
                j2k->cur_tp_num = 0;
2518
 
 
 
2385
                tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2519
2386
                /* initialisation before tile encoding  */
2520
2387
                if (tileno == 0) {
2521
2388
                        tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2522
2389
                } else {
2523
2390
                        tcd_init_encode(tcd, image, cp, j2k->curtileno);
2524
2391
                }
2525
 
                
 
2392
 
2526
2393
                /* INDEX >> */
2527
 
                if(image_info && image_info->index_on) {
2528
 
                        image_info->tile[j2k->curtileno].num_tile = j2k->curtileno;
2529
 
                        image_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
 
2394
                if(cstr_info) {
 
2395
                        cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2530
2396
                }
2531
2397
                /* << INDEX */
2532
2398
 
2533
2399
                for(pino = 0; pino <= tcp->numpocs; pino++) {
2534
2400
                        int tot_num_tp;
2535
2401
                        tcd->cur_pino=pino;
2536
 
                        
 
2402
 
2537
2403
                        /*Get number of tile parts*/
2538
2404
                        tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2539
2405
                        tcd->tp_pos = cp->tp_pos;
2540
2406
 
2541
2407
                        for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2542
2408
                                j2k->tp_num = tilepartno;
 
2409
                                /* INDEX >> */
 
2410
                                if(cstr_info)
 
2411
                                        cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
 
2412
                                        cio_tell(cio) + j2k->pos_correction;
 
2413
                                /* << INDEX */
2543
2414
                                j2k_write_sot(j2k);
2544
2415
 
2545
2416
                                if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2552
2423
                                        }
2553
2424
                                }
2554
2425
 
 
2426
                                /* INDEX >> */
 
2427
                                if(cstr_info)
 
2428
                                        cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
 
2429
                                        cio_tell(cio) + j2k->pos_correction + 1;
 
2430
                                /* << INDEX */
 
2431
 
2555
2432
                                j2k_write_sod(j2k, tcd);
2556
 
                                j2k->cur_tp_num ++;
2557
 
                        }
2558
 
                        
2559
 
                }
2560
 
                /* INDEX >> */
2561
 
                if(image_info && image_info->index_on) {
2562
 
                        image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2563
 
                }
2564
 
                /* << INDEX */
2565
 
                
2566
 
                
 
2433
 
 
2434
                                /* INDEX >> */
 
2435
                                if(cstr_info) {
 
2436
                                        cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
 
2437
                                                cio_tell(cio) + j2k->pos_correction - 1;
 
2438
                                        cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
 
2439
                                                acc_pack_num;
 
2440
                                        cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
 
2441
                                                cstr_info->packno - acc_pack_num;
 
2442
                                        acc_pack_num = cstr_info->packno;
 
2443
                                }
 
2444
                                /* << INDEX */
 
2445
 
 
2446
                                j2k->cur_tp_num++;
 
2447
                        }                       
 
2448
                }
 
2449
                if(cstr_info) {
 
2450
                        cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
 
2451
                }
 
2452
 
 
2453
 
2567
2454
                /*
2568
2455
                if (tile->PPT) { // BAD PPT !!! 
2569
 
                        FILE *PPT_file;
2570
 
                        int i;
2571
 
                        PPT_file=fopen("PPT","rb");
2572
 
                        fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2573
 
                        for (i=0;i<tile->len_ppt;i++) {
2574
 
                                unsigned char elmt;
2575
 
                                fread(&elmt, 1, 1, PPT_file);
2576
 
                                fwrite(&elmt,1,1,f);
2577
 
                        }
2578
 
                        fclose(PPT_file);
2579
 
                        unlink("PPT");
 
2456
                FILE *PPT_file;
 
2457
                int i;
 
2458
                PPT_file=fopen("PPT","rb");
 
2459
                fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
 
2460
                for (i=0;i<tile->len_ppt;i++) {
 
2461
                unsigned char elmt;
 
2462
                fread(&elmt, 1, 1, PPT_file);
 
2463
                fwrite(&elmt,1,1,f);
 
2464
                }
 
2465
                fclose(PPT_file);
 
2466
                unlink("PPT");
2580
2467
                }
2581
2468
                */
2582
 
                
 
2469
 
2583
2470
        }
2584
 
        
 
2471
 
2585
2472
        /* destroy the tile encoder */
2586
2473
        tcd_free_encode(tcd);
2587
2474
        tcd_destroy(tcd);
2588
2475
 
 
2476
        opj_free(j2k->cur_totnum_tp);
 
2477
 
2589
2478
        j2k_write_eoc(j2k);
2590
 
        
2591
 
        /* Creation of the index file */
2592
 
        if(image_info && image_info->index_on) {
2593
 
                if(!j2k_create_index(j2k, cio, image_info, index)) {
2594
 
                        opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2595
 
                        return false;
2596
 
                }
 
2479
 
 
2480
        if(cstr_info) {
 
2481
                cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
 
2482
                /* UniPG>> */
 
2483
                /* The following adjustment is done to adjust the codestream size */
 
2484
                /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
 
2485
                /* the first bunch of bytes is not in the codestream              */
 
2486
                cstr_info->codestream_size -= cstr_info->main_head_start;
 
2487
                /* <<UniPG */
2597
2488
        }
2598
2489
 
2599
 
 
2600
2490
#ifdef USE_JPWL
2601
2491
        /*
2602
 
        preparation of JPWL marker segments: can be finalized only when the whole
2603
 
        codestream is known
 
2492
        preparation of JPWL marker segments
2604
2493
        */
2605
 
        if(image_info && image_info->index_on && cp->epc_on) {
2606
 
 
2607
 
                /* let's begin creating a marker list, according to user wishes */
2608
 
                jpwl_prepare_marks(j2k, cio, image);
2609
 
 
2610
 
                /* now we dump the JPWL markers on the codestream */
2611
 
                jpwl_dump_marks(j2k, cio, image);
2612
 
 
2613
 
                /* do not know exactly what is this for,
2614
 
                but it gets called during index creation */
2615
 
                j2k->pos_correction = 0;
2616
 
 
2617
 
                /* Re-creation of the index file, with updated info */
2618
 
                if(image_info && image_info->index_on) {
2619
 
                        if(!j2k_create_index(j2k, cio, image_info, index)) {
2620
 
                                opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to re-create index file %s\n", index);
2621
 
                                return false;
2622
 
                        }
2623
 
                }
2624
 
 
2625
 
                /* now we finalize the marker contents */
2626
 
                /*jpwl_finalize_marks(j2k, cio, image);*/
 
2494
        if(cp->epc_on) {
 
2495
 
 
2496
                /* encode according to JPWL */
 
2497
                jpwl_encode(j2k, cio, image);
2627
2498
 
2628
2499
        }
2629
2500
#endif /* USE_JPWL */
2630
2501
 
2631
 
        
2632
2502
        return true;
2633
2503
}
2634
2504
 
 
2505
 
 
2506
 
 
2507
 
 
2508
 
 
2509