From 815e2f3d033e63616563578cdc3f59a7370e2877 Mon Sep 17 00:00:00 2001 From: Kaori Hagihara Date: Tue, 8 Nov 2011 15:22:02 +0000 Subject: [PATCH] [trunk][JPIP] enabled OpenJPEG V2 API --- applications/jpip/CHANGES | 1 + .../jpip/libopenjpip/dec_clientmsg_handler.c | 3 +- .../jpip/libopenjpip/imgsock_manager.c | 4 +- applications/jpip/libopenjpip/jp2k_decoder.c | 70 +++++++++++++------ applications/jpip/libopenjpip/jp2k_decoder.h | 2 +- .../jpip/libopenjpip/jpipstream_manager.c | 11 ++- applications/jpip/libopenjpip/openjpip.c | 2 +- 7 files changed, 65 insertions(+), 28 deletions(-) diff --git a/applications/jpip/CHANGES b/applications/jpip/CHANGES index 902eff40..14ac96d7 100644 --- a/applications/jpip/CHANGES +++ b/applications/jpip/CHANGES @@ -7,6 +7,7 @@ What's New for OpenJPIP November 8, 2011 ! [kaori] updated main page of doxygen +! [kaori] enabled OpenJPEG V2 API November 3, 2011 * [kaori] solved memory leak of opj_server diff --git a/applications/jpip/libopenjpip/dec_clientmsg_handler.c b/applications/jpip/libopenjpip/dec_clientmsg_handler.c index 294285af2..f001238f 100644 --- a/applications/jpip/libopenjpip/dec_clientmsg_handler.c +++ b/applications/jpip/libopenjpip/dec_clientmsg_handler.c @@ -112,7 +112,8 @@ void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_par send_PNMstream( connected_socket, pnmstream, ihdrbox->width, ihdrbox->height, ihdrbox->nc, ihdrbox->bpc > 8 ? 255 : (1 << ihdrbox->bpc) - 1); - free( pnmstream); + if( pnmstream) + free( pnmstream); } void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist) diff --git a/applications/jpip/libopenjpip/imgsock_manager.c b/applications/jpip/libopenjpip/imgsock_manager.c index 79149f5f..6caf7278 100644 --- a/applications/jpip/libopenjpip/imgsock_manager.c +++ b/applications/jpip/libopenjpip/imgsock_manager.c @@ -85,7 +85,7 @@ SOCKET open_listeningsocket() close_socket(listening_socket); exit(1); } - printf("port %d is listened\n", port); + fprintf( stderr, "port %d is listened\n", port); return listening_socket; } @@ -114,7 +114,7 @@ msgtype_t identify_clientmsg( SOCKET connected_socket) for( i=0; i failed to create the stream from the file\n"); + return NULL; + } + /* decode the code-stream */ /* ---------------------- */ /* JPEG-2000 codestream */ /* get a decoder handle */ - dinfo = opj_create_decompress( CODEC_J2K); - - /* catch events using our callbacks and give a local context */ - opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); + dinfo = opj_create_decompress_v2(CODEC_J2K); /* setup the decoder decoding parameters using user parameters */ - opj_setup_decoder(dinfo, ¶meters); - /* open a byte stream */ - cio = opj_cio_open((opj_common_ptr)dinfo, j2kstream, j2klen); - - fprintf( stderr, "opj_decode dinfo:%p cio:%p\n", dinfo, cio); - /* decode the stream and fill the image structure */ - image = opj_decode(dinfo, cio); - - fprintf( stderr, "done\n"); - if(!image) { - fprintf(stderr, "ERROR -> jp2_to_image: failed to decode image!\n"); - opj_destroy_decompress(dinfo); - opj_cio_close(cio); + if ( !opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr) ){ + fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); return NULL; } + /* Read the main header of the codestream and if necessary the JP2 boxes*/ + if(! opj_read_header( cio, dinfo, &image)){ + fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n"); + opj_stream_destroy(cio); + opj_destroy_codec(dinfo); + opj_image_destroy(image); + return NULL; + } + +#ifdef TODO //decode area could be set from j2k_to_pnm call, modify the protocol between JPIP viewer and opj_dec_server + if (! opj_set_decode_area( dinfo, image, parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){ + fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n"); + opj_stream_destroy(cio); + opj_destroy_codec(dinfo); + opj_image_destroy(image); + return NULL; + } +#endif //TODO + + /* Get the decoded image */ + if ( !( opj_decode_v2(dinfo, cio, image) && opj_end_decompress(dinfo,cio) ) ) { + fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); + opj_stream_destroy(cio); + opj_destroy_codec(dinfo); + opj_image_destroy(image); + return NULL; + } + + fprintf(stderr, "image is decoded!\n"); + /* close the byte stream */ - opj_cio_close(cio); + opj_stream_destroy(cio); /* create output image */ /* ------------------- */ @@ -96,7 +122,7 @@ Byte_t * j2k_to_pnm( Byte_t *j2kstream, Byte8_t j2klen, ihdrbox_param_t **ihdrbo /* free remaining structures */ if(dinfo) { - opj_destroy_decompress(dinfo); + opj_destroy_codec(dinfo); } /* free image data structure */ diff --git a/applications/jpip/libopenjpip/jp2k_decoder.h b/applications/jpip/libopenjpip/jp2k_decoder.h index 59c8dc97..c999cdf1 100644 --- a/applications/jpip/libopenjpip/jp2k_decoder.h +++ b/applications/jpip/libopenjpip/jp2k_decoder.h @@ -34,6 +34,6 @@ #include "byte_manager.h" #include "ihdrbox_manager.h" -Byte_t * j2k_to_pnm( Byte_t *j2kstream, Byte8_t j2klen, ihdrbox_param_t **ihdrbox); +Byte_t * j2k_to_pnm( FILE *fp, ihdrbox_param_t **ihdrbox); #endif /* !JP2K_DECODER_H_ */ diff --git a/applications/jpip/libopenjpip/jpipstream_manager.c b/applications/jpip/libopenjpip/jpipstream_manager.c index 97b61526..983aac48 100644 --- a/applications/jpip/libopenjpip/jpipstream_manager.c +++ b/applications/jpip/libopenjpip/jpipstream_manager.c @@ -73,11 +73,20 @@ Byte_t * jpipstream_to_pnm( Byte_t *jpipstream, msgqueue_param_t *msgqueue, Byte Byte_t *pnmstream; Byte_t *j2kstream; // j2k or jp2 codestream Byte8_t j2klen; + FILE *fp; + char j2kfname[] = "tmp.j2k"; j2kstream = recons_j2k( msgqueue, jpipstream, csn, fw, fh, &j2klen); - pnmstream = j2k_to_pnm( j2kstream, j2klen, ihdrbox); + fp = fopen( j2kfname, "w+b"); + fwrite( j2kstream, j2klen, 1, fp); free( j2kstream); + fseek( fp, 0, SEEK_SET); + + pnmstream = j2k_to_pnm( fp, ihdrbox); + + fclose( fp); + remove( j2kfname); return pnmstream; } diff --git a/applications/jpip/libopenjpip/openjpip.c b/applications/jpip/libopenjpip/openjpip.c index a4f3000a..7bdedea0 100644 --- a/applications/jpip/libopenjpip/openjpip.c +++ b/applications/jpip/libopenjpip/openjpip.c @@ -218,7 +218,7 @@ bool handle_clientreq( client_t client, dec_server_record_t *rec) break; } - printf("\t end of the connection\n\n"); + fprintf( stderr, "\t end of the connection\n\n"); if( close_socket(client) != 0){ perror("close"); return false;