From 4edb8c83374f52cd6a8f2c7c875e8ffacccb5fa5 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Tue, 21 Apr 2020 15:55:44 +0200 Subject: Add support for generation of PLT markers in encoder * -PLT switch added to opj_compress * Add a opj_encoder_set_extra_options() function that accepts a PLT=YES option, and could be expanded later for other uses. ------- Testing with a Sentinel2 10m band, T36JTT_20160914T074612_B02.jp2, coming from S2A_MSIL1C_20160914T074612_N0204_R135_T36JTT_20160914T081456.SAFE Decompress it to TIFF: ``` opj_uncompress -i T36JTT_20160914T074612_B02.jp2 -o T36JTT_20160914T074612_B02.tif ``` Recompress it with similar parameters as original: ``` opj_compress -n 5 -c [256,256],[256,256],[256,256],[256,256],[256,256] -t 1024,1024 -PLT -i T36JTT_20160914T074612_B02.tif -o T36JTT_20160914T074612_B02_PLT.jp2 ``` Dump codestream detail with GDAL dump_jp2.py utility (https://github.com/OSGeo/gdal/blob/master/gdal/swig/python/samples/dump_jp2.py) ``` python dump_jp2.py T36JTT_20160914T074612_B02.jp2 > /tmp/dump_sentinel2_ori.txt python dump_jp2.py T36JTT_20160914T074612_B02_PLT.jp2 > /tmp/dump_sentinel2_openjpeg_plt.txt ``` The diff between both show very similar structure, and identical number of packets in PLT markers Now testing with Kakadu (KDU803_Demo_Apps_for_Linux-x86-64_200210) Full file decompression: ``` kdu_expand -i T36JTT_20160914T074612_B02_PLT.jp2 -o tmp.tif Consumed 121 tile-part(s) from a total of 121 tile(s). Consumed 80,318,806 codestream bytes (excluding any file format) = 5.329697 bits/pel. Processed using the multi-threaded environment, with 8 parallel threads of execution ``` Partial decompresson (presumably using PLT markers): ``` kdu_expand -i T36JTT_20160914T074612_B02.jp2 -o tmp.pgm -region "{0.5,0.5},{0.01,0.01}" kdu_expand -i T36JTT_20160914T074612_B02_PLT.jp2 -o tmp2.pgm -region "{0.5,0.5},{0.01,0.01}" diff tmp.pgm tmp2.pgm && echo "same !" ``` ------- Funded by ESA for S2-MPC project --- src/bin/jp2/opj_compress.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'src/bin') diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index cd3953e9..cbc30fba 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -229,6 +229,8 @@ static void encode_help_display(void) fprintf(stdout, " Write SOP marker before each packet.\n"); fprintf(stdout, "-EPH\n"); fprintf(stdout, " Write EPH marker after each header packet.\n"); + fprintf(stdout, "-PLT\n"); + fprintf(stdout, " Write PLT marker in tile-part header.\n"); fprintf(stdout, "-M \n"); fprintf(stdout, " Mode switch.\n"); fprintf(stdout, " [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n"); @@ -576,7 +578,8 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename, size_t indexfilename_size, - int* pOutFramerate) + int* pOutFramerate, + OPJ_BOOL* pOutPLT) { OPJ_UINT32 i, j; int totlen, c; @@ -592,7 +595,8 @@ static int parse_cmdline_encoder(int argc, char **argv, {"ROI", REQ_ARG, NULL, 'R'}, {"jpip", NO_ARG, NULL, 'J'}, {"mct", REQ_ARG, NULL, 'Y'}, - {"IMF", REQ_ARG, NULL, 'Z'} + {"IMF", REQ_ARG, NULL, 'Z'}, + {"PLT", NO_ARG, NULL, 'A'} }; /* parse the command line */ @@ -1670,6 +1674,13 @@ static int parse_cmdline_encoder(int argc, char **argv, break; /* ------------------------------------------------------ */ + case 'A': { /* PLT markers */ + *pOutPLT = OPJ_TRUE; + } + break; + + /* ------------------------------------------------------ */ + default: fprintf(stderr, "[WARNING] An invalid option has been ignored\n"); @@ -1848,6 +1859,8 @@ int main(int argc, char **argv) int framerate = 0; OPJ_FLOAT64 t = opj_clock(); + OPJ_BOOL PLT = OPJ_FALSE; + /* set encoding parameters to default values */ opj_set_default_encoder_parameters(¶meters); @@ -1867,7 +1880,7 @@ int main(int argc, char **argv) parameters.tcp_mct = (char) 255; /* This will be set later according to the input image or the provided option */ if (parse_cmdline_encoder(argc, argv, ¶meters, &img_fol, &raw_cp, - indexfilename, sizeof(indexfilename), &framerate) == 1) { + indexfilename, sizeof(indexfilename), &framerate, &PLT) == 1) { ret = 1; goto fin; } @@ -2117,6 +2130,17 @@ int main(int argc, char **argv) goto fin; } + if (PLT) { + const char* const options[] = { "PLT=YES", NULL }; + if (!opj_encoder_set_extra_options(l_codec, options)) { + fprintf(stderr, "failed to encode image: opj_encoder_set_extra_options\n"); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + ret = 1; + goto fin; + } + } + /* open a byte stream for writing and allocate memory for all tiles */ l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE); if (! l_stream) { -- cgit v1.2.3