2202
Create an index file
2206
@param index Index filename
2207
@return Returns 1 if successful, returns 0 otherwise
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;
2214
image_info->codestream_size = cio_tell(cio) + j2k->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
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
2222
if (j2k->cp->epc_on && !strcmp(index, JPWL_PRIVATEINDEX_NAME))
2224
#endif /* USE_JPWL */
2227
stream = fopen(index, "w");
2229
opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
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);
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 */
2245
fprintf(stream, "\n");
2246
fprintf(stream, "%d\n", image_info->main_head_end);
2247
fprintf(stream, "%d\n", image_info->codestream_size);
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);
2259
for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
2260
int start_pos, end_pos;
2264
if (image_info->prog == LRCP) { /* LRCP */
2266
fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n");
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;
2285
else if (image_info->prog == RLCP) { /* RLCP */
2287
fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto\n");
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;
2306
else if (image_info->prog == RPCL) { /* RPCL */
2308
fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
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;
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;
2352
fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
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;
2386
fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
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;
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;
2426
fprintf(stream, "%8e\n", image_info->D_max); /* SE max */
2427
fprintf(stream, "%.8e\n", total_disto); /* SE totale */
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;
2438
2291
opj_tcd_t *tcd = NULL; /* TCD component */
2445
2298
/* j2k_dump_cp(stdout, image, cp); */
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;
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;
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;
2468
2328
j2k_write_soc(j2k);
2469
2329
j2k_write_siz(j2k);
2470
2330
j2k_write_cod(j2k);
2471
2331
j2k_write_qcd(j2k);
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);
2489
if(image_info && image_info->index_on) {
2490
image_info->main_head_end = cio_tell(cio) - 1;
2494
2349
j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2496
2351
if(cp->cinema){
2497
2352
j2k_write_tlm(j2k);
2498
2353
if (cp->cinema == CINEMA4K_24) {
2499
2354
j2k_write_poc(j2k);
2358
/* uncomment only for testing JPSEC marker writing */
2359
/* j2k_write_sec(j2k); */
2363
cstr_info->main_head_end = cio_tell(cio) - 1;
2502
2366
/**** Main Header ENDS here ***/
2504
2368
/* create the tile encoder */
2505
2369
tcd = tcd_create(j2k->cinfo);
2507
2371
/* encode each tile */
2509
2372
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2511
2374
int tilepartno=0;
2376
int acc_pack_num = 0;
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);
2516
2383
j2k->curtileno = tileno;
2517
2384
j2k->cur_tp_num = 0;
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);
2523
2390
tcd_init_encode(tcd, image, cp, j2k->curtileno);
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;
2395
cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2533
2399
for(pino = 0; pino <= tcp->numpocs; pino++) {
2534
2400
int tot_num_tp;
2535
2401
tcd->cur_pino=pino;
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;
2541
2407
for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2542
2408
j2k->tp_num = tilepartno;
2411
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2412
cio_tell(cio) + j2k->pos_correction;
2543
2414
j2k_write_sot(j2k);
2545
2416
if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2428
cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2429
cio_tell(cio) + j2k->pos_correction + 1;
2555
2432
j2k_write_sod(j2k, tcd);
2561
if(image_info && image_info->index_on) {
2562
image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
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 =
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;
2450
cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2568
2455
if (tile->PPT) { // BAD PPT !!!
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++) {
2575
fread(&elmt, 1, 1, PPT_file);
2576
fwrite(&elmt,1,1,f);
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++) {
2462
fread(&elmt, 1, 1, PPT_file);
2463
fwrite(&elmt,1,1,f);
2585
2472
/* destroy the tile encoder */
2586
2473
tcd_free_encode(tcd);
2587
2474
tcd_destroy(tcd);
2476
opj_free(j2k->cur_totnum_tp);
2589
2478
j2k_write_eoc(j2k);
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);
2481
cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
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;
2600
2490
#ifdef USE_JPWL
2602
preparation of JPWL marker segments: can be finalized only when the whole
2492
preparation of JPWL marker segments
2605
if(image_info && image_info->index_on && cp->epc_on) {
2607
/* let's begin creating a marker list, according to user wishes */
2608
jpwl_prepare_marks(j2k, cio, image);
2610
/* now we dump the JPWL markers on the codestream */
2611
jpwl_dump_marks(j2k, cio, image);
2613
/* do not know exactly what is this for,
2614
but it gets called during index creation */
2615
j2k->pos_correction = 0;
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);
2625
/* now we finalize the marker contents */
2626
/*jpwl_finalize_marks(j2k, cio, image);*/
2496
/* encode according to JPWL */
2497
jpwl_encode(j2k, cio, image);
2629
2500
#endif /* USE_JPWL */