diff --git a/CMakeLists.txt b/CMakeLists.txt index df1230e7..b7a50295 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ INCLUDE_REGULAR_EXPRESSION("^.*$") #----------------------------------------------------------------------------- # OPENJPEG version number, useful for packaging and doxygen doc: SET(OPENJPEG_VERSION_MAJOR 1) -SET(OPENJPEG_VERSION_MINOR 3) +SET(OPENJPEG_VERSION_MINOR 2) SET(OPENJPEG_VERSION_BUILD 0) SET(OPENJPEG_VERSION "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}") @@ -36,10 +36,14 @@ SET(OPENJPEG_LIBRARY_PROPERTIES SOVERSION "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}" ) +#----------------------------------------------------------------------------- +# Test for some required system information. +INCLUDE (${CMAKE_ROOT}/Modules/CMakeBackwardCompatibilityC.cmake) #----------------------------------------------------------------------------- # OpenJPEG build configuration options. OPTION(BUILD_SHARED_LIBS "Build OpenJPEG with shared libraries." OFF) +OPTION(ENABLE_PROFILING "Enable profiling for the library" OFF) #----------------------------------------------------------------------------- SET (EXECUTABLE_OUTPUT_PATH ${OPENJPEG_BINARY_DIR}/bin CACHE PATH "Single output directory for building all executables.") @@ -61,29 +65,38 @@ CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in @ONLY IMMEDIATE) ENDIF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/openjpeg_mangle.h.in) +#----------------------------------------------------------------------------- +# Configure files with settings for use by the build. +CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/opj_configure.h.in + ${CMAKE_CURRENT_BINARY_DIR}/opj_configure.h) + + #----------------------------------------------------------------------------- # Always build the library INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) SUBDIRS( libopenjpeg - mj2 + codec + test_V2_tile_handling + test_Free_image_V2_tile_handling + # mj2 # cmake 2.4.5 has poor java support #j2kviewer/src ) -IF(NOT UNIX) -SUBDIRS( - jpwl - jp3d - indexer_JPIP - ) -ENDIF(NOT UNIX) +#IF(NOT UNIX) +#SUBDIRS( +# jpwl +# jp3d +# indexer_JPIP +# ) +#ENDIF(NOT UNIX) #----------------------------------------------------------------------------- # Build example only if requested -IF(BUILD_EXAMPLES) - SUBDIRS(codec) -ENDIF(BUILD_EXAMPLES) +#IF(BUILD_EXAMPLES) +# SUBDIRS(codec) +#ENDIF(BUILD_EXAMPLES) #----------------------------------------------------------------------------- # For the documentation @@ -94,15 +107,15 @@ ENDIF(BUILD_DOCUMENTATION) #----------------------------------------------------------------------------- # For openjpeg team if they ever want Dart+CMake -IF(OPENJPEG_STANDALONE) - INCLUDE(Dart) - MARK_AS_ADVANCED(BUILD_TESTING DART_ROOT TCL_TCLSH) - IF(BUILD_TESTING) - ENABLE_TESTING() - SET(BUILDNAME "OpenJPEG-${CMAKE_SYSTEM}-${CMAKE_C_COMPILER}" CACHE STRING "Name of build on the dashboard") - MARK_AS_ADVANCED(BUILDNAME) - ENDIF(BUILD_TESTING) -ENDIF(OPENJPEG_STANDALONE) +#IF(OPENJPEG_STANDALONE) +# INCLUDE(Dart) +# MARK_AS_ADVANCED(BUILD_TESTING DART_ROOT TCL_TCLSH) +# IF(BUILD_TESTING) +# ENABLE_TESTING() +# SET(BUILDNAME "OpenJPEG-${CMAKE_SYSTEM}-${CMAKE_C_COMPILER}" CACHE STRING "Name of build on the dashboard") +# MARK_AS_ADVANCED(BUILDNAME) +# ENDIF(BUILD_TESTING) +#ENDIF(OPENJPEG_STANDALONE) # Adding test with dataset from: # http://www.crc.ricoh.com/~gormish/jpeg2000conformance/ diff --git a/ChangeLog b/ChangeLog index 3565e425..773b71de 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,9 @@ What's New for OpenJPEG ! : changed + : added +July 31, 2008 ++ [antonin] created a branch for openjpeg version 2. Modifications that have been applied to the trunk after revision 483 should be re-applied on the branch (version 2 has been developed based on revision 483). + July 9, 2008 + [Parvatha] Added the default lossless parameter to opj_set_default_encoder_parameters in openjpeg.c. @@ -42,7 +45,7 @@ February 11, 2008 February 5, 2008 ! [Parvatha] In convert.c, corrected imagetobmp() conversion for grayscale. In tcd.c, corrected Rate modification in - tcd_init_encode(). Thanks to Jeremy Furtek and Jérôme Fimes. + tcd_init_encode(). Thanks to Jeremy Furtek and Jérôme Fimes. January 31, 2008 ! [GB] In opjviewer, unification of JPEG 2000 family handlers (*.jp2, *.mj2, *.j2k) in a single file @@ -352,7 +355,7 @@ March 30, 2007 * [GB] OPJViewer should now work under Linux, at least with not big j2k files. Tested under Suse 10.1 64 bit. March 29, 2007 -* [Parvatha] Enable accepting file names with `-´ symbol .Modification getopt.c +* [Parvatha] Enable accepting file names with `-´ symbol .Modification getopt.c * [Parvatha] Rsiz profile name generation to be STD_RSIZ for profiles which are not DCI compliant.Modification in image_to_j2k.c ! [Parvatha] renamed convert_progression_order to j2k_convert_progression_order. Modification j2k.c * [Parvatha] Calculation of number of tile part in each tile in j2k_calculate_tp. Modification j2k.c @@ -369,7 +372,7 @@ March 28, 2007 March 27, 2007 + [GB] Improved parsing in OPJViewer, as well some minor aesthetic modifications; support for image rendering with bit depths lower than 8 bits; can display an arbitrary frame of an MJ2 file (only in B/W, though); can reload a file; better resizing capabilities -* [GB] Following to Hervé's suggestions, all the exit() calls, added by JPWL strict checking in t2.c and j2k.c, +* [GB] Following to Hervé's suggestions, all the exit() calls, added by JPWL strict checking in t2.c and j2k.c, have been substituted with (object free'ing + opj_evt_message(EVT_ERROR) + return) + [GB] Added linking to TIFF library in the JPWL VC6 workspaces diff --git a/Free_CMakeImport.cmake b/Free_CMakeImport.cmake new file mode 100644 index 00000000..9b8aaf24 --- /dev/null +++ b/Free_CMakeImport.cmake @@ -0,0 +1,14 @@ +# Import library +#________________ + + # MESSAGE(STATUS "IMPORT : FreeImagelibrary") + + # Include directories + INCLUDE_DIRECTORIES(${OPENJPEG_SOURCE_DIR}/libs/FreeImage) + + # Link libraries + LINK_DIRECTORIES(${OPENJPEG_SOURCE_DIR}/libs/FreeImage) + + LINK_LIBRARIES(freeimage.s) + ADD_DEFINITIONS ( -DFREEIMAGE_LIB ) + diff --git a/README.v2 b/README.v2 new file mode 100644 index 00000000..be57240e --- /dev/null +++ b/README.v2 @@ -0,0 +1,26 @@ +Preliminary notes +----------------- + +The Version 2 of openjpeg has been currently added as a branch on the repository. + +All the trunk has been copied and files that needed to change have been patched. In comparison with the trunk, the 'jp3d' directory has been removed because it's based on a modified version of libopenjpeg version 1 and there is no plan to make it reach the version 2 status. Idem with the 'indexer_JPIP', which is obsolete and should be rewritten from scratch. + +At the time of the branch creation, the following files and directories have been updated (or added) for version 2 : +* libopenjpeg/ +* codec/ +* test_Free_image_V2_tile_handling/ : a test program that uses libopenjpeg v2 and libfreeimage (see libs) to implement a basic codec. +* test_V2_tile_handling/ : a test program that generates a random image and (de)compresses it with libopenjpeg v2. +* CMakeLists.txt +* Free_CMakeImport.cmake +* opj_configure.h.in + +Other files and directories from the trunk (project files, jpwl/, OPJViewer/, ...) have also been copied in the branch and should be modified progressively to comply with v2. Check the Changelog for updates on this topic. + +Enjoy v2 and feel free to contribute ! + + +Instructions to compile v2 +-------------------------- + +to be added. + diff --git a/codec/compat/getopt.c b/codec/compat/getopt.c index f434924b..23270e6a 100644 --- a/codec/compat/getopt.c +++ b/codec/compat/getopt.c @@ -59,11 +59,7 @@ typedef struct option #define BADARG (int)':' #define EMSG "" -/* As this class remembers its values from one Java call to the other, reset the values before each use */ -void reset_options_reading() { - opterr = 1; - optind = 1; -} + /* * getopt -- @@ -136,7 +132,7 @@ struct option *longopts, int totlen) { char param = 1; again: - if (optind >= argc || !argv[optind] || *argv[optind]!='-') + if (optind>argc || !argv[optind] || *argv[optind]!='-') return -1; if (argv[optind][0]=='-' && argv[optind][1]==0) { diff --git a/codec/convert.c b/codec/convert.c index e56d1311..4572d7e5 100644 --- a/codec/convert.c +++ b/codec/convert.c @@ -914,8 +914,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) { if (image->comps[0].prec > 8) { adjustR = image->comps[0].prec - 8; printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); - }else - adjustR = 0; + } for (i = 0; i < 256; i++) { fprintf(fdest, "%c%c%c%c", i, i, i, 0); @@ -1087,7 +1086,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { comp->data[i] = v; } fclose(f); - comp->bpp = int_floorlog2(max) + 1; + //comp->bpp = int_floorlog2(max) + 1; return image; } diff --git a/codec/image_to_j2k.c b/codec/image_to_j2k.c index 7e9436e1..15621489 100644 --- a/codec/image_to_j2k.c +++ b/codec/image_to_j2k.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,10 +31,11 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include +#define __USE_BSD #include #include #include - +#define USE_OPJ_DEPRECATED #include "openjpeg.h" #include "compat/getopt.h" #include "convert.h" @@ -208,9 +210,8 @@ void encode_help_display() { fprintf(stdout,"\n"); fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n"); fprintf(stdout,"\n"); - fprintf(stdout,"-F : characteristics of the raw input image\n"); - fprintf(stdout," -F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); - fprintf(stdout," Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); + fprintf(stdout,"-m : use array-based MCT, values are coma separated, line by line\n"); + fprintf(stdout," no specific separators between lines, no space allowed between values\n"); fprintf(stdout,"\n"); /* UniPG>> */ #ifdef USE_JPWL @@ -522,6 +523,8 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_ } parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution); break; + default : + break; } switch (parameters->cp_cinema){ @@ -565,6 +568,8 @@ void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_ } parameters->max_comp_size = COMP_48_CS; break; + default: + break; } parameters->cp_disto_alloc = 1; } @@ -587,7 +592,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, }; /* parse the command line */ - const char optlist[] = "i:o:hr:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:" + const char optlist[] = "i:o:hr:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:m:" #ifdef USE_JPWL "W:" #endif /* USE_JPWL */ @@ -908,7 +913,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile, &POC[numpocs].resno0, &POC[numpocs].compno0, &POC[numpocs].layno1, &POC[numpocs].resno1, - &POC[numpocs].compno1, &POC[numpocs].progorder) == 7) { + &POC[numpocs].compno1, POC[numpocs].progorder) == 7) { POC[numpocs].prg1 = give_progression(POC[numpocs].progorder); numpocs++; while (*s && *s != '/') { @@ -1044,6 +1049,76 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, fprintf(stdout,"CINEMA 4K compliant codestream\n"); parameters->cp_rsiz = CINEMA4K; } + break; + + case 'm': /* output file */ + { + char *lFilename = optarg; + char * lMatrix; + char *lCurrentPtr ; + int lNbComp = 0; + int lTotalComp; + int lMctComp; + float * lCurrentDoublePtr; + float * lSpace; + int * l_int_ptr; + int i; + int lStrLen; + + FILE * lFile = fopen(lFilename,"r"); + if + (lFile == NULL) + { + return 1; + } + fseek(lFile,0,SEEK_END); + lStrLen = ftell(lFile); + fseek(lFile,0,SEEK_SET); + lMatrix = (char *) malloc(lStrLen + 1); + fread(lMatrix,lStrLen,1,lFile); + fclose(lFile); + lMatrix[lStrLen] = 0; + lCurrentPtr = lMatrix; + + // replace ',' by 0 + while + (*lCurrentPtr != 0 ) + { + if + (*lCurrentPtr == ' ') + { + *lCurrentPtr = 0; + ++lNbComp; + } + ++lCurrentPtr; + } + ++lNbComp; + lCurrentPtr = lMatrix; + + lNbComp = (int) (sqrt(4*lNbComp + 1)/2. - 0.5); + lMctComp = lNbComp * lNbComp; + lTotalComp = lMctComp + lNbComp; + lSpace = (float *) malloc(lTotalComp * sizeof(float)); + lCurrentDoublePtr = lSpace; + for + (i=0;ibuffer, 1, codestream_length, f); - fclose(f); - - fprintf(stderr,"Generated outfile %s\n",parameters.outfile); - /* close and free the byte stream */ - opj_cio_close(cio); - - /* Write the index to disk */ - if (*indexfilename) { - bSuccess = write_index_file(&cstr_info, indexfilename); - if (bSuccess) { - fprintf(stderr, "Failed to output index file into [%s]\n", indexfilename); - } - } - - /* free remaining compression structures */ - opj_destroy_compress(cinfo); - if (*indexfilename) - opj_destroy_cstr_info(&cstr_info); - } else { /* JP2 format output */ - int codestream_length; - opj_cio_t *cio = NULL; - FILE *f = NULL; - - /* get a JP2 compressor handle */ - opj_cinfo_t* cinfo = opj_create_compress(CODEC_JP2); - - /* catch events using our callbacks and give a local context */ - opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr); - - /* setup the encoder parameters using the current image and using user parameters */ - opj_setup_encoder(cinfo, ¶meters, image); - - /* open a byte stream for writing */ - /* allocate memory for all tiles */ - cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); - - /* encode the image */ - if (*indexfilename) // If need to extract codestream information - bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info); - else - bSuccess = opj_encode(cinfo, cio, image, NULL); - if (!bSuccess) { - opj_cio_close(cio); - fprintf(stderr, "failed to encode image\n"); - return 1; - } - codestream_length = cio_tell(cio); - - /* write the buffer to disk */ - f = fopen(parameters.outfile, "wb"); - if (!f) { - fprintf(stderr, "failed to open %s for writing\n", parameters.outfile); - return 1; - } - fwrite(cio->buffer, 1, codestream_length, f); - fclose(f); - fprintf(stderr,"Generated outfile %s\n",parameters.outfile); - /* close and free the byte stream */ - opj_cio_close(cio); - - /* Write the index to disk */ - if (*indexfilename) { - bSuccess = write_index_file(&cstr_info, indexfilename); - if (bSuccess) { - fprintf(stderr, "Failed to output index file\n"); - } - } - - /* free remaining compression structures */ - opj_destroy_compress(cinfo); - if (*indexfilename) - opj_destroy_cstr_info(&cstr_info); + cinfo = parameters.cod_format == J2K_CFMT ? opj_create_compress(CODEC_J2K) : opj_create_compress(CODEC_JP2); + opj_setup_encoder(cinfo, ¶meters, image); + f = fopen(parameters.outfile, "wb"); + if + (! f) + { + fprintf(stderr, "failed to encode image\n"); + return 1; } - + /* open a byte stream for writing */ + /* allocate memory for all tiles */ + cio = opj_stream_create_default_file_stream(f,false); + if + (! cio) + { + return 1; + } + /* encode the image */ + /*if (*indexfilename) // If need to extract codestream information + bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info); + else*/ + bSuccess = opj_start_compress(cinfo,image,cio); + bSuccess = bSuccess && opj_encode(cinfo, cio); + bSuccess = bSuccess && opj_end_compress(cinfo, cio); + + if + (!bSuccess) + { + opj_stream_destroy(cio); + fclose(f); + fprintf(stderr, "failed to encode image\n"); + return 1; + } + + fprintf(stderr,"Generated outfile %s\n",parameters.outfile); + /* close and free the byte stream */ + opj_stream_destroy(cio); + fclose(f); + + /* Write the index to disk */ + if (*indexfilename) { + bSuccess = write_index_file(&cstr_info, indexfilename); + if (bSuccess) { + fprintf(stderr, "Failed to output index file\n"); + } + } + + /* free remaining compression structures */ + opj_destroy_codec(cinfo); + if (*indexfilename) + opj_destroy_cstr_info(&cstr_info); + /* free image data */ opj_image_destroy(image); } diff --git a/codec/j2k_to_image.c b/codec/j2k_to_image.c index feee30a0..523cb5c4 100644 --- a/codec/j2k_to_image.c +++ b/codec/j2k_to_image.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,10 +31,11 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include +#define __USE_BSD #include #include #include - +#define USE_OPJ_DEPRECATED #include "openjpeg.h" #include "compat/getopt.h" #include "convert.h" @@ -504,27 +506,24 @@ void info_callback(const char *msg, void *client_data) { /* -------------------------------------------------------------------------- */ -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ opj_dparameters_t parameters; /* decompression parameters */ img_fol_t img_fol; - opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *image = NULL; FILE *fsrc = NULL; - unsigned char *src = NULL; - int file_length; + bool bResult; int num_images; int i,imageno; dircnt_t *dirptr; - opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */ - opj_cio_t *cio = NULL; + opj_codec_t* dinfo = NULL; /* handle to a decompressor */ + opj_stream_t *cio = NULL; opj_codestream_info_t cstr_info; /* Codestream information structure */ char indexfilename[OPJ_PATH_LEN]; /* index file name */ + OPJ_INT32 l_tile_x0,l_tile_y0; + OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y; /* configure the event callbacks (not required) */ - memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); - event_mgr.error_handler = error_callback; - event_mgr.warning_handler = warning_callback; - event_mgr.info_handler = info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); @@ -566,7 +565,8 @@ int main(int argc, char **argv) { } /*Encoding image one by one*/ - for(imageno = 0; imageno < num_images ; imageno++) { + for(imageno = 0; imageno < num_images ; imageno++) + { image = NULL; fprintf(stderr,"\n"); @@ -580,155 +580,90 @@ int main(int argc, char **argv) { /* read the input file and put it in memory */ /* ---------------------------------------- */ fsrc = fopen(parameters.infile, "rb"); - if (!fsrc) { + if + (!fsrc) + { fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile); return 1; } - fseek(fsrc, 0, SEEK_END); - file_length = ftell(fsrc); - fseek(fsrc, 0, SEEK_SET); - src = (unsigned char *) malloc(file_length); - fread(src, 1, file_length, fsrc); - fclose(fsrc); - + cio = opj_stream_create_default_file_stream(fsrc,true); /* decode the code-stream */ /* ---------------------- */ - switch(parameters.decod_format) { - case J2K_CFMT: + switch + (parameters.decod_format) { - /* JPEG-2000 codestream */ + case J2K_CFMT: + { + /* JPEG-2000 codestream */ - /* get a decoder handle */ - dinfo = opj_create_decompress(CODEC_J2K); + /* get a decoder handle */ + dinfo = opj_create_decompress(CODEC_J2K); + break; + } + case JP2_CFMT: + { + /* JPEG 2000 compressed image data */ + /* get a decoder handle */ + dinfo = opj_create_decompress(CODEC_JP2); + break; + } + case JPT_CFMT: + { + /* JPEG 2000, JPIP */ + /* get a decoder handle */ + dinfo = opj_create_decompress(CODEC_JPT); + break; + } + default: + fprintf(stderr, "skipping file..\n"); + opj_stream_destroy(cio); + continue; + } + /* catch events using our callbacks and give a local context */ + + /* setup the decoder decoding parameters using user parameters */ + opj_setup_decoder(dinfo, ¶meters); - /* catch events using our callbacks and give a local context */ - opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); - - /* 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, src, file_length); - - /* decode the stream and fill the image structure */ - if (*indexfilename) // If need to extract codestream information + /* decode the stream and fill the image structure */ + /* if (*indexfilename) // If need to extract codestream information image = opj_decode_with_info(dinfo, cio, &cstr_info); else - image = opj_decode(dinfo, cio); - if(!image) { - fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); - opj_destroy_decompress(dinfo); - opj_cio_close(cio); - return 1; - } - - /* close the byte stream */ - opj_cio_close(cio); - - /* Write the index to disk */ - if (*indexfilename) { - char bSuccess; - bSuccess = write_index_file(&cstr_info, indexfilename); - if (bSuccess) { - fprintf(stderr, "Failed to output index file\n"); - } - } - } - break; - - case JP2_CFMT: + */ + bResult = opj_read_header( + dinfo, + &image, + &l_tile_x0, + &l_tile_y0, + &l_tile_width, + &l_tile_height, + &l_nb_tiles_x, + &l_nb_tiles_y, + cio); + image = opj_decode(dinfo, cio); + bResult = bResult && (image != 00); + bResult = bResult && opj_end_decompress(dinfo,cio); + if + (!image) { - /* JPEG 2000 compressed image data */ + fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); + opj_destroy_codec(dinfo); + opj_stream_destroy(cio); + fclose(fsrc); + return 1; + } - /* get a decoder handle */ - dinfo = opj_create_decompress(CODEC_JP2); - - /* catch events using our callbacks and give a local context */ - opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); - - /* setup the decoder decoding parameters using the current image and user parameters */ - opj_setup_decoder(dinfo, ¶meters); - - /* open a byte stream */ - cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); - - /* decode the stream and fill the image structure */ - if (*indexfilename) // If need to extract codestream information - image = opj_decode_with_info(dinfo, cio, &cstr_info); - else - image = opj_decode(dinfo, cio); - if(!image) { - fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); - opj_destroy_decompress(dinfo); - opj_cio_close(cio); - return 1; - } - - /* close the byte stream */ - opj_cio_close(cio); - - /* Write the index to disk */ - if (*indexfilename) { - char bSuccess; - bSuccess = write_index_file(&cstr_info, indexfilename); - if (bSuccess) { - fprintf(stderr, "Failed to output index file\n"); - } + /* close the byte stream */ + opj_stream_destroy(cio); + fclose(fsrc); + /* Write the index to disk */ + if (*indexfilename) { + char bSuccess; + bSuccess = write_index_file(&cstr_info, indexfilename); + if (bSuccess) { + fprintf(stderr, "Failed to output index file\n"); } } - break; - - case JPT_CFMT: - { - /* JPEG 2000, JPIP */ - - /* get a decoder handle */ - dinfo = opj_create_decompress(CODEC_JPT); - - /* catch events using our callbacks and give a local context */ - opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); - - /* 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, src, file_length); - - /* decode the stream and fill the image structure */ - if (*indexfilename) // If need to extract codestream information - image = opj_decode_with_info(dinfo, cio, &cstr_info); - else - image = opj_decode(dinfo, cio); - if(!image) { - fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); - opj_destroy_decompress(dinfo); - opj_cio_close(cio); - return 1; - } - - /* close the byte stream */ - opj_cio_close(cio); - - /* Write the index to disk */ - if (*indexfilename) { - char bSuccess; - bSuccess = write_index_file(&cstr_info, indexfilename); - if (bSuccess) { - fprintf(stderr, "Failed to output index file\n"); - } - } - } - break; - - default: - fprintf(stderr, "skipping file..\n"); - continue; - } - - /* free the memory containing the code-stream */ - free(src); - src = NULL; /* create output image */ /* ------------------- */ @@ -789,8 +724,10 @@ int main(int argc, char **argv) { } /* free remaining structures */ - if(dinfo) { - opj_destroy_decompress(dinfo); + if + (dinfo) + { + opj_destroy_codec(dinfo); } /* free codestream information structure */ if (*indexfilename) diff --git a/indexer_JPIP/CMakeLists.txt b/indexer_JPIP/CMakeLists.txt deleted file mode 100644 index 4dbb95b0..00000000 --- a/indexer_JPIP/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -# index_create - -ADD_EXECUTABLE(index_create -bio.c cio.c int.c pi.c t2.c tgt.c tcd.c index_create.c jpip.c jp2.c -) diff --git a/indexer_JPIP/Makefile b/indexer_JPIP/Makefile deleted file mode 100644 index 40c049ea..00000000 --- a/indexer_JPIP/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -CC = gcc - -LDFLAGS = -lm -CFLAGS = -Wall - -all: index_create - - -bio.o : bio.c bio.h -cio.o : cio.c cio.h -int.o : int.c -pi.o : pi.c pi.h int.h -index_create.o : index_create.c j2k.h cio.h tcd.h int.h -t2.o : t2.c t2.h tcd.h bio.h j2k.h pi.h tgt.h int.h cio.h -tgt.o : tgt.c bio.h tgt.h -tcd.o : tcd.c tcd.h t2.h int.h -jpip.o : jpip.c j2k.h cio.h tcd.h int.h -jp2.o : jp2.c j2k.h cio.h tcd.h int.h - -index_create : bio.o cio.o int.o pi.o t2.o tgt.o tcd.o index_create.o jpip.o jp2.o - -clean: - rm -rf *.o *.*~ *~ core.* diff --git a/indexer_JPIP/bio.c b/indexer_JPIP/bio.c deleted file mode 100644 index 2c989e56..00000000 --- a/indexer_JPIP/bio.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "bio.h" -#include - -static unsigned char *bio_start, *bio_end, *bio_bp; -static unsigned int bio_buf; -static int bio_ct; - -extern jmp_buf j2k_error; - -/// -/// Number of bytes written. -/// -int bio_numbytes() { - return bio_bp-bio_start; -} - -/// -/// Init decoder. -/// -/// Input buffer -/// Input buffer length -void bio_init_dec(unsigned char *bp, int len) { - bio_start=bp; - bio_end=bp+len; - bio_bp=bp; - bio_buf=0; - bio_ct=0; -} - -int bio_byteout() -{ - bio_buf = (bio_buf << 8) & 0xffff; - bio_ct = bio_buf == 0xff00 ? 7 : 8; - if (bio_bp >= bio_end) - return 1; - *bio_bp++ = bio_buf >> 8; - return 0; -} - -/// -/// Read byte. -/// -int bio_bytein() { - bio_buf=(bio_buf<<8)&0xffff; - bio_ct=bio_buf==0xff00?7:8; - if (bio_bp>=bio_end) return 1; //longjmp(j2k_error, 1); - bio_buf|=*bio_bp++; - return 0; -} - -/// -/// Read bit. -/// -int bio_getbit() { - if (bio_ct==0) { - bio_bytein(); - } - bio_ct--; - return (bio_buf>>bio_ct)&1; -} - -/// -/// Read bits. -/// -/// Number of bits to read -int bio_read(int n) { - int i, v; - v=0; - for (i=n-1; i>=0; i--) { - v+=bio_getbit()< -/// Flush bits. -/// -int bio_flush() { - bio_ct=0; - bio_byteout(); - if (bio_ct==7) { - bio_ct=0; - if ( bio_byteout()) return 1;; - } - return 0; -} - -/// -/// -int bio_inalign() { - bio_ct=0; - if ((bio_buf&0xff)==0xff) { - if( bio_bytein()) return 1; - bio_ct=0; - } - return 0; -} diff --git a/indexer_JPIP/bio.h b/indexer_JPIP/bio.h deleted file mode 100644 index eea6cff0..00000000 --- a/indexer_JPIP/bio.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003-2004, Yannick Verschueren - * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __BIO_H -#define __BIO_H - -int bio_numbytes(); -void bio_init_dec(unsigned char *bp, int len); -int bio_read(int n); -int bio_flush(); -int bio_inalign(); - -#endif diff --git a/indexer_JPIP/cio.c b/indexer_JPIP/cio.c deleted file mode 100644 index 29f160ee..00000000 --- a/indexer_JPIP/cio.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "cio.h" -#include - -static unsigned char *cio_start, *cio_end, *cio_bp; - -extern jmp_buf j2k_error; - -/// -/// Number of bytes written. -/// -int cio_numbytes() { - return cio_bp-cio_start; -} - -/// -/// Get position in byte stream. -/// -int cio_tell() { - return cio_bp-cio_start; -} - -/// -/// Set position in byte stream. -/// -void cio_seek(int pos) { - cio_bp=cio_start+pos; -} - -/// -/// Number of bytes left before the end of the stream. -/// -int cio_numbytesleft() { - return cio_end-cio_bp; -} - -/// -/// Get pointer to the current position in the stream. -/// -unsigned char *cio_getbp() { - return cio_bp; -} - -/// -/// Initialize byte IO. -/// -void cio_init(unsigned char *bp, int len) { - cio_start=bp; - cio_end=bp+len; - cio_bp=bp; -} - -/// -/// Write a byte. -/// -void cio_byteout(unsigned char v) { - if (cio_bp>=cio_end) longjmp(j2k_error, 1); - *cio_bp++=v; - -} - -/// -/// Read a byte. -/// -unsigned char cio_bytein() { - if (cio_bp>=cio_end) longjmp(j2k_error, 1); - return *cio_bp++; -} - -/// -/// Write a byte. -/// -//void cio_write(unsigned int v, int n) { -void cio_write(long long v, int n) { - int i; - for (i=n-1; i>=0; i--) - { - cio_byteout((unsigned char)((v>>(i<<3))&0xff)); - } -} - -/// -/// Read some bytes. -/// -/* unsigned int cio_read(int n) { */ -long long cio_read(int n) { - int i; - /*unsigned int v;*/ - long long v; - v=0; - for (i=n-1; i>=0; i--) { - v+=cio_bytein()<<(i<<3); - } - return v; -} - -/// -/// Write some bytes. -/// -void cio_skip(int n) { - cio_bp+=n; -} diff --git a/indexer_JPIP/cio.h b/indexer_JPIP/cio.h deleted file mode 100644 index 3e297897..00000000 --- a/indexer_JPIP/cio.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003-2004, Yannick Verschueren - * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __CIO_H -#define __CIO_H - -int cio_tell(); -void cio_seek(int pos); -int cio_numbytes(); -int cio_numbytesleft(); -unsigned char *cio_getbp(); -void cio_init(unsigned char *bp, int len); -/* void cio_write(unsigned int v, int n); */ -void cio_write(long long v, int n); -/* unsigned int cio_read(int n); */ -long long cio_read(int n); -void cio_skip(int n); - -#endif diff --git a/indexer_JPIP/fix.h b/indexer_JPIP/fix.h deleted file mode 100644 index 4b6e1b54..00000000 --- a/indexer_JPIP/fix.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __FIX_H -#define __FIX_H - -int fix_mul(int a, int b); - -#endif diff --git a/indexer_JPIP/index_create.c b/indexer_JPIP/index_create.c deleted file mode 100644 index 759f86bb..00000000 --- a/indexer_JPIP/index_create.c +++ /dev/null @@ -1,1218 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003-2004, Yannick Verschueren - * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - - -#include -#include -#include -#include -#include - -#include "j2k.h" -#include "cio.h" -#include "tcd.h" -#include "int.h" -#include "jpip.h" -#include "jp2.h" - -#define J2K_MS_SOC 0xff4f -#define J2K_MS_SOT 0xff90 -#define J2K_MS_SOD 0xff93 -#define J2K_MS_EOC 0xffd9 -#define J2K_MS_SIZ 0xff51 -#define J2K_MS_COD 0xff52 -#define J2K_MS_COC 0xff53 -#define J2K_MS_RGN 0xff5e -#define J2K_MS_QCD 0xff5c -#define J2K_MS_QCC 0xff5d -#define J2K_MS_POC 0xff5f -#define J2K_MS_TLM 0xff55 -#define J2K_MS_PLM 0xff57 -#define J2K_MS_PLT 0xff58 -#define J2K_MS_PPM 0xff60 -#define J2K_MS_PPT 0xff61 -#define J2K_MS_SOP 0xff91 -#define J2K_MS_EPH 0xff92 -#define J2K_MS_CRG 0xff63 -#define J2K_MS_COM 0xff64 - -#define J2K_STATE_MHSOC 0x0001 -#define J2K_STATE_MHSIZ 0x0002 -#define J2K_STATE_MH 0x0004 -#define J2K_STATE_TPHSOT 0x0008 -#define J2K_STATE_TPH 0x0010 -#define J2K_STATE_MT 0x0020 - -#define START_NB 5 -#define INCREMENT 5 - -jmp_buf j2k_error; - -static int j2k_state; -static int j2k_curtileno; -static j2k_tcp_t j2k_default_tcp; -static unsigned char *j2k_eot; - -static j2k_image_t *j2k_img; -static j2k_cp_t *j2k_cp; - -static unsigned char **j2k_tile_data; -static int *j2k_tile_len; - -static info_image_t img; - - -void j2k_clean() { - int tileno = 0; - int compno=0, resno=0, precno=0; - - tcd_free(j2k_img, j2k_cp); - for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) { - info_tile_t *tile_Idx = &img.tile[tileno]; - - for (compno = 0; compno < img.Comp; compno++) - { - info_compo_t *compo_Idx = &tile_Idx->compo[compno]; - for(resno = 0; resno < img.Decomposition + 1; resno++) - { - info_reso_t *reso_Idx = &compo_Idx->reso[resno]; - for (precno = 0; precno < img.tile[tileno].pw * img.tile[tileno].ph; precno++) - { - info_prec_t *prec_Idx = &reso_Idx->prec[precno]; - free(prec_Idx->layer); - } - free(reso_Idx->prec); - } - free(compo_Idx->reso); - } - free(tile_Idx->compo); - free(tile_Idx->marker); - free(tile_Idx->tile_parts); - free(tile_Idx->marker_mul.COC); - free(tile_Idx->marker_mul.RGN); - free(tile_Idx->marker_mul.QCC); - free(tile_Idx->marker_mul.PLT); - free(tile_Idx->marker_mul.PPT); - free(tile_Idx->marker_mul.COM); -} - free(img.tile); - free(img.marker); - free(img.marker_mul.COC); - free(img.marker_mul.RGN); - free(img.marker_mul.QCC); - free(img.marker_mul.PLM); - free(img.marker_mul.PPM); - free(img.marker_mul.COM); -} - - - -void j2k_read_soc() { - j2k_state=J2K_STATE_MHSIZ; -} - - - -void j2k_read_siz() { - int len, i; - info_tile_t *tile; - - len = cio_read(2); - - /* [MHIX BOX] */ - img.marker[img.num_marker].type = J2K_MS_SIZ; - img.marker[img.num_marker].start_pos = cio_tell()-2; - img.marker[img.num_marker].len = len; - img.num_marker++; - /* [MHIX BOX] */ - - cio_read(2); /* Rsiz (capabilities) */ - j2k_img->x1 = cio_read(4); /* Xsiz */ - j2k_img->y1 = cio_read(4); /* Ysiz */ - j2k_img->x0 = cio_read(4); /* X0siz */ - j2k_img->y0 = cio_read(4); /* Y0siz */ - j2k_cp->tdx = cio_read(4); /* XTsiz */ - j2k_cp->tdy = cio_read(4); /* YTsiz */ - j2k_cp->tx0 = cio_read(4); /* XT0siz */ - j2k_cp->ty0 = cio_read(4); /* YTOsiz */ - - j2k_img->numcomps = cio_read(2); /* Csiz */ - j2k_img->comps = (j2k_comp_t*)malloc(j2k_img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < j2k_img->numcomps; i++) { - int tmp, w, h; - tmp = cio_read(1); - j2k_img->comps[i].prec = (tmp & 0x7f) + 1; - j2k_img->comps[i].sgnd = tmp >> 7; - j2k_img->comps[i].dx = cio_read(1); - j2k_img->comps[i].dy = cio_read(1); - w = int_ceildiv(j2k_img->x1-j2k_img->x0, j2k_img->comps[i].dx); - h = int_ceildiv(j2k_img->y1-j2k_img->y0, j2k_img->comps[i].dy); - j2k_img->comps[i].data = (int*)malloc(sizeof(int) * w * h); - } - j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx); - j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy); - - j2k_cp->tcps = (j2k_tcp_t*)calloc((j2k_cp->tw * j2k_cp->th), sizeof(j2k_tcp_t)); - - for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) - { - j2k_cp->tcps[i].POC = 0; - j2k_cp->tcps[i].numpocs = 0; - // j2k_cp->tcps[i].first=1; - } - - /* Initialization for PPM marker */ - j2k_cp->ppm = 0; - j2k_cp->ppm_data = NULL; - j2k_cp->ppm_previous = 0; - j2k_cp->ppm_store = 0; - - j2k_default_tcp.tccps = (j2k_tccp_t*)malloc(j2k_img->numcomps * sizeof(j2k_tccp_t)); - for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) { - j2k_cp->tcps[i].tccps = (j2k_tccp_t*)malloc(j2k_img->numcomps * sizeof(j2k_tccp_t)); - } - j2k_tile_data = (unsigned char**)calloc(j2k_cp->tw * j2k_cp->th, sizeof(char*)); - j2k_tile_len = (int*)calloc(j2k_cp->tw * j2k_cp->th, sizeof(int)); - j2k_state = J2K_STATE_MH; - - /* */ - img.Im_w = j2k_img->x1 - j2k_img->x0; - img.Im_h = j2k_img->y1 - j2k_img->y0; - img.Tile_x = j2k_cp->tdx; - img.Tile_y = j2k_cp->tdy; - img.Comp = j2k_img->numcomps; - img.tw = j2k_cp->tw; - img.th = j2k_cp->th; - img.tile = (info_tile_t*)malloc(img.tw * img.th * sizeof(info_tile_t)); - - for (i = 0; i < img.tw * img.th; i++) - { - tile = &img.tile[i]; - tile->marker = (info_marker_t*)malloc(32 * sizeof(info_marker_t)); - tile->num_marker = 0; - tile->marker_mul.num_COC = 0; - tile->marker_mul.CzCOC = START_NB; - tile->marker_mul.num_RGN = 0; - tile->marker_mul.CzRGN = START_NB; - tile->marker_mul.num_QCC = 0; - tile->marker_mul.CzQCC = START_NB; - tile->marker_mul.num_PLT = 0; - tile->marker_mul.CzPLT = START_NB; - tile->marker_mul.num_PPT = 0; - tile->marker_mul.CzPPT = START_NB; - tile->marker_mul.num_COM = 0; - tile->marker_mul.CzCOM = START_NB; - } - /* */ - - - } - -void j2k_read_com() { - int len; - info_tile_t *tile; - info_marker_t *tmp; - - len = cio_read(2); - - /* [MHIX BOX] */ - if (j2k_state == J2K_STATE_MH) - { - if (!img.marker_mul.num_COM) - img.marker_mul.COM = (info_marker_t*)malloc(img.marker_mul.CzCOM * sizeof(info_marker_t)); - if (img.marker_mul.num_COM >= img.marker_mul.CzCOM) - { - tmp = (info_marker_t*)malloc(2 * img.marker_mul.CzCOM * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.COM,img.marker_mul.CzCOM); - img.marker_mul.CzCOM *= 2; - free(img.marker_mul.COM); - img.marker_mul.COM = tmp; - } - - img.marker_mul.COM[img.marker_mul.num_COM].type = J2K_MS_COM; - img.marker_mul.COM[img.marker_mul.num_COM].start_pos = cio_tell()-2; - img.marker_mul.COM[img.marker_mul.num_COM].len = len; - img.marker_mul.num_COM++; - } else - { - tile = &img.tile[j2k_curtileno]; - if (!tile->marker_mul.num_COM) - tile->marker_mul.COM = (info_marker_t*)calloc(START_NB, sizeof(info_marker_t)); - if (tile->marker_mul.num_COM >= tile->marker_mul.CzCOM) - { - tmp = (info_marker_t*)malloc(2 * tile->marker_mul.CzCOM * sizeof(info_marker_t)); - memcpy(tmp,tile->marker_mul.COM,tile->marker_mul.CzCOM); - tile->marker_mul.CzCOM *= 2; - free(tile->marker_mul.COM); - tile->marker_mul.COM = tmp; - } - tile->marker_mul.COM[tile->marker_mul.num_COM].type = J2K_MS_COM; - tile->marker_mul.COM[tile->marker_mul.num_COM].start_pos = cio_tell()-2; - tile->marker_mul.COM[tile->marker_mul.num_COM].len = len; - tile->marker_mul.num_COM++; - } - /* [MHIX BOX] */ - - cio_skip(len - 2); -} - - - - -void j2k_read_cox(int compno) { - int i; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - tccp = &tcp->tccps[compno]; - tccp->numresolutions = cio_read(1) + 1; - - img.Decomposition = tccp->numresolutions - 1; /* */ - - tccp->cblkw = cio_read(1) + 2; - tccp->cblkh = cio_read(1) + 2; - tccp->cblksty = cio_read(1); - tccp->qmfbid = cio_read(1); - if (tccp->csty&J2K_CP_CSTY_PRT) { - for (i = 0; i < tccp->numresolutions; i++) { - int tmp = cio_read(1); - tccp->prcw[i] = tmp&0xf; - tccp->prch[i] = tmp>>4; - } - } -} - - - - -void j2k_read_cod() { - int len, i, pos; - j2k_tcp_t *tcp; - info_tile_t *tile; - - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - len = cio_read(2); - - /* [MHIX BOX] */ - if (j2k_state == J2K_STATE_MH) - { - img.marker[img.num_marker].type = J2K_MS_SIZ; - img.marker[img.num_marker].start_pos = cio_tell()-2; - img.marker[img.num_marker].len = len; - img.num_marker++; - } - else - { - tile = &img.tile[j2k_curtileno]; - tile->marker[tile->num_marker].type = J2K_MS_SIZ; - tile->marker[tile->num_marker].start_pos = cio_tell()-2; - tile->marker[tile->num_marker].len = len; - tile->num_marker++; - } - /* [MHIX BOX] */ - - tcp->csty = cio_read(1); - tcp->prg = cio_read(1); - tcp->numlayers = cio_read(2); - tcp->mct = cio_read(1); - - pos = cio_tell(); - for (i = 0; i < j2k_img->numcomps; i++) { - tcp->tccps[i].csty = tcp->csty&J2K_CP_CSTY_PRT; - cio_seek(pos); - j2k_read_cox(i); - } - - /* */ - img.Prog = tcp->prg; - img.Layer = tcp->numlayers; - /* */ -} - - - - -void j2k_read_coc() { - int len, compno; - j2k_tcp_t *tcp; - info_tile_t *tile; - info_marker_t *tmp; - - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - len = cio_read(2); - - /* [MHIX BOX] */ - if (j2k_state == J2K_STATE_MH) - { - if (!img.marker_mul.num_COC) - img.marker_mul.COC = (info_marker_t*)malloc(img.marker_mul.CzCOC * sizeof(info_marker_t)); - if (img.marker_mul.num_COC >= img.marker_mul.CzCOC) - { - tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzCOC) * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.COC,img.marker_mul.CzCOC); - img.marker_mul.CzCOC += INCREMENT; - free(img.marker_mul.COC); - img.marker_mul.COC = tmp; - } - img.marker_mul.COC[img.marker_mul.num_COC].type = J2K_MS_COC; - img.marker_mul.COC[img.marker_mul.num_COC].start_pos = cio_tell()-2; - img.marker_mul.COC[img.marker_mul.num_COC].len = len; - img.marker_mul.num_COC++; - } else - { - tile = &img.tile[j2k_curtileno]; - if (!tile->marker_mul.num_COC) - tile->marker_mul.COC = (info_marker_t*)malloc(tile->marker_mul.CzCOC * sizeof(info_marker_t)); - if (tile->marker_mul.num_COC >= tile->marker_mul.CzCOC) - { - tmp = (info_marker_t*)malloc((INCREMENT + tile->marker_mul.CzCOC) * sizeof(info_marker_t)); - memcpy(tmp,tile->marker_mul.COC,tile->marker_mul.CzCOC); - tile->marker_mul.CzCOC += INCREMENT; - free(tile->marker_mul.COC); - tile->marker_mul.COC = tmp; - } - tile->marker_mul.COC[tile->marker_mul.num_COC].type = J2K_MS_COC; - tile->marker_mul.COC[tile->marker_mul.num_COC].start_pos = cio_tell() - 2; - tile->marker_mul.COC[tile->marker_mul.num_COC].len = len; - tile->marker_mul.num_COC++; - } - /* [MHIX BOX] */ - - compno =cio_read(j2k_img->numcomps <= 256 ? 1 : 2); - - tcp->tccps[compno].csty = cio_read(1); - j2k_read_cox(compno); -} - - - - -void j2k_read_qcx(int compno, int len) { - int tmp; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - int bandno, numbands; - - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - tccp = &tcp->tccps[compno]; - tmp = cio_read(1); - tccp->qntsty = tmp & 0x1f; - tccp->numgbits = tmp >> 5; - numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT ? len - 1 : (len - 1) / 2); - for (bandno = 0; bandno < numbands; bandno++) { - int expn, mant; - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { /* WHY STEPSIZES WHEN NOQNT ? */ - expn = cio_read(1) >> 3; - mant = 0; - } else { - tmp = cio_read(2); - expn = tmp >> 11; - mant = tmp & 0x7ff; - } - tccp->stepsizes[bandno].expn = expn; - tccp->stepsizes[bandno].mant = mant; - } -} - - - - -void j2k_read_qcd() { - int len, i, pos; - info_tile_t *tile; - - len = cio_read(2); - - /* [MHIX BOX] */ - if (j2k_state == J2K_STATE_MH) - { - img.marker[img.num_marker].type = J2K_MS_QCD; - img.marker[img.num_marker].start_pos = cio_tell()-2; - img.marker[img.num_marker].len = len; - img.num_marker++; - } else - { - tile = &img.tile[j2k_curtileno]; - tile->marker[tile->num_marker].type = J2K_MS_QCD; - tile->marker[tile->num_marker].start_pos = cio_tell()-2; - tile->marker[tile->num_marker].len = len; - tile->num_marker++; - } - /* [MHIX BOX] */ - - - pos=cio_tell(); - for (i = 0; i < j2k_img->numcomps; i++) { - cio_seek(pos); - j2k_read_qcx(i, len - 2); - } -} - - - - -void j2k_read_qcc() { - int len, compno; - info_tile_t *tile; - info_marker_t *tmp; - - len = cio_read(2); - /* [MHIX BOX] */ - if (j2k_state == J2K_STATE_MH) - { - if (!img.marker_mul.num_QCC) - img.marker_mul.QCC = (info_marker_t*)malloc(img.marker_mul.CzQCC * sizeof(info_marker_t)); - if (img.marker_mul.num_QCC >= img.marker_mul.CzQCC) - { - tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzQCC) * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.QCC,img.marker_mul.CzQCC); - img.marker_mul.CzQCC += INCREMENT; - free(img.marker_mul.QCC); - img.marker_mul.QCC = tmp; - } - img.marker_mul.QCC[img.marker_mul.num_QCC].type = J2K_MS_QCC; - img.marker_mul.QCC[img.marker_mul.num_QCC].start_pos = cio_tell() - 2; - img.marker_mul.QCC[img.marker_mul.num_QCC].len = len; - img.marker_mul.num_QCC++; - } else - { - tile = &img.tile[j2k_curtileno]; - if (!tile->marker_mul.num_QCC) - tile->marker_mul.QCC = (info_marker_t*)malloc(tile->marker_mul.CzQCC * sizeof(info_marker_t)); - if (tile->marker_mul.num_QCC >= tile->marker_mul.CzQCC) - { - tmp = (info_marker_t*)malloc((INCREMENT + tile->marker_mul.CzQCC) * sizeof(info_marker_t)); - memcpy(tmp,tile->marker_mul.QCC,tile->marker_mul.CzQCC); - tile->marker_mul.CzQCC += INCREMENT; - free(tile->marker_mul.QCC); - tile->marker_mul.QCC = tmp; - } - tile->marker_mul.QCC[tile->marker_mul.num_QCC].type = J2K_MS_QCC; - tile->marker_mul.QCC[tile->marker_mul.num_QCC].start_pos = cio_tell()-2; - tile->marker_mul.QCC[tile->marker_mul.num_QCC].len = len; - tile->marker_mul.num_QCC++; - } - /* [MHIX BOX] */ - - compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); - j2k_read_qcx(compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2)); -} - - - - -void j2k_read_poc() { - int len, numpchgs, i, old_poc; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - info_tile_t *tile; - - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - old_poc = tcp->POC ? tcp->numpocs+1 : 0; - tcp->POC = 1; - tccp = &tcp->tccps[0]; - len = cio_read(2); - - /* [MHIX BOX] */ - if (j2k_state == J2K_STATE_MH) - { - img.marker[img.num_marker].type = J2K_MS_POC; - img.marker[img.num_marker].start_pos = cio_tell()-2; - img.marker[img.num_marker].len = len; - img.num_marker++; - } else - { - tile = &img.tile[j2k_curtileno]; - tile->marker[tile->num_marker].type = J2K_MS_POC; - tile->marker[tile->num_marker].start_pos = cio_tell()-2; - tile->marker[tile->num_marker].len = len; - tile->num_marker++; - } - /* [MHIX BOX] */ - - numpchgs = (len - 2) / (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)); - for (i = 0; i < numpchgs; i++) { - j2k_poc_t *poc; - poc = &tcp->pocs[i]; - poc->resno0 = cio_read(1); - poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); - poc->layno1 = int_min(cio_read(2), tcp->numlayers); - poc->resno1 = int_min(cio_read(1), tccp->numresolutions); - poc->compno1 = int_min(cio_read(j2k_img->numcomps <= 256 ? 1 : 2), j2k_img->numcomps); - poc->prg = cio_read(1); - } - - tcp->numpocs = numpchgs + old_poc - 1; -} - - - - -void j2k_read_crg() { - int len, i, Xcrg_i, Ycrg_i; - - len = cio_read(2); - - /* [MHIX BOX] */ - img.marker[img.num_marker].type = J2K_MS_CRG; - img.marker[img.num_marker].start_pos = cio_tell()-2; - img.marker[img.num_marker].len = len; - img.num_marker++; - /* [MHIX BOX] */ - - for (i = 0; i < j2k_img->numcomps; i++) - { - Xcrg_i = cio_read(2); - Ycrg_i = cio_read(2); - } -} - - - - -void j2k_read_tlm() { - int len, Ztlm, Stlm, ST, SP, tile_tlm, i; - long int Ttlm_i, Ptlm_i; - info_marker_t *tmp; - - len = cio_read(2); - - /* [MHIX BOX] */ - if (!img.marker_mul.num_TLM) - img.marker_mul.TLM = (info_marker_t*)malloc(img.marker_mul.CzTLM * sizeof(info_marker_t)); - if (img.marker_mul.num_TLM >= img.marker_mul.CzTLM) - { - tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzTLM) * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.TLM,img.marker_mul.CzTLM); - img.marker_mul.CzTLM += INCREMENT; - free(img.marker_mul.TLM); - img.marker_mul.TLM = tmp; - } - img.marker_mul.TLM[img.marker_mul.num_TLM].type = J2K_MS_TLM; - img.marker_mul.TLM[img.marker_mul.num_TLM].start_pos = cio_tell()-2; - img.marker_mul.TLM[img.marker_mul.num_TLM].len = len; - img.marker_mul.num_TLM++; - /* [MHIX BOX] */ - - Ztlm = cio_read(1); - Stlm = cio_read(1); - ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); - SP = (Stlm >> 6) & 0x01; - tile_tlm = (len - 4) / ((SP + 1) * 2 + ST); - for (i = 0; i < tile_tlm; i++) - { - Ttlm_i = cio_read(ST); - Ptlm_i = cio_read(SP ? 4 : 2); - } -} - - - - -void j2k_read_plm() { - int len, i, Z_plm, N_plm, add, packet_len=0; - info_marker_t *tmp; - - len=cio_read(2); - - /* [MHIX BOX] */ - if (!img.marker_mul.num_PLM) - img.marker_mul.PLM = (info_marker_t*)malloc(img.marker_mul.CzPLM * sizeof(info_marker_t)); - if (img.marker_mul.num_PLM >= img.marker_mul.CzPLM) - { - tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzPLM) * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.PLM,img.marker_mul.CzPLM); - img.marker_mul.CzPLM += INCREMENT; - free(img.marker_mul.PLM); - img.marker_mul.PLM = tmp; - } - img.marker_mul.PLM[img.marker_mul.num_PLM].type = J2K_MS_PLM; - img.marker_mul.PLM[img.marker_mul.num_PLM].start_pos = cio_tell()-2; - img.marker_mul.PLM[img.marker_mul.num_PLM].len = len; - img.marker_mul.num_PLM++; - /* [MHIX BOX] */ - - Z_plm = cio_read(1); - len -= 3; - while (len > 0) - { - N_plm = cio_read(4); - len -= 4; - for (i = N_plm ; i > 0 ; i--) - { - add = cio_read(1); - len--; - packet_len = (packet_len << 7) + add; - if ((add & 0x80) == 0) - { - /* New packet */ - packet_len = 0; - } - if (len <= 0) break; - } - } -} - - - - -void j2k_read_plt() { - int len, i, Zplt, packet_len=0, add; - info_tile_t *tile; - info_marker_t *tmp; -; - len = cio_read(2); - - /* [MHIX BOX] */ - tile = &img.tile[j2k_curtileno]; - if (!tile->marker_mul.num_PLT) - tile->marker_mul.PLT = (info_marker_t*)malloc(tile->marker_mul.CzPLT * sizeof(info_marker_t)); - if (tile->marker_mul.num_PLT >= tile->marker_mul.CzPLT) - { - tmp = (info_marker_t*)malloc((INCREMENT + tile->marker_mul.CzPLT) * sizeof(info_marker_t)); - memcpy(tmp,tile->marker_mul.PLT,tile->marker_mul.CzPLT); - tile->marker_mul.CzPLT += INCREMENT; - free(tile->marker_mul.PLT); - tile->marker_mul.PLT = tmp; - } - - tile->marker_mul.PLT[tile->marker_mul.num_PLT].type = J2K_MS_PLT; - tile->marker_mul.PLT[tile->marker_mul.num_PLT].start_pos = cio_tell()-2; - tile->marker_mul.PLT[tile->marker_mul.num_PLT].len = len; - tile->marker_mul.num_PLT++; - /* [MHIX BOX] */ - - Zplt = cio_read(1); - for (i = len-3; i > 0; i--) - { - add = cio_read(1); - packet_len = (packet_len << 7) + add; - if ((add & 0x80) == 0) - { - /* New packet */ - packet_len = 0; - } - } -} - - - - -void j2k_read_ppm() { - int len, Z_ppm, i, j; - int N_ppm; - info_marker_t *tmp; - - len = cio_read(2); - - /* [MHIX BOX] */ - if (!img.marker_mul.num_PPM) - img.marker_mul.PPM = (info_marker_t*)malloc(img.marker_mul.CzPPM * sizeof(info_marker_t)); - if (img.marker_mul.num_PPM >= img.marker_mul.CzPPM) - { - tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzPPM) * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.PPM,img.marker_mul.CzPPM); - img.marker_mul.CzPPM += INCREMENT; - free(img.marker_mul.PPM); - img.marker_mul.PPM = tmp; - } - img.marker_mul.PLM[img.marker_mul.num_PPM].type = J2K_MS_PPM; - img.marker_mul.PLM[img.marker_mul.num_PPM].start_pos = cio_tell()-2; - img.marker_mul.PLM[img.marker_mul.num_PPM].len = len; - img.marker_mul.num_PPM++; - /* [MHIX BOX] */ - - j2k_cp->ppm = 1; - - Z_ppm = cio_read(1); /* Z_ppm */ - len -= 3; - while (len > 0) - { - if (j2k_cp->ppm_previous == 0) - { - N_ppm = cio_read(4); /* N_ppm */ - len -= 4; - } else - { - N_ppm = j2k_cp->ppm_previous; - } - - j = j2k_cp->ppm_store; - if (Z_ppm == 0) /* First PPM marker */ - j2k_cp->ppm_data = (unsigned char*)calloc(N_ppm, sizeof(unsigned char)); - else /* NON-first PPM marker */ - j2k_cp->ppm_data = (unsigned char*)realloc(j2k_cp->ppm_data, (N_ppm + j2k_cp->ppm_store) * sizeof(unsigned char)); - - for (i = N_ppm ; i > 0 ; i--) /* Read packet header */ - { - j2k_cp->ppm_data[j] = cio_read(1); - j++; - len--; - if (len == 0) break; /* Case of non-finished packet header in present marker but finished in next one */ - } - - j2k_cp->ppm_previous = i - 1; - j2k_cp->ppm_store = j; - } -} - - - - -void j2k_read_ppt() { - int len, Z_ppt, i, j = 0; - j2k_tcp_t *tcp; - info_tile_t *tile; - len=cio_read(2); - - /* [MHIX BOX] */ - tile = & img.tile[j2k_curtileno]; - tile->marker[tile->num_marker].type = J2K_MS_PPT; - tile->marker[tile->num_marker].start_pos = cio_tell()-2; - tile->marker[tile->num_marker].len = len; - tile->num_marker++; - /* [MHIX BOX] */ - - Z_ppt = cio_read(1); - tcp = &j2k_cp->tcps[j2k_curtileno]; - tcp->ppt = 1; - if (Z_ppt == 0) /* First PPT marker */ - { - tcp->ppt_data = (unsigned char*)calloc(len - 3, sizeof(unsigned char)); - tcp->ppt_store = 0; - } - else /* NON-first PPT marker */ - tcp->ppt_data = (unsigned char*)realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char)); - - j = tcp->ppt_store; - for (i = len - 3 ; i > 0 ; i--) - { - tcp->ppt_data[j] = cio_read(1); - j++; - } - tcp->ppt_store = j; -} - - - - -void j2k_read_sot() { - int len, tileno, totlen, partno, numparts, i; - j2k_tcp_t *tcp; - j2k_tccp_t *tmp; - info_tile_t *tile; - info_tile_part_t *tilepart_tmp; - - - //fprintf(stderr,"SOT\n"); - len = cio_read(2); - tileno = cio_read(2); - /* [MHIX BOX] */ - tile = & img.tile[tileno]; - tile->marker[tile->num_marker].type = J2K_MS_SOT; - tile->marker[tile->num_marker].start_pos = cio_tell() - 4; - tile->marker[tile->num_marker].len = len; - tile->num_marker++; - /* [MHIX BOX] */ - - totlen = cio_read(4); - if (!totlen) totlen = cio_numbytesleft() + 8; - partno = cio_read(1); - numparts = cio_read(1); - - /* */ - if (tileno == 0 && partno == 0 ) - img.Main_head_end = cio_tell() - 7; /* Correction End = First byte of first SOT */ - - img.tile[tileno].num_tile = tileno; - /* */ - - tile->numparts = partno + 1; /* INDEX : Number of tile_parts for the tile */ - img.num_max_tile_parts = int_max(tile->numparts, img.num_max_tile_parts); /* INDEX : Maximum number of tile_part per tile */ - - if (partno == 0) - { - tile->tile_parts = (info_tile_part_t*)malloc(START_NB * sizeof(info_tile_part_t*)); - tile->Cztile_parts = START_NB; - } - if (partno >= tile->Cztile_parts) - { - tilepart_tmp = (info_tile_part_t*)malloc((INCREMENT + tile->Cztile_parts) * sizeof(info_tile_part_t)); - memcpy(tmp, tile->tile_parts, tile->Cztile_parts); - tile->Cztile_parts += INCREMENT; - free(tile->tile_parts); - tile->tile_parts = tilepart_tmp; - } - - tile->tile_parts[partno].start_pos = cio_tell() - 12; /* INDEX : start_pos of the tile_part */ - tile->tile_parts[partno].length = totlen; /* INDEX : length of the tile_part */ - tile->tile_parts[partno].end_pos = totlen + cio_tell() - 12; /* INDEX : end position of the tile_part */ - - - j2k_curtileno = tileno; - j2k_eot = cio_getbp() - 12 + totlen; - j2k_state = J2K_STATE_TPH; - tcp = &j2k_cp->tcps[j2k_curtileno]; - - tile->tile_parts[numparts].num_reso_AUX = tcp->tccps[0].numresolutions; /* INDEX : AUX value for TPIX */ - - if (partno == 0) - // if (tcp->first == 1) - { - tmp = tcp->tccps; - *tcp = j2k_default_tcp; - /* Initialization PPT */ - tcp->ppt = 0; - tcp->ppt_data = NULL; - - tcp->tccps = tmp; - for (i = 0; i < j2k_img->numcomps; i++) { - tcp->tccps[i] = j2k_default_tcp.tccps[i]; - } - //j2k_cp->tcps[j2k_curtileno].first=0; - } -} - - - -void j2k_read_rgn() { - int len, compno, roisty; - j2k_tcp_t *tcp; - info_tile_t *tile; - info_marker_t *tmp; - // fprintf(stderr,"RGN\n"); - tcp = j2k_state == J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp; - len = cio_read(2); - - /* [MHIX BOX]*/ - if (j2k_state == J2K_STATE_MH) - { - if (!img.marker_mul.num_RGN) - img.marker_mul.RGN = (info_marker_t*)malloc(img.marker_mul.CzRGN * sizeof(info_marker_t)); - if (img.marker_mul.num_RGN >= img.marker_mul.CzRGN) - { - tmp = (info_marker_t*)malloc((INCREMENT + img.marker_mul.CzRGN) * sizeof(info_marker_t)); - memcpy(tmp,img.marker_mul.RGN, img.marker_mul.CzRGN); - img.marker_mul.CzRGN += INCREMENT; - free(img.marker_mul.RGN); - img.marker_mul.RGN = tmp; - } - img.marker_mul.RGN[img.marker_mul.num_RGN].type = J2K_MS_RGN; - img.marker_mul.RGN[img.marker_mul.num_RGN].start_pos = cio_tell() - 2; - img.marker_mul.RGN[img.marker_mul.num_RGN].len = len; - img.marker_mul.num_RGN++; - } else - { - tile = &img.tile[j2k_curtileno]; - if (!tile->marker_mul.num_RGN) - tile->marker_mul.RGN = (info_marker_t*)malloc(tile->marker_mul.CzRGN * sizeof(info_marker_t)); - if (tile->marker_mul.num_RGN >= tile->marker_mul.CzRGN) - { - tmp = (info_marker_t*)malloc((INCREMENT + tile->marker_mul.CzRGN) * sizeof(info_marker_t)); - memcpy(tmp,tile->marker_mul.RGN,tile->marker_mul.CzRGN); - tile->marker_mul.CzRGN += INCREMENT; - free(tile->marker_mul.RGN); - tile->marker_mul.RGN = tmp; - } - - tile->marker_mul.RGN[tile->marker_mul.num_RGN].type = J2K_MS_RGN; - tile->marker_mul.RGN[tile->marker_mul.num_RGN].start_pos = cio_tell() - 2; - tile->marker_mul.RGN[tile->marker_mul.num_RGN].len = len; - tile->marker_mul.num_RGN++; - } - /* [MHIX BOX] */ - - compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); - roisty = cio_read(1); - tcp->tccps[compno].roishift = cio_read(1); -} - - - - - -void j2k_read_sod() { - int len; - unsigned char *data; - info_tile_t *tile; - info_tile_part_t *tile_part; - // fprintf(stderr,"SOD\n"); - /* [MHIX BOX] */ - tile = &img.tile[j2k_curtileno]; - tile->marker[tile->num_marker].type = J2K_MS_SOD; - tile->marker[tile->num_marker].start_pos = cio_tell(); - tile->marker[tile->num_marker].len = 0; - tile->num_marker++; - /* [MHIX BOX] */ - - tile_part = &tile->tile_parts[tile->numparts - 1]; /* INDEX : Current tilepart of a tile */ - tile_part->length_header = cio_tell() - 1 - tile_part->start_pos; /* INDEX : length of the tile-part header */ - tile_part->end_header = cio_tell() - 1; /* INDEX : end header position of the tile-part header */ - - len = int_min(j2k_eot - cio_getbp(), cio_numbytesleft()); - - j2k_tile_len[j2k_curtileno] += len; - data = (unsigned char*)realloc(j2k_tile_data[j2k_curtileno], j2k_tile_len[j2k_curtileno]); - memcpy(data, cio_getbp(), len); - j2k_tile_data[j2k_curtileno] = data; - cio_skip(len); - j2k_state = J2K_STATE_TPHSOT; -} - -void j2k_read_eoc() { - int tileno; - tcd_init(j2k_img, j2k_cp, &img); - for (tileno = 0; tilenotw * j2k_cp->th; tileno++) { - tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno, &img); - } - - j2k_state = J2K_STATE_MT; - longjmp(j2k_error, 1); -} - - - - -void j2k_read_unk() { - fprintf(stderr, "warning: unknown marker\n"); -} - - - - -int j2k_index_JPIP(char *Idx_file, char *J2K_file, int len, int version){ - FILE *dest; - char *index; - int pos_iptr, end_pos; - int len_cidx, pos_cidx; - int len_jp2c, pos_jp2c; - int len_fidx, pos_fidx; - - dest=fopen(Idx_file, "wb"); - if (!dest) { - fprintf(stderr, "Failed to open %s for reading !!\n", Idx_file); - return 0; - } - - /* INDEX MODE JPIP */ - index = (char*)malloc(len); - cio_init(index, len); - jp2_write_jp(); - jp2_write_ftyp(); - - jp2_write_jp2h(j2k_img); - jp2_write_dbtl(Idx_file); - - pos_iptr=cio_tell(); - cio_skip(24); /* IPTR further ! */ - - pos_jp2c = cio_tell(); - len_jp2c = jp2_write_jp2c(J2K_file); - - pos_cidx = cio_tell(); - len_cidx = jpip_write_cidx(pos_jp2c + 8,img, j2k_cp, version); /* Correction len_jp2C --> pos_jp2c + 8 */ - - - pos_fidx = cio_tell(); - len_fidx = jpip_write_fidx(pos_jp2c, len_jp2c, pos_cidx, len_cidx); - -end_pos = cio_tell(); - - cio_seek(pos_iptr); - jpip_write_iptr(pos_fidx,len_fidx); - cio_seek(end_pos); - - fwrite(index, 1, cio_tell(), dest); - free(index); - - fclose(dest); - return 1; -} - - - -typedef struct { - int id; - int states; - void (*handler)(); -} j2k_dec_mstabent_t; - -j2k_dec_mstabent_t j2k_dec_mstab[]={ - {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc}, - {J2K_MS_SOT, J2K_STATE_MH|J2K_STATE_TPHSOT, j2k_read_sot}, - {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod}, - {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc}, - {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz}, - {J2K_MS_COD, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_cod}, - {J2K_MS_COC, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_coc}, - {J2K_MS_RGN, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_rgn}, - {J2K_MS_QCD, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_qcd}, - {J2K_MS_QCC, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_qcc}, - {J2K_MS_POC, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_poc}, - {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm}, - {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm}, - {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt}, - {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm}, - {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt}, - {J2K_MS_SOP, 0, 0}, - {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg}, - {J2K_MS_COM, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_com}, - {0, J2K_STATE_MH|J2K_STATE_TPH, j2k_read_unk} -}; - -j2k_dec_mstabent_t *j2k_dec_mstab_lookup(int id) { - j2k_dec_mstabent_t *e; - for (e = j2k_dec_mstab; e->id != 0; e++) { - if (e->id == id) { - break; - } - } - return e; -} - -int j2k_decode(unsigned char *src, int len, j2k_image_t **image, j2k_cp_t **cp) { - if (setjmp(j2k_error)) { - if (j2k_state != J2K_STATE_MT) { - fprintf(stderr, "WARNING: incomplete bitstream\n"); - return 0; - } - return cio_numbytes(); - } - j2k_img = (j2k_image_t*)calloc(1, sizeof(j2k_image_t)); - j2k_cp = (j2k_cp_t*)calloc(1, sizeof(j2k_cp_t)); - *image = j2k_img; - *cp = j2k_cp; - j2k_state = J2K_STATE_MHSOC; - cio_init(src, len); - for (;;) { - j2k_dec_mstabent_t *e; - int id = cio_read(2); - if (id >> 8 != 0xff) { - fprintf(stderr, "%.8x: expected a marker instead of %x\n", cio_tell() - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup(id); - if (!(j2k_state & e->states)) { - fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id); - return 0; - } - if (e->handler) { - (*e->handler)(); - } - } - -} - - -#ifdef WIN32 -#include - -BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - switch (ul_reason_for_call) { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - break; - } - return TRUE; -} -#endif - -int main(int argc, char **argv) -{ - FILE *src; - int totlen; - char *j2kfile; - j2k_image_t *imgg; - j2k_cp_t *cp; - int version; - - if (argc != 4) - { - fprintf(stderr,"\nUSAGE : ./index_create J2K-file JP2-file version\n\nVersion : 0, 1, 2 or 3\n 0 : [faix] 4-byte and no AUX fields\n 1 : [faix] 8-byte and no AUX fields\n 2 : [faix] 4-byte and AUX fields\n 3 : [faix] 8-byte and AUX fields\n\nReference Document : annex I from JPIP-FCD-version 2 (SC 29 N5727)\n\n"); - return 1; - } - - src=fopen(argv[1], "rb"); - if (!src) { - fprintf(stderr, "Failed to open %s for reading !!\n", argv[1]); - return 1; - } - - /* length of the codestream */ - fseek(src, 0, SEEK_END); - totlen = ftell(src); - fseek(src, 0, SEEK_SET); - - j2kfile = (char*)malloc(totlen); - fread(j2kfile, 1, totlen, src); - fclose(src); - - img.marker = (info_marker_t*)malloc(32 * sizeof(info_marker_t)); - img.num_marker = 0; - img.num_max_tile_parts = 0; - img.marker_mul.num_COC = 0; - img.marker_mul.CzCOC = START_NB; - img.marker_mul.num_RGN = 0; - img.marker_mul.CzRGN = START_NB; - img.marker_mul.num_QCC = 0; - img.marker_mul.CzQCC = START_NB; - img.marker_mul.num_TLM = 0; - img.marker_mul.CzTLM = START_NB; - img.marker_mul.num_PLM = 0; - img.marker_mul.CzPLM = START_NB; - img.marker_mul.num_PPM = 0; - img.marker_mul.CzPPM = START_NB; - img.marker_mul.num_COM = 0; - img.marker_mul.CzCOM = START_NB; - - /* decode */ - - if (!j2k_decode(j2kfile, totlen, &imgg, &cp)) { - fprintf(stderr, "Index_creator: failed to decode image!\n"); - return 1; - } - free(j2kfile); - - // fseek(src, 0, SEEK_SET); - img.codestream_size = totlen; - sscanf(argv[3], "%d", &version); - if (version > 3) - { - fprintf(stderr,"Error : value of version unauthorized !! Value accepted : 0, 1, 2 or 3 !!\n"); - return 0; - } - - j2k_index_JPIP(argv[2], argv[1], totlen * 2 > 60000 ? totlen * 2 : 60000, version); - - j2k_clean(); - return 0; -} - diff --git a/indexer_JPIP/int.c b/indexer_JPIP/int.c deleted file mode 100644 index 29f778c9..00000000 --- a/indexer_JPIP/int.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/// -/// Get the minimum of two integers. -/// -int int_min(int a, int b) { - return a -/// Get the maximum of two integers. -/// -int int_max(int a, int b) { - return a>b?a:b; -} - -/// -/// Clamp an integer inside an interval. -/// -int int_clamp(int a, int min, int max) { - if (amax) return max; - return a; -} - -/// -/// Get absolute value of integer. -/// -int int_abs(int a) { - return a<0?-a:a; -} - -/// -/// Divide an integer and round upwards. -/// -int int_ceildiv(int a, int b) { - return (a+b-1)/b; -} - -/// -/// Divide an integer by a power of 2 and round upwards. -/// -int int_ceildivpow2(int a, int b) { - return (a+(1<>b; -} - -/// -/// Divide an integer by a power of 2 and round downwards. -/// -int int_floordivpow2(int a, int b) { - return a>>b; -} - -/// -/// Get logarithm of an integer and round downwards. -/// -int int_floorlog2(int a) { - int l; - for (l=0; a>1; l++) { - a>>=1; - } - return l; -} diff --git a/indexer_JPIP/int.h b/indexer_JPIP/int.h deleted file mode 100644 index 4921ff4d..00000000 --- a/indexer_JPIP/int.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __INT_H -#define __INT_H - -int int_min(int a, int b); -int int_max(int a, int b); -int int_clamp(int a, int min, int max); -int int_abs(int a); -int int_ceildiv(int a, int b); -int int_ceildivpow2(int a, int b); -int int_floordivpow2(int a, int b); -int int_floorlog2(int a); - -#endif diff --git a/indexer_JPIP/j2k.h b/indexer_JPIP/j2k.h deleted file mode 100644 index 5ead3b45..00000000 --- a/indexer_JPIP/j2k.h +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003-2004, Yannick Verschueren - * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#define VERSION "0.0.8" - -#ifdef WIN32 -#ifdef LIBJ2K_EXPORTS -#define LIBJ2K_API __declspec(dllexport) -#else -#define LIBJ2K_API __declspec(dllimport) -#endif -#else -#define LIBJ2K_API -#endif - -#ifndef __J2K_H -#define __J2K_H - -#define J2K_MAXRLVLS 33 -#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1) - -#define J2K_CP_CSTY_PRT 0x01 -#define J2K_CP_CSTY_SOP 0x02 -#define J2K_CP_CSTY_EPH 0x04 -#define J2K_CCP_CSTY_PRT 0x01 -#define J2K_CCP_CBLKSTY_LAZY 0x01 -#define J2K_CCP_CBLKSTY_RESET 0x02 -#define J2K_CCP_CBLKSTY_TERMALL 0x04 -#define J2K_CCP_CBLKSTY_VSC 0x08 -#define J2K_CCP_CBLKSTY_PTERM 0x10 -#define J2K_CCP_CBLKSTY_SEGSYM 0x20 -#define J2K_CCP_QNTSTY_NOQNT 0 -#define J2K_CCP_QNTSTY_SIQNT 1 -#define J2K_CCP_QNTSTY_SEQNT 2 - -typedef struct -{ - int dx, dy; /* XRsiz, YRsiz */ - int prec; /* precision */ - int bpp; /* deapth of image in bits */ - int sgnd; /* signed */ - int *data; /* image-component data */ -} j2k_comp_t; - -typedef struct { - int version; - int x0, y0; /* XOsiz, YOsiz */ - int x1, y1; /* Xsiz, Ysiz */ - int numcomps; /* number of components */ - int index_on; /* 0 = no index || 1 = index */ - j2k_comp_t *comps; /* image-components */ -} j2k_image_t; - -typedef struct { - int expn; /* exponent */ - int mant; /* mantissa */ -} j2k_stepsize_t; - -typedef struct { - int csty; /* coding style */ - int numresolutions; /* number of resolutions */ - int cblkw; /* width of code-blocks */ - int cblkh; /* height of code-blocks */ - int cblksty; /* code-block coding style */ - int qmfbid; /* discrete wavelet transform identifier */ - int qntsty; /* quantisation style */ - j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantisation */ - int numgbits; /* number of guard bits */ - int roishift; /* Region of Interest shift */ - int prcw[J2K_MAXRLVLS]; /* Precinct width */ - int prch[J2K_MAXRLVLS]; /* Precinct height */ -} j2k_tccp_t; - -typedef struct { - int resno0, compno0; - int layno1, resno1, compno1; - int prg; - int tile; - char progorder[4]; -} j2k_poc_t; - -typedef struct { - int csty; /* coding style */ - int prg; /* progression order */ - int numlayers; /* number of layers */ - int mct; /* multi-component transform identifier */ - int rates[100]; /* rates of layers */ - int numpocs; /* number of progression order changes */ - int POC; /* Precise if a POC marker has been used O:NO, 1:YES */ - j2k_poc_t pocs[32]; /* progression order changes */ - unsigned char *ppt_data; /* packet header store there for futur use in t2_decode_packet */ - int ppt; /* If ppt == 1 --> there was a PPT marker for the present tile */ - int ppt_store; /* Use in case of multiple marker PPT (number of info already store) */ - j2k_tccp_t *tccps; /* tile-component coding parameters */ -} j2k_tcp_t; - -typedef struct { - int tx0, ty0; /* XTOsiz, YTOsiz */ - int tdx, tdy; /* XTsiz, YTsiz */ - int tw, th; - unsigned char *ppm_data; /* packet header store there for futur use in t2_decode_packet */ - int ppm; /* If ppm == 1 --> there was a PPM marker for the present tile */ - int ppm_store; /* Use in case of multiple marker PPM (number of info already store) */ - int ppm_previous; /* Use in case of multiple marker PPM (case on non-finished previous info) */ - j2k_tcp_t *tcps; /* tile coding parameters */ -} j2k_cp_t; - - - - - -/* Packet information : Layer level */ -typedef struct { - int len; /* Length of the body of the packet */ - int len_header; /* Length of the header of the packet */ - int offset; /* Offset of the body of the packet */ - int offset_header; /* Offset of the header of the packet */ -} info_layer_t; - - -/* Access to packet information : precinct level */ -typedef struct { - info_layer_t *layer; -} info_prec_t; - - -/* Access to packet information : resolution level */ -typedef struct { - info_prec_t *prec; -} info_reso_t; - - -/* Access to packet information : component level */ -typedef struct { - info_reso_t *reso; -} info_compo_t; - - -/* Information about the marker */ -typedef struct { - int type; /* type of marker [SIZ, QCD, POC, PPM, CRG, COD] appearing only once */ - int start_pos; /* Start position of the marker */ - int len; /* Length of the marker */ -} info_marker_t; - - -/* Multiple marker in tile header */ -typedef struct{ - info_marker_t *COC; /* COC markers */ - int num_COC; /* Number of COC marker */ - int CzCOC; /* Current size of the vector COC */ - - info_marker_t *RGN; /* RGN markers */ - int num_RGN; /* Number of RGN marker */ - int CzRGN; /* Current size of the vector RGN */ - - info_marker_t *QCC; /* QCC markers */ - int num_QCC; /* Number of QCC marker */ - int CzQCC; /* Current size of the vector QCC */ - - info_marker_t *PLT; /* PLT markers */ - int num_PLT; /* Number of PLT marker */ - int CzPLT; /* Current size of the vector PLT */ - - info_marker_t *PPT; /* PPT markers */ - int num_PPT; /* Number of PPT marker */ - int CzPPT; /* Current size of the vector PPT */ - - info_marker_t *COM; /* COM markers */ - int num_COM; /* Number of COM marker */ - int CzCOM; /* Current size of the vector COC */ -} info_marker_mul_tile_t; - - -/* Information about each tile_part for a particulary tile */ -typedef struct{ - int start_pos; /* Start position of the tile_part */ - int length; /* Length of the tile_part header + body */ - int length_header; /* Length of the header */ - int end_pos; /* End position of the tile part */ - int end_header; /* End position of the tile part header */ - - int num_reso_AUX; /* Number of resolution level completed */ -} info_tile_part_t; - - -/* Information about each tile */ -typedef struct { - int num_tile; /* Number of Tile */ - int pw, ph; /* number of precinct by tile */ - int num_packet; /* number of packet in the tile */ - info_compo_t *compo; /* component [packet] */ - - info_marker_t *marker; /* information concerning markers inside image [only one apparition] */ - info_marker_mul_tile_t marker_mul; /* information concerning markers inside image [multiple apparition] */ - int num_marker; /* number of marker */ - - int numparts; /* number of tile_part for this tile */ - info_tile_part_t *tile_parts; /* Information about each tile_part */ - int Cztile_parts; /* Current size of the tile_parts vector */ -} info_tile_t; /* index struct */ - - -/* Multiple marker in main header */ -typedef struct{ - info_marker_t *COC; /* COC markers */ - int num_COC; /* Number of COC marker */ - int CzCOC; /* Current size of the vector COC */ - - info_marker_t *RGN; /* RGN markers */ - int num_RGN; /* Number of RGN marker */ - int CzRGN; /* Current size of the vector RGN */ - - info_marker_t *QCC; /* QCC markers */ - int num_QCC; /* Number of QCC marker */ - int CzQCC; /* Current size of the vector QCC */ - - info_marker_t *TLM; /* TLM markers */ - int num_TLM; /* Number of TLM marker */ - int CzTLM; /* Current size of the vector TLM */ - - info_marker_t *PLM; /* PLM markers */ - int num_PLM; /* Number of PLM marker */ - int CzPLM; /* Current size of the vector PLM */ - - info_marker_t *PPM; /* PPM markers */ - int num_PPM; /* Number of PPM marker */ - int CzPPM; /* Current size of the vector PPM */ - - info_marker_t *COM; /* COM markers */ - int num_COM; /* Number of COM marker */ - int CzCOM; /* Current size of the vector COM */ -} info_marker_mul_t; /* index struct */ - - -/* Information about image */ -typedef struct { - int Im_w, Im_h; /* Image width and Height */ - int Tile_x, Tile_y; /* Number of Tile in X and Y */ - int tw, th; - int pw, ph; /* nombre precinct in X and Y */ - int pdx, pdy; /* size of precinct in X and Y */ - - int Prog; /* progression order */ - int Comp; /* Component numbers */ - int Layer; /* number of layer */ - int Decomposition; /* number of decomposition */ - - int Main_head_end; /* Main header position */ - int codestream_size; /* codestream's size */ - - info_marker_t *marker; /* information concerning markers inside image [only one apparition] */ - info_marker_mul_t marker_mul; /* information concerning markers inside image [multiple apparition] */ - int num_marker; /* number of marker */ - - int num_packet_max; /* Maximum number of packet */ - - int num_max_tile_parts; /* Maximum number of tile-part */ - info_tile_t *tile; /* information concerning tiles inside image */ -} info_image_t; /* index struct */ - - -#endif diff --git a/indexer_JPIP/jp2.c b/indexer_JPIP/jp2.c deleted file mode 100644 index 1c193ad7..00000000 --- a/indexer_JPIP/jp2.c +++ /dev/null @@ -1,301 +0,0 @@ -/* - * Copyright (c) 2003-2004, Yannick Verschueren - * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include - -#include "j2k.h" -#include "cio.h" -#include "tcd.h" -#include "int.h" - -#define JPIP_JPIP 0x6a706970 - -#define JP2_JP 0x6a502020 -#define JP2_FTYP 0x66747970 -#define JP2_JP2H 0x6a703268 -#define JP2_IHDR 0x69686472 -#define JP2_COLR 0x636f6c72 -#define JP2_JP2C 0x6a703263 -#define JP2_URL 0x75726c20 -#define JP2_DBTL 0x6474626c -#define JP2_BPCC 0x62706363 -#define JP2 0x6a703220 - - -void jp2_write_url(char *Idx_file) -{ - int len, lenp, i; - char str[256]; - - sprintf(str, "%s", Idx_file); - lenp=cio_tell(); - cio_skip(4); - cio_write(JP2_URL, 4); // DBTL - cio_write(0,1); // VERS - cio_write(0,3); // FLAG - - for (i=0; iy1-j2k_img->x0,4); // HEIGHT - cio_write(j2k_img->x1-j2k_img->x0,4); // WIDTH - cio_write(j2k_img->numcomps,2); // NC - - depth_0=j2k_img->comps[0].prec-1; - sign=j2k_img->comps[0].sgnd; - - for(i=1;inumcomps;i++) - { - depth=j2k_img->comps[i].prec-1; - sign=j2k_img->comps[i].sgnd; - if(depth_0!=depth) BPC_ok=0; - } - - if (BPC_ok) - cio_write(depth_0+(sign<<7),1); - else - cio_write(255,1); - - cio_write(7,1); // C : Always 7 - cio_write(1,1); // UnkC, colorspace unknow - cio_write(0,1); // IPR, no intellectual property - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len,4); // L - cio_seek(lenp+len); - - return BPC_ok; -} - -void jp2_write_bpcc(j2k_image_t *j2k_img) -{ - int len, lenp, i; - - lenp=cio_tell(); - cio_skip(4); - cio_write(JP2_BPCC, 4); // BPCC - - for(i=0;inumcomps;i++) - cio_write(j2k_img->comps[i].prec-1+(j2k_img->comps[i].sgnd<<7),1); - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len,4); // L - cio_seek(lenp+len); -} - -void jp2_write_colr(int BPC_ok, j2k_image_t *j2k_img) -{ - int len, lenp, meth; - - lenp=cio_tell(); - cio_skip(4); - cio_write(JP2_COLR, 4); // COLR - - if ((j2k_img->numcomps==1 || j2k_img->numcomps==3) && (BPC_ok && j2k_img->comps[0].prec==8)) - meth=1; - else - meth=2; - - cio_write(meth,1); // METH - cio_write(0,1); // PREC - cio_write(0,1); // APPROX - - if (meth==1) - cio_write(j2k_img->numcomps>1?16:17,4); // EnumCS - - if (meth==2) - cio_write(0,1); // PROFILE (??) - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len,4); // L - cio_seek(lenp+len); -} - -/* - * Write the JP2H box - * - * JP2 Header box - * - */ -void jp2_write_jp2h(j2k_image_t *j2k_img) -{ - int len, lenp, BPC_ok; - - lenp=cio_tell(); - cio_skip(4); - cio_write(JP2_JP2H, 4); /* JP2H */ - - BPC_ok=jp2_write_ihdr(j2k_img); - - if (!BPC_ok) - jp2_write_bpcc(j2k_img); - jp2_write_colr(BPC_ok, j2k_img); - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len,4); /* L */ - cio_seek(lenp+len); -} - -/* - * Write the FTYP box - * - * File type box - * - */ -void jp2_write_ftyp() -{ - int len, lenp; - - lenp=cio_tell(); - cio_skip(4); - cio_write(JP2_FTYP, 4); /* FTYP */ - - cio_write(JP2,4); /* BR */ - cio_write(0,4); /* MinV */ - cio_write(JP2,4); /* CL0 : JP2 */ - cio_write(JPIP_JPIP,4); /* CL1 : JPIP */ - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len,4); /* L */ - cio_seek(lenp+len); -} - -/* - * Read the FTYP box - * - * File type box - * - */ -void jp2_read_ftyp(int length) -{ - int BR, MinV, type, i; - - BR = cio_read(4); /* BR */ - MinV = cio_read(4); /* MinV */ - length-=8; - - for (i=length/4;i>0;i--) - type = cio_read(4); /* CLi : JP2, JPIP */ -} - -int jp2_write_jp2c(char *J2K_file) -{ - int len, lenp, totlen, i; - FILE *src; - char *j2kfile; - - lenp=cio_tell(); - cio_skip(4); - cio_write(JP2_JP2C, 4); // JP2C - - src=fopen(J2K_file, "rb"); - fseek(src, 0, SEEK_END); - totlen=ftell(src); - fseek(src, 0, SEEK_SET); - - j2kfile=(char*)malloc(totlen); - fread(j2kfile, 1, totlen, src); - fclose(src); - - for (i=0;i -#include -#include -#include -#include - -#include "j2k.h" -#include "cio.h" -#include "tcd.h" -#include "int.h" - -#define JPIP_CIDX 0x63696478 /* Codestream index */ -#define JPIP_CPTR 0x63707472 /* Codestream Finder Box */ -#define JPIP_MANF 0x6d616e66 /* Manifest Box */ -#define JPIP_FAIX 0x66616978 /* Fragment array Index box */ -#define JPIP_MHIX 0x6d686978 /* Main Header Index Table */ -#define JPIP_TPIX 0x74706978 /* Tile-part Index Table box */ -#define JPIP_THIX 0x74686978 /* Tile header Index Table box */ -#define JPIP_PPIX 0x70706978 /* Precinct Packet Index Table box */ -#define JPIP_PHIX 0x70686978 /* Packet Header index Table */ -#define JPIP_FIDX 0x66696478 /* File Index */ -#define JPIP_FPTR 0x66707472 /* File Finder */ -#define JPIP_PRXY 0x70727879 /* Proxy boxes */ -#define JPIP_IPTR 0x69707472 /* Index finder box */ -#define JPIP_PHLD 0x70686c64 /* Place holder */ - -#define JP2C 0x6a703263 - -//static info_marker_t marker_jpip[32], marker_local_jpip[32]; /* SIZE to precise ! */ -//static int num_marker_jpip, num_marker_local_jpip; - -/* - * Write the CPTR box - * - * Codestream finder box (box) - * - */ -void jpip_write_cptr(int offset, info_image_t img) -{ - int len, lenp; - - lenp=cio_tell(); - cio_skip(4); /* L [at the end] */ - cio_write(JPIP_CPTR,4); /* T */ - cio_write(0,2); /* DR A PRECISER !! */ - cio_write(0,2); /* CONT */ - cio_write(offset,8); /* COFF A PRECISER !! */ - cio_write(img.codestream_size,8); /* CLEN */ - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len, 4); /* L */ - cio_seek(lenp+len); -} - -/* - * Read the CPTR box - * - * Codestream finder box (box) - * - */ -void jpip_read_cptr() -{ - int DR, CONT; - long long Coff, codestream_size; - - DR = cio_read(2); /* DR */ - CONT = cio_read(2); /* CONT */ - Coff = cio_read(8); /* COFF */ - codestream_size = cio_read(8); /* CLEN */ -} - -/* - * Write the MANF box - * - * Manifest box (box) - * - */ -void jpip_write_manf(int second, int v, info_marker_t *marker) -{ - int len, lenp, i; - lenp=cio_tell(); - cio_skip(4); /* L [at the end] */ - cio_write(JPIP_MANF,4); /* T */ - - if (second) /* Write only during the second pass */ - { - for(i=0;i= 0; i--) /* COC */ - { - cio_write(img.marker_mul.COC[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.COC[i].start_pos, 8); - cio_write(img.marker_mul.COC[i].len, 2); - } - - for(i = img.marker_mul.num_RGN - 1; i >= 0; i--) /* RGN */ - { - cio_write(img.marker_mul.RGN[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.RGN[i].start_pos, 8); - cio_write(img.marker_mul.RGN[i].len, 2); - } - - for(i = img.marker_mul.num_QCC - 1; i >= 0; i--) /* QCC */ - { - cio_write(img.marker_mul.QCC[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.QCC[i].start_pos, 8); - cio_write(img.marker_mul.QCC[i].len, 2); - } - - for(i = img.marker_mul.num_TLM - 1; i >= 0; i--) /* TLM */ - { - cio_write(img.marker_mul.TLM[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.TLM[i].start_pos, 8); - cio_write(img.marker_mul.TLM[i].len, 2); - } - - for(i = img.marker_mul.num_PLM - 1; i >= 0; i--) /* PLM */ - { - cio_write(img.marker_mul.PLM[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.PLM[i].start_pos, 8); - cio_write(img.marker_mul.PLM[i].len, 2); - } - - for(i = img.marker_mul.num_PPM - 1; i >= 0; i--) /* PPM */ - { - cio_write(img.marker_mul.PPM[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.PPM[i].start_pos, 8); - cio_write(img.marker_mul.PPM[i].len, 2); - } - - for(i = img.marker_mul.num_COM - 1; i >= 0; i--) /* COM */ - { - cio_write(img.marker_mul.COM[i].type, 2); - cio_write(i, 2); - cio_write(img.marker_mul.COM[i].start_pos, 8); - cio_write(img.marker_mul.COM[i].len, 2); - } - } - else /* TILE HEADER */ - { - tile = &img.tile[tileno]; - cio_write(tile->tile_parts[0].length_header, 8); /* TLEN */ - - for(i = 0; i < tile->num_marker; i++) /* Marker restricted to 1 apparition */ - { - cio_write(tile->marker[i].type, 2); - cio_write(0, 2); - cio_write(tile->marker[i].start_pos, 8); - cio_write(tile->marker[i].len, 2); - } - - /* Marker NOT restricted to 1 apparition */ - for(i = tile->marker_mul.num_COC - 1; i >= 0; i--) /* COC */ - { - cio_write(tile->marker_mul.COC[i].type, 2); - cio_write(i, 2); - cio_write(tile->marker_mul.COC[i].start_pos, 8); - cio_write(tile->marker_mul.COC[i].len, 2); - } - - for(i = tile->marker_mul.num_RGN - 1; i >= 0; i--) /* RGN */ - { - cio_write(tile->marker_mul.RGN[i].type, 2); - cio_write(i, 2); - cio_write(tile->marker_mul.RGN[i].start_pos, 8); - cio_write(tile->marker_mul.RGN[i].len, 2); - } - - for(i = tile->marker_mul.num_QCC - 1; i >= 0; i--) /* QCC */ - { - cio_write(tile->marker_mul.QCC[i].type, 2); - cio_write(i, 2); - cio_write(tile->marker_mul.QCC[i].start_pos, 8); - cio_write(tile->marker_mul.QCC[i].len, 2); - } - - for(i = tile->marker_mul.num_PLT - 1; i >= 0; i--) /* PLT */ - { - cio_write(tile->marker_mul.PLT[i].type,2); - cio_write(i,2); - cio_write(tile->marker_mul.PLT[i].start_pos,8); - cio_write(tile->marker_mul.PLT[i].len,2); - } - - for(i = tile->marker_mul.num_PPT - 1; i >= 0; i--) /* PPT */ - { - cio_write(tile->marker_mul.PPT[i].type, 2); - cio_write(i, 2); - cio_write(tile->marker_mul.PPT[i].start_pos, 8); - cio_write(tile->marker_mul.PPT[i].len, 2); - } - - for(i = tile->marker_mul.num_COM - 1; i >= 0; i--) /* COM */ - { - cio_write(tile->marker_mul.COM[i].type, 2); - cio_write(i, 2); - cio_write(tile->marker_mul.COM[i].start_pos, 8); - cio_write(tile->marker_mul.COM[i].len, 2); - } - } - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len, 4); /* L */ - cio_seek(lenp+len); - - return len; -} - -/* - * Read the MHIX box - * - * Main Header Index Table (box) - * - */ -void jpip_read_mhix(int len) -{ - int i, v, marker_type, marker_start_pos, marker_len, marker_remains; - - v = (len - 8) / 14; - - for (i=0; icompo[compno]; - int correction; - - num_packet=0; - - if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH) - correction=3; - else - correction=1; - for(resno=0;resnoreso[resno]; - for (precno=0;precnoprec[precno]; - for(layno=0;laynolayer[layno]; - cio_write(layer_Idx->offset,(version & 0x01)?8:4); /* start position */ - cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len,(version & 0x01)?8:4); /* length */ - if (version & 0x02) - cio_write(0,4); /* Aux_i,j : Auxiliary value */ - num_packet++; - } - } - } - /* PADDING */ - while (num_packet < img.num_packet_max) - { - cio_write(0,(version & 0x01)?8:4); /* start position */ - cio_write(0,(version & 0x01)?8:4); /* length */ - if (version & 0x02) - cio_write(0,4); /* Aux_i,j : Auxiliary value */ - num_packet++; - } - } - - break; - - case 3: /* PHIX NOT FINISHED !! */ - cio_write(img.num_packet_max,(version & 0x01)?8:4); /* NMAX */ - cio_write(img.tw*img.th,(version & 0x01)?8:4); /* M */ - for(tileno=0;tilenocompo[compno]; - int correction; - - num_packet = 0; - if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH) - correction=3; - else - correction=1; - for(resno=0;resnoreso[resno]; - for (precno=0;precnoprec[precno]; - for(layno=0;laynolayer[layno]; - cio_write(layer_Idx->offset_header,(version & 0x01)?8:4); /* start position */ - cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len_header,(version & 0x01)?8:4); /* length */ - if (version & 0x02) - cio_write(0,4); /* Aux_i,j : Auxiliary value */ - num_packet++; - } - } - } - /* PADDING */ - while (num_packettw*j2k_cp->th); - - for ( i = 0; i < 2 ; i++ ) - { - if (i) cio_seek(lenp); - - lenp = cio_tell(); - cio_skip(4); /* L [at the end] */ - cio_write(JPIP_THIX, 4); /* THIX */ - jpip_write_manf(i, j2k_cp->tw*j2k_cp->th, marker); - num_marker_local_jpip=img.Comp; - - for (tileno = 0; tileno < j2k_cp->tw*j2k_cp->th; tileno++) - { - marker[tileno].len = jpip_write_mhix(img, 1, tileno); - marker[tileno].type = JPIP_MHIX; - } - - len=cio_tell()-lenp; - cio_seek(lenp); - cio_write(len, 4); /* L */ - cio_seek(lenp+len); - } - - free(marker); - - return len; -} -/* - * Write the PPIX box - * - * Precinct Packet Index table box (superbox) - * - */ -int jpip_write_ppix(info_image_t img,j2k_cp_t *j2k_cp) -{ - int len, lenp, compno, i; - info_marker_t *marker; - int num_marker_local_jpip; - marker = (info_marker_t*)calloc(sizeof(info_marker_t), img.Comp); - - for (i=0;i<2;i++) - { - if (i) cio_seek(lenp); - - lenp=cio_tell(); - cio_skip(4); /* L [at the end] */ - cio_write(JPIP_PPIX, 4); /* PPIX */ - jpip_write_manf(i,img.Comp,marker); - num_marker_local_jpip=img.Comp; - - for (compno=0; compno -#include - - -/* */ -/* Create a packet iterator. */ -/* */ -pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno) -{ - int p, q; - int compno, resno, pino; - int maxres = 0; - pi_iterator_t *pi; - j2k_tcp_t *tcp; - j2k_tccp_t *tccp; - - tcp = &cp->tcps[tileno]; - pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) * sizeof(pi_iterator_t)); - - for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ - p = tileno % cp->tw; - q = tileno / cp->tw; - - pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0); - pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0); - pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1); - pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1); - pi[pino].numcomps = img->numcomps; - pi[pino].comps = (pi_comp_t *) malloc(img->numcomps * sizeof(pi_comp_t)); - - for (compno = 0; compno < pi->numcomps; compno++) { - int tcx0, tcy0, tcx1, tcy1; - pi_comp_t *comp = &pi[pino].comps[compno]; - tccp = &tcp->tccps[compno]; - comp->dx = img->comps[compno].dx; - comp->dy = img->comps[compno].dy; - comp->numresolutions = tccp->numresolutions; - comp->resolutions = - (pi_resolution_t *) malloc(comp->numresolutions * - sizeof(pi_resolution_t)); - tcx0 = int_ceildiv(pi->tx0, comp->dx); - tcy0 = int_ceildiv(pi->ty0, comp->dy); - tcx1 = int_ceildiv(pi->tx1, comp->dx); - tcy1 = int_ceildiv(pi->ty1, comp->dy); - if (comp->numresolutions > maxres) { - maxres = comp->numresolutions; - } - for (resno = 0; resno < comp->numresolutions; resno++) { - int levelno; - int rx0, ry0, rx1, ry1; - int px0, py0, px1, py1; - pi_resolution_t *res = &comp->resolutions[resno]; - if (tccp->csty & J2K_CCP_CSTY_PRT) { - res->pdx = tccp->prcw[resno]; - res->pdy = tccp->prch[resno]; - } else { - res->pdx = 15; - res->pdy = 15; - } - levelno = comp->numresolutions - 1 - resno; - rx0 = int_ceildivpow2(tcx0, levelno); - ry0 = int_ceildivpow2(tcy0, levelno); - rx1 = int_ceildivpow2(tcx1, levelno); - ry1 = int_ceildivpow2(tcy1, levelno); - px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; - py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; - px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; - py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; - res->pw = (px1 - px0) >> res->pdx; - res->ph = (py1 - py0) >> res->pdy; - } - } - - tccp = &tcp->tccps[0]; - pi[pino].step_p=1; - pi[pino].step_c=100*pi[pino].step_p; - pi[pino].step_r=img->numcomps*pi[pino].step_c; - pi[pino].step_l=maxres*pi[pino].step_r; - - if (pino==0) - pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*100,sizeof(short int)); - else - pi[pino].include=pi[pino-1].include; - - /*if (pino == tcp->numpocs) {*/ - if (tcp->POC == 0) { - pi[pino].first = 1; - pi[pino].poc.resno0 = 0; - pi[pino].poc.compno0 = 0; - pi[pino].poc.layno1 = tcp->numlayers; - pi[pino].poc.resno1 = maxres; - pi[pino].poc.compno1 = img->numcomps; - pi[pino].poc.prg = tcp->prg; - } else { - pi[pino].first = 1; - pi[pino].poc.resno0 = tcp->pocs[pino].resno0; - pi[pino].poc.compno0 = tcp->pocs[pino].compno0; - pi[pino].poc.layno1 = tcp->pocs[pino].layno1; - pi[pino].poc.resno1 = tcp->pocs[pino].resno1; - pi[pino].poc.compno1 = tcp->pocs[pino].compno1; - pi[pino].poc.prg = tcp->pocs[pino].prg; - } - } - return pi; -} - -/* */ -/* Get next packet in layer=resolution-component-precinct order. */ -/* */ -int pi_next_lrcp(pi_iterator_t * pi) -{ - pi_comp_t *comp; - pi_resolution_t *res; - - if (!pi->first) { - comp = &pi->comps[pi->compno]; - res = &comp->resolutions[pi->resno]; - goto skip; - } else { - pi->first = 0; - } - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; - pi->resno++) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolutions) { - - continue; - } - res = &comp->resolutions[pi->resno]; - for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) { - if (!pi->include[pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p]){ - pi->include[pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p] = 1; - return 1; - } - skip:; - } - } - } - } - return 0; -} - -/* */ -/* Get next packet in resolution-layer-component-precinct order. */ -/* */ -int pi_next_rlcp(pi_iterator_t * pi) -{ - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - comp = &pi->comps[pi->compno]; - res = &comp->resolutions[pi->resno]; - goto skip; - } else { - pi->first = 0; - } - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolutions) { - continue; - } - res = &comp->resolutions[pi->resno]; - for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) { - if (!pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){ - pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1; - return 1; - } - skip:; - } - } - } - } - return 0; -} - -/* */ -/* Get next packet in resolution-precinct-component-layer order. */ -/* */ -int pi_next_rpcl(pi_iterator_t * pi) -{ - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - goto skip; - } else { - int compno, resno; - pi->first = 0; - pi->dx = 0; - pi->dy = 0; - for (compno = 0; compno < pi->numcomps; compno++) { - comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = - comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = - comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - } - } - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->y = pi->ty0; pi->y < pi->ty1; - pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; - pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - int levelno; - int trx0, try0; - int rpx, rpy; - int prci, prcj; - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolutions) { - continue; - } - res = &comp->resolutions[pi->resno]; - levelno = comp->numresolutions - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); - try0 = int_ceildiv(pi->ty0, comp->dy << levelno); - rpx = res->pdx + levelno; - rpy = res->pdy + levelno; - if (! - (pi->x % (comp->dx << rpx) == 0 - || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; - } - if (! - (pi->y % (comp->dy << rpy) == 0 - || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; - } - prci = - int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), - res->pdx) - int_floordivpow2(trx0, res->pdx); - prcj = - int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), - res->pdy) - int_floordivpow2(try0, res->pdy); - pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - if (!pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){ - pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1; - return 1; - } - skip:; - } - } - } - } - } - return 0; -} - -/* */ -/* Get next packet in precinct-component-resolution-layer order. */ -/* */ -int pi_next_pcrl(pi_iterator_t * pi) -{ - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - comp = &pi->comps[pi->compno]; - goto skip; - } else { - int compno, resno; - pi->first = 0; - pi->dx = 0; - pi->dy = 0; - for (compno = 0; compno < pi->numcomps; compno++) { - comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = - comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = - comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - } - } - for (pi->y = pi->ty0; pi->y < pi->ty1; - pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; - pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - comp = &pi->comps[pi->compno]; - for (pi->resno = pi->poc.resno0; - pi->resno < int_min(pi->poc.resno1, comp->numresolutions); - pi->resno++) { - int levelno; - int trx0, try0; - int rpx, rpy; - int prci, prcj; - res = &comp->resolutions[pi->resno]; - levelno = comp->numresolutions - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); - try0 = int_ceildiv(pi->ty0, comp->dy << levelno); - rpx = res->pdx + levelno; - rpy = res->pdy + levelno; - if (! - (pi->x % (comp->dx << rpx) == 0 - || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; - } - if (! - (pi->y % (comp->dy << rpy) == 0 - || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; - } - prci = - int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), - res->pdx) - int_floordivpow2(trx0, res->pdx); - prcj = - int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), - res->pdy) - int_floordivpow2(try0, res->pdy); - pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - if (! pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){ - pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1; - return 1; - } - skip:; - } - } - } - } - } - return 0; -} - -/* */ -/* Get next packet in component-precinct-resolution-layer order. */ -/* */ -int pi_next_cprl(pi_iterator_t * pi) -{ - pi_comp_t *comp; - pi_resolution_t *res; - if (!pi->first) { - comp = &pi->comps[pi->compno]; - goto skip; - } else { - pi->first = 0; - } - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; - pi->compno++) { - int resno; - comp = &pi->comps[pi->compno]; - pi->dx = 0; - pi->dy = 0; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - for (pi->y = pi->ty0; pi->y < pi->ty1; - pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; - pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->resno = pi->poc.resno0; - pi->resno < int_min(pi->poc.resno1, comp->numresolutions); - pi->resno++) { - int levelno; - int trx0, try0; - int rpx, rpy; - int prci, prcj; - res = &comp->resolutions[pi->resno]; - levelno = comp->numresolutions - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); - try0 = int_ceildiv(pi->ty0, comp->dy << levelno); - rpx = res->pdx + levelno; - rpy = res->pdy + levelno; - if (! - (pi->x % (comp->dx << rpx) == 0 - || (pi->x == pi->tx0 && (trx0 << levelno) % (1 << rpx)))) { - continue; - } - if (! - (pi->y % (comp->dy << rpy) == 0 - || (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) { - continue; - } - prci = - int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), - res->pdx) - int_floordivpow2(trx0, res->pdx); - prcj = - int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), - res->pdy) - int_floordivpow2(try0, res->pdy); - pi->precno = prci + prcj * res->pw; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - if (! pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p]){ - pi->include[pi->layno*pi->step_l+pi->resno*pi->step_r+pi->compno*pi->step_c+pi->precno*pi->step_p] = 1; - return 1; - } - skip:; - } - } - } - } - } - return 0; -} - -/* */ -/* Get next packet. */ -/* */ -int pi_next(pi_iterator_t * pi) -{ - switch (pi->poc.prg) { - case 0: - return pi_next_lrcp(pi); - case 1: - return pi_next_rlcp(pi); - case 2: - return pi_next_rpcl(pi); - case 3: - return pi_next_pcrl(pi); - case 4: - return pi_next_cprl(pi); - } - return 0; -} diff --git a/indexer_JPIP/pi.h b/indexer_JPIP/pi.h deleted file mode 100644 index b300b9e3..00000000 --- a/indexer_JPIP/pi.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __PI_H -#define __PI_H - -#include "j2k.h" -#include "tcd.h" - -typedef struct { - int pdx, pdy; - int pw, ph; -} pi_resolution_t; - -typedef struct { - int dx, dy; - int numresolutions; - pi_resolution_t *resolutions; -} pi_comp_t; - -typedef struct { - short int *include; - int step_l, step_r, step_c, step_p; - int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */ - int first; - j2k_poc_t poc; - int numcomps; - pi_comp_t *comps; - int tx0, ty0, tx1, ty1; - int x, y, dx, dy; -} pi_iterator_t; /* packet iterator */ - -/* - * Create a packet iterator - * img: raw image for which the packets will be listed - * cp: coding paremeters - * tileno: number that identifies the tile for which to list the packets - * return value: returns a packet iterator that points to the first packet of the tile - */ -pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno); - -/* - * Modify the packet iterator to point to the next packet - * pi: packet iterator to modify - * return value: returns 0 if pi pointed to the last packet or else returns 1 - */ -int pi_next(pi_iterator_t * pi); - -#endif diff --git a/indexer_JPIP/t2.c b/indexer_JPIP/t2.c deleted file mode 100644 index 14a44bea..00000000 --- a/indexer_JPIP/t2.c +++ /dev/null @@ -1,389 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "t2.h" -#include "tcd.h" -#include "bio.h" -#include "j2k.h" -#include "pi.h" -#include "tgt.h" -#include "int.h" -#include "cio.h" -#include -#include -#include -#include - -#define RESTART 0x04 - -extern jmp_buf j2k_error; - -int t2_getcommacode() { - int n; - for (n=0; bio_read(1); n++) {} - return n; -} - -int t2_getnumpasses() -{ - int n; - if (!bio_read(1)) return 1; - if (!bio_read(1)) return 2; - if ((n=bio_read(2))!=3) return 3+n; - if ((n=bio_read(5))!=31) return 6+n; - return 37+bio_read(7); -} - -void t2_init_seg(tcd_seg_t *seg, int cblksty) { - seg->numpasses=0; - seg->len=0; - seg->maxpasses=cblksty&J2K_CCP_CBLKSTY_TERMALL?1:100; -} - -int t2_decode_packet(unsigned char *src, int len, tcd_tile_t *tile, j2k_cp_t * cp, j2k_tcp_t *tcp, int compno, int resno, int precno, int layno, info_layer_t *layer_Idx) { - int bandno, cblkno; - tcd_tilecomp_t *tilec = &tile->comps[compno]; - tcd_resolution_t *res = &tilec->resolutions[resno]; - unsigned char *c = src; - unsigned char *d = c; - int e; - int present; - - if (layno == 0) { - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - cblk->numsegs = 0; - } - } - } - /* INDEX */ - layer_Idx->len_header = 0; - - /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker - This part deal with this caracteristic - step 1: Read packet header in the saved structure - step 2: (futher) return to codestream for decoding */ - if (cp->ppm == 1) /* PPM */ - { - c = cp->ppm_data; - d = c; - bio_init_dec(c, 1000); - } else - { - if (tcp->ppt == 1) /* PPT */ - { - c = tcp->ppt_data; - d = c; - bio_init_dec(c, 1000); - } else /* Normal Case */ - { - if (tcp->csty & J2K_CP_CSTY_SOP) - { - if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [1]!!!\n");} - c += 6; - } - bio_init_dec(c, src + len - c); - layer_Idx->len_header = -6; - } - } - - present = bio_read(1); - - if (!present) - { - bio_inalign(); - /* Normal case */ - c += bio_numbytes(); - if (tcp->csty & J2K_CP_CSTY_EPH) - { - if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [1]!!!\n");} - c += 2; - } - /* INDEX */ - layer_Idx->len_header += (c-d); - - /* PPT and PPM dealing */ - if (cp->ppm == 1) /* PPM */ - { - cp->ppm_data = c; - return 0; - } - if (tcp->ppt == 1) /* PPT */ - { - tcp->ppt_data = c; - return 0; - } - return c - src; - } - - for (bandno=0; bandnonumbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int included, increment, n; - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_seg_t *seg; - if (!cblk->numsegs) { - included = tgt_decode(prc->incltree, cblkno, layno+1); - } else { - included = bio_read(1); - } - if (!included) { - cblk->numnewpasses = 0; - continue; - } - if (!cblk->numsegs) { - int i, numimsbs; - for (i = 0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {} - numimsbs = i-1; - cblk->numbps = band->numbps - numimsbs; - cblk->numlenbits = 3; - } - cblk->numnewpasses = t2_getnumpasses(); - increment = t2_getcommacode(); - cblk->numlenbits += increment; - if (!cblk->numsegs) { - seg = &cblk->segs[0]; - t2_init_seg(seg, tcp->tccps[compno].cblksty); - } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - t2_init_seg(++seg, tcp->tccps[compno].cblksty); - } - } - n = cblk->numnewpasses; - do { - seg->numnewpasses = int_min(seg->maxpasses-seg->numpasses, n); - seg->newlen = bio_read(cblk->numlenbits + int_floorlog2(seg->numnewpasses)); - n -= seg->numnewpasses; - if (n > 0) { - t2_init_seg(++seg, tcp->tccps[compno].cblksty); - } - } while (n > 0); - } - } - if(bio_inalign()) return -999; - c += bio_numbytes(); - - if (tcp->csty & J2K_CP_CSTY_EPH) { /* EPH marker */ - if ((*c) != 255 || (*(c+1) != 146)) {printf("Error : expected EPH marker [2]!!!\n"); } - c += 2; - } - - /* INDEX */ - layer_Idx->len_header += (c-d); - - /* PPT Step 2 : see above for details */ - if (cp->ppm == 1) - { - cp->ppm_data = c; /* Update pointer */ - - /* INDEX */ - layer_Idx->len_header = c-d; - - c = src; - d = c; - if (tcp->csty & J2K_CP_CSTY_SOP) - { - if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); } - c += 6; - } - bio_init_dec(c, src + len - c); - } else - { - if (tcp->ppt == 1) - { - tcp->ppt_data = c; /* Update pointer */ - /* INDEX */ - layer_Idx->len_header = c-d; - - c = src; - d = c; - if (tcp->csty & J2K_CP_CSTY_SOP) /* SOP marker */ - { - if ((*c) != 255 || (*(c+1) != 145)) {printf("Error : expected SOP marker [2] !!!\n"); } - c += 6; - } - bio_init_dec(c, src + len - c); - - } - } - - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd_band_t *band = &res->bands[bandno]; - tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw*prc->ch; cblkno++) { - tcd_cblk_t *cblk = &prc->cblks[cblkno]; - tcd_seg_t *seg; - if (!cblk->numnewpasses) continue; - if (!cblk->numsegs) { - seg = &cblk->segs[cblk->numsegs++]; - cblk->len = 0; - } else { - seg = &cblk->segs[cblk->numsegs-1]; - if (seg->numpasses == seg->maxpasses) { - seg++; - cblk->numsegs++; - } - } - do { - if (c + seg->newlen > src + len) return -999; - memcpy(cblk->data + cblk->len, c, seg->newlen); - if (seg->numpasses == 0) { - seg->data = cblk->data + cblk->len; - } - c += seg->newlen; - cblk->len += seg->newlen; - seg->len += seg->newlen; - seg->numpasses += seg->numnewpasses; - cblk->numnewpasses -= seg->numnewpasses; - if (cblk->numnewpasses > 0) { - seg++; - cblk->numsegs++; - } - } while (cblk->numnewpasses > 0); - } - } - /* */ - e = c-d; - layer_Idx->len = e; - /* */ - - return c-src; -} - -void t2_init_info_packets(info_image_t *img, j2k_cp_t *cp) -{ - int compno, tileno, resno, precno, layno; - - for(compno = 0; compno < img->Comp; compno++) - { - for(tileno = 0; tileno < img->tw*img->th; tileno++) - { - info_tile_t *tile_Idx = &img->tile[tileno]; - info_compo_t *compo_Idx = &tile_Idx->compo[compno]; - for(resno = 0; resno < img->Decomposition + 1 ; resno++) - { - info_reso_t *reso_Idx = &compo_Idx->reso[resno]; - for (precno = 0; precno < img->tile[tileno].pw * img->tile[tileno].ph; precno++) - { - info_prec_t *prec_Idx = &reso_Idx->prec[precno]; - for(layno = 0; layno < img->Layer ; layno++) - { - info_layer_t *layer_Idx = &prec_Idx->layer[layno]; - layer_Idx->offset = 0; /* start position */ - layer_Idx->len_header = 0; /* length */ - } - } - } - } - } -} - -int t2_decode_packets(unsigned char *src, int len, j2k_image_t *img, j2k_cp_t *cp, int tileno, tcd_tile_t *tile, info_image_t *imgg) { - unsigned char *c = src; - pi_iterator_t *pi; - int pino, compno,e; - int partno; - info_tile_part_t *tile_part; - int position; - int length_read; - info_tile_t *tile_Idx; - info_compo_t *compo_Idx; - info_reso_t *reso_Idx; - info_prec_t *prec_Idx; - info_layer_t *layer_Idx; - - t2_init_info_packets(imgg, cp); /* Initialize the packets information : LEN and OFFSET to 0 */ - - tile_Idx = &imgg->tile[tileno]; - tile_Idx->num_packet = 0; - pi = pi_create(img, cp, tileno); - partno = 0; - tile_part = &tile_Idx->tile_parts[partno]; - position = tile_part->end_header + 1; - length_read = 0; - - for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) - { - while (pi_next(&pi[pino])) - { - compo_Idx = &tile_Idx->compo[pi[pino].compno]; - reso_Idx = &compo_Idx->reso[pi[pino].resno]; - prec_Idx = &reso_Idx->prec[pi[pino].precno]; - layer_Idx = &prec_Idx->layer[pi[pino].layno]; - - layer_Idx->offset = position; - layer_Idx->offset_header = position; - - e = t2_decode_packet(c, src+len-c, tile, cp, &cp->tcps[tileno], pi[pino].compno, pi[pino].resno, pi[pino].precno, pi[pino].layno,layer_Idx); - if (e == -999) - { - break; - } else - c += e; - position += e; - - /* Update position in case of multiple tile-parts for a tile >> */ - length_read += e; - if (length_read >= (tile_part->end_pos - tile_part->end_header)) - { - partno++; - tile_part = &tile_Idx->tile_parts[partno]; - position = tile_part->end_header + 1; - length_read = 0; - } - /* << end_update */ - - tile_Idx->num_packet++; - } - - // FREE space memory taken by pi - for (compno = 0; compno < pi[pino].numcomps; compno++) - { - free(pi[pino].comps[compno].resolutions); - } - free(pi[pino].comps); - } - - free(pi[0].include); - free(pi); - - if (e==-999) - return e; - else - { - imgg->num_packet_max=int_max(imgg->num_packet_max,tile_Idx->num_packet); - return c-src; - } -} diff --git a/indexer_JPIP/t2.h b/indexer_JPIP/t2.h deleted file mode 100644 index f4951079..00000000 --- a/indexer_JPIP/t2.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __T2_H -#define __T2_H - -#include "tcd.h" -#include "j2k.h" - -/* - * Decode the packets of a tile from a source buffer - * src: the source buffer - * len: length of the source buffer - * img: destination image - * cp: image coding parameters - * tileno: number that identifies the tile for which to decode the packets - * tile: tile for which to decode the packets - */ -int t2_decode_packets(unsigned char *src, int len, j2k_image_t *img, j2k_cp_t *cp, int tileno, tcd_tile_t *tile, info_image_t *imgg); - -#endif diff --git a/indexer_JPIP/tcd.c b/indexer_JPIP/tcd.c deleted file mode 100644 index c4045c73..00000000 --- a/indexer_JPIP/tcd.c +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "tcd.h" -#include "int.h" -#include "t2.h" -#include -#include -#include -#include -#include -#include -#include - -static tcd_image_t tcd_image; - -static j2k_image_t *tcd_img; -static j2k_cp_t *tcd_cp; - -extern jmp_buf j2k_error; - -void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg) { - int tileno, compno, resno, bandno, precno, cblkno; - tcd_img=img; - tcd_cp=cp; - tcd_image.tw=cp->tw; - tcd_image.th=cp->th; - tcd_image.tiles=(tcd_tile_t*)malloc(cp->tw*cp->th*sizeof(tcd_tile_t)); - for (tileno=0; tilenotw*cp->th; tileno++) { - j2k_tcp_t *tcp=&cp->tcps[tileno]; - tcd_tile_t *tile=&tcd_image.tiles[tileno]; - // cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) - int p=tileno%cp->tw; // si numerotation matricielle .. - int q=tileno/cp->tw; // .. coordonnees de la tile (q,p) q pour ligne et p pour colonne - info_tile_t *tile_Idx=&imgg->tile[tileno]; // INDEX - - // 4 borders of the tile rescale on the image if necessary - tile->x0=int_max(cp->tx0+p*cp->tdx, img->x0); - tile->y0=int_max(cp->ty0+q*cp->tdy, img->y0); - tile->x1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1); - tile->y1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1); - - tile->numcomps=img->numcomps; - tile->comps=(tcd_tilecomp_t*)malloc(img->numcomps*sizeof(tcd_tilecomp_t)); - tile_Idx->compo=(info_compo_t*)malloc(img->numcomps*sizeof(info_compo_t)); // INDEX - for (compno=0; compnonumcomps; compno++) { - j2k_tccp_t *tccp=&tcp->tccps[compno]; - tcd_tilecomp_t *tilec=&tile->comps[compno]; - info_compo_t *compo_Idx=&tile_Idx->compo[compno]; // INDEX - - // border of each tile component (global) - tilec->x0=int_ceildiv(tile->x0, img->comps[compno].dx); - tilec->y0=int_ceildiv(tile->y0, img->comps[compno].dy); - tilec->x1=int_ceildiv(tile->x1, img->comps[compno].dx); - tilec->y1=int_ceildiv(tile->y1, img->comps[compno].dy); - - tilec->data=(int*)malloc(sizeof(int)*(tilec->x1-tilec->x0)*(tilec->y1-tilec->y0)); - tilec->numresolutions=tccp->numresolutions; - tilec->resolutions=(tcd_resolution_t*)malloc(tilec->numresolutions*sizeof(tcd_resolution_t)); - compo_Idx->reso=(info_reso_t*)malloc(tilec->numresolutions*sizeof(info_reso_t)); // INDEX - for (resno=0; resnonumresolutions; resno++) { - int pdx, pdy; - int levelno=tilec->numresolutions-1-resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - tcd_resolution_t *res=&tilec->resolutions[resno]; - info_reso_t *res_Idx=&compo_Idx->reso[resno]; // INDEX - int precno_Idx; // INDEX - - // border for each resolution level (global) - res->x0=int_ceildivpow2(tilec->x0, levelno); - res->y0=int_ceildivpow2(tilec->y0, levelno); - res->x1=int_ceildivpow2(tilec->x1, levelno); - res->y1=int_ceildivpow2(tilec->y1, levelno); - - res->numbands=resno==0?1:3; - // p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) - if (tccp->csty&J2K_CCP_CSTY_PRT) { - pdx=tccp->prcw[resno]; - pdy=tccp->prch[resno]; - } else { - pdx=15; - pdy=15; - } - // p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) - tlprcxstart=int_floordivpow2(res->x0, pdx)<y0, pdy)<x1, pdx)<y1, pdy)<pw=(brprcxend-tlprcxstart)>>pdx; - res->ph=(brprcyend-tlprcystart)>>pdy; - - // - imgg->tile[tileno].pw=res->pw; - imgg->tile[tileno].ph=res->ph; - - res_Idx->prec=(info_prec_t*)malloc(res->pw*res->ph*sizeof(info_prec_t)); - for (precno_Idx=0;precno_Idxpw*res->ph;precno_Idx++) - { - info_prec_t *prec_Idx = &res_Idx->prec[precno_Idx]; - prec_Idx->layer=(info_layer_t*)malloc(imgg->Layer*sizeof(info_layer_t)); - } - - imgg->pw=res->pw; // old parser version - imgg->ph=res->ph; // old parser version - imgg->pdx=1<pdy=1< - - if (resno==0) { - tlcbgxstart=tlprcxstart; - tlcbgystart=tlprcystart; - brcbgxend=brprcxend; - brcbgyend=brprcyend; - cbgwidthexpn=pdx; - cbgheightexpn=pdy; - } else { - tlcbgxstart=int_ceildivpow2(tlprcxstart, 1); - tlcbgystart=int_ceildivpow2(tlprcystart, 1); - brcbgxend=int_ceildivpow2(brprcxend, 1); - brcbgyend=int_ceildivpow2(brprcyend, 1); - cbgwidthexpn=pdx-1; - cbgheightexpn=pdy-1; - } - - cblkwidthexpn=int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn=int_min(tccp->cblkh, cbgheightexpn); - - for (bandno=0; bandnonumbands; bandno++) { - int x0b, y0b; - tcd_band_t *band=&res->bands[bandno]; - band->bandno=resno==0?0:bandno+1; - x0b=(band->bandno==1)||(band->bandno==3)?1:0; - y0b=(band->bandno==2)||(band->bandno==3)?1:0; - - if (band->bandno==0) { - // band border (global) - band->x0=int_ceildivpow2(tilec->x0, levelno); - band->y0=int_ceildivpow2(tilec->y0, levelno); - band->x1=int_ceildivpow2(tilec->x1, levelno); - band->y1=int_ceildivpow2(tilec->y1, levelno); - } else { - // band border (global) - band->x0=int_ceildivpow2(tilec->x0-(1<y0=int_ceildivpow2(tilec->y0-(1<x1=int_ceildivpow2(tilec->x1-(1<y1=int_ceildivpow2(tilec->y1-(1<precincts=(tcd_precinct_t*)malloc(res->pw*res->ph*sizeof(tcd_precinct_t)); - - for (precno=0; precnopw*res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - int cbgxstart=tlcbgxstart+(precno%res->pw)*(1<pw)*(1<precincts[precno]; - // precinct size (global) - prc->x0=int_max(cbgxstart, band->x0); - prc->y0=int_max(cbgystart, band->y0); - prc->x1=int_min(cbgxend, band->x1); - prc->y1=int_min(cbgyend, band->y1); - - tlcblkxstart=int_floordivpow2(prc->x0, cblkwidthexpn)<y0, cblkheightexpn)<x1, cblkwidthexpn)<y1, cblkheightexpn)<cw=(brcblkxend-tlcblkxstart)>>cblkwidthexpn; - prc->ch=(brcblkyend-tlcblkystart)>>cblkheightexpn; - - prc->cblks=(tcd_cblk_t*)malloc(prc->cw*prc->ch*sizeof(tcd_cblk_t)); - - prc->incltree=tgt_create(prc->cw, prc->ch); - prc->imsbtree=tgt_create(prc->cw, prc->ch); - - for (cblkno=0; cblknocw*prc->ch; cblkno++) { - int cblkxstart=tlcblkxstart+(cblkno%prc->cw)*(1<cw)*(1<cblks[cblkno]; - // code-block size (global) - cblk->x0=int_max(cblkxstart, prc->x0); - cblk->y0=int_max(cblkystart, prc->y0); - cblk->x1=int_min(cblkxend, prc->x1); - cblk->y1=int_min(cblkyend, prc->y1); - } - } - } - } - } - } -} - - -void tcd_free(j2k_image_t *img, j2k_cp_t *cp) { - int tileno, compno, resno, bandno, precno; - tcd_img=img; - tcd_cp=cp; - tcd_image.tw=cp->tw; - tcd_image.th=cp->th; - for (tileno=0; tilenotcps[curtileno]; - tcd_tile_t *tile=&tcd_image.tiles[tileno]; - for (compno=0; compnonumcomps; compno++) - { - tcd_tilecomp_t *tilec=&tile->comps[compno]; - for (resno=0; resnonumresolutions; resno++) - { - tcd_resolution_t *res=&tilec->resolutions[resno]; - for (bandno=0; bandnonumbands; bandno++) - { - tcd_band_t *band=&res->bands[bandno]; - for (precno=0; precnopw*res->ph; precno++) - { - tcd_precinct_t *prc=&band->precincts[precno]; - - if (prc->incltree!=NULL) - tgt_destroy(prc->incltree); - if (prc->imsbtree!=NULL) - tgt_destroy(prc->imsbtree); - free(prc->cblks); - } // for (precno - free(band->precincts); - } // for (bandno - } // for (resno - free(tilec->resolutions); - } // for (compno - free(tile->comps); - } // for (tileno - free(tcd_image.tiles); -} - - -int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg) { - int l; - int eof=0; - tcd_tile_t *tile; - - tile = &tcd_image.tiles[tileno]; - - l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile, imgg); - - if (l==-999) - { - eof=1; - fprintf(stderr, "tcd_decode: incomplete bistream\n"); - } - - if (eof) { - longjmp(j2k_error, 1); - } - - l=1; - return l; -} diff --git a/indexer_JPIP/tcd.h b/indexer_JPIP/tcd.h deleted file mode 100644 index 9a31723b..00000000 --- a/indexer_JPIP/tcd.h +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __TCD_H -#define __TCD_H - -#include "j2k.h" -#include "tgt.h" - -typedef struct { - int numpasses; - int len; - unsigned char *data; - int maxpasses; - int numnewpasses; - int newlen; -} tcd_seg_t; - -typedef struct { - int rate; - double distortiondec; -} tcd_pass_t; - -typedef struct { - int numpasses; - int len; - unsigned char *data; -} tcd_layer_t; - -typedef struct { - int x0, y0, x1, y1; - int numbps; - int numlenbits; - int len; - int numpasses; - int numnewpasses; - int numsegs; - tcd_seg_t segs[100]; - unsigned char data[8192]; - int numpassesinlayers; - tcd_layer_t layers[100]; - int totalpasses; - tcd_pass_t passes[100]; -} tcd_cblk_t; - -typedef struct { - int x0, y0, x1, y1; - int cw, ch; - tcd_cblk_t *cblks; - tgt_tree_t *incltree; - tgt_tree_t *imsbtree; -} tcd_precinct_t; - -typedef struct { - int x0, y0, x1, y1; - int bandno; - tcd_precinct_t *precincts; - int numbps; - int stepsize; -} tcd_band_t; - -typedef struct { - int x0, y0, x1, y1; - int previous_x0, previous_y0, previous_x1, previous_y1; // usefull for the DWT - int cas_col, cas_row; // usefull for the DWT - int pw, ph; - int numbands; - tcd_band_t bands[3]; -} tcd_resolution_t; - -typedef struct { - int x0, y0, x1, y1; - int previous_row, previous_col; // usefull for the DWT - int numresolutions; - tcd_resolution_t *resolutions; - int *data; -} tcd_tilecomp_t; - -typedef struct { - int x0, y0, x1, y1; - int numcomps; - //int PPT; - //int len_ppt; - tcd_tilecomp_t *comps; -} tcd_tile_t; - -typedef struct { - int tw, th; - tcd_tile_t *tiles; -} tcd_image_t; - -/* - * Initialize the tile coder/decoder - * img: raw image - * cp: coding parameters - * imgg: creation of index file - */ - -void tcd_init(j2k_image_t *img, j2k_cp_t *cp, info_image_t *imgg); - -void tcd_free(j2k_image_t *img, j2k_cp_t *cp); - -/* - * Decode a tile from a buffer into a raw image - * src: source buffer - * len: length of the source buffer - * tileno: number that identifies the tile that will be decoded - * imgg : Structure for index file - */ -int tcd_decode_tile(unsigned char *src, int len, int tileno, info_image_t *imgg); - -#endif diff --git a/indexer_JPIP/tgt.c b/indexer_JPIP/tgt.c deleted file mode 100644 index c2b46824..00000000 --- a/indexer_JPIP/tgt.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "tgt.h" -#include "bio.h" -#include -#include - -/// -/// Reset tag-tree. -/// -void tgt_reset(tgt_tree_t *tree) -{ - int i; - for (i=0; inumnodes; i++) { - tree->nodes[i].value=999; - tree->nodes[i].low=0; - tree->nodes[i].known=0; - } -} - -/// -/// Create tag-tree. -/// -tgt_tree_t *tgt_create(int numleafsh, int numleafsv) -{ - int nplh[32]; - int nplv[32]; - tgt_node_t *node; - tgt_node_t *parentnode; - tgt_node_t *parentnode0; - tgt_tree_t *tree; - int i, j, k; - int numlvls; - int n; - - tree=(tgt_tree_t*)malloc(sizeof(tgt_tree_t)); - tree->numleafsh=numleafsh; - tree->numleafsv=numleafsv; - - numlvls=0; - nplh[0]=numleafsh; - nplv[0]=numleafsv; - tree->numnodes=0; - do { - n=nplh[numlvls]*nplv[numlvls]; - nplh[numlvls+1]=(nplh[numlvls]+1)/2; - nplv[numlvls+1]=(nplv[numlvls]+1)/2; - tree->numnodes+=n; - ++numlvls; - } while (n>1); - - tree->nodes=(tgt_node_t*)malloc(tree->numnodes*sizeof(tgt_node_t)); - - node=tree->nodes; - parentnode=&tree->nodes[tree->numleafsh*tree->numleafsv]; - parentnode0=parentnode; - - for (i=0; i=0) { - node->parent=parentnode; - ++node; - if (--k >= 0) { - node->parent=parentnode; - ++node; - } - ++parentnode; - } - if ((j&1)||j==nplv[i]-1) { - parentnode0=parentnode; - } else { - parentnode=parentnode0; - parentnode0+=nplh[i]; - } - } - } - node->parent=0; - - tgt_reset(tree); - - return tree; -} - -/// -/// Destroy tag-tree. -/// -void tgt_destroy(tgt_tree_t *t) { - free(t->nodes); - free(t); -} - -/// -/// Set the value of a leaf of the tag-tree. -/// -void tgt_setvalue(tgt_tree_t *tree, int leafno, int value) { - tgt_node_t *node; - node=&tree->nodes[leafno]; - while (node && node->value>value) { - node->value=value; - node=node->parent; - } -} - -/// -/// Decode the value of a leaf of the tag-tree. -/// -int tgt_decode(tgt_tree_t *tree, int leafno, int threshold) -{ - tgt_node_t *stk[31]; - tgt_node_t **stkptr; - tgt_node_t *node; - int low; - - stkptr=stk; - node=&tree->nodes[leafno]; - while (node->parent) { - *stkptr++=node; - node=node->parent; - } - - low=0; - for (;;) { - if (low>node->low) { - node->low=low; - } else { - low=node->low; - } - while (lowvalue) { - if (bio_read(1)) { - node->value=low; - } else { - ++low; - } - } - node->low=low; - if (stkptr==stk) { - break; - } - node=*--stkptr; - } - - return (node->value -# Microsoft Developer Studio Generated Build File, Format Version 6.00 -# ** DO NOT EDIT ** - -# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 - -CFG=DllOpenJPEG - Win32 Debug -!MESSAGE This is not a valid makefile. To build this project using NMAKE, -!MESSAGE use the Export Makefile command and run -!MESSAGE -!MESSAGE NMAKE /f "DllOpenJPEG.mak". -!MESSAGE -!MESSAGE You can specify a configuration when running NMAKE -!MESSAGE by defining the macro CFG on the command line. For example: -!MESSAGE -!MESSAGE NMAKE /f "DllOpenJPEG.mak" CFG="DllOpenJPEG - Win32 Debug" -!MESSAGE -!MESSAGE Possible choices for configuration are: -!MESSAGE -!MESSAGE "DllOpenJPEG - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") -!MESSAGE "DllOpenJPEG - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") -!MESSAGE - -# Begin Project -# PROP AllowPerConfigDependencies 0 -# PROP Scc_ProjName "" -# PROP Scc_LocalPath "" -CPP=cl.exe -MTL=midl.exe -RSC=rc.exe - -!IF "$(CFG)" == "DllOpenJPEG - Win32 Release" - -# PROP BASE Use_MFC 0 -# PROP BASE Use_Debug_Libraries 0 -# PROP BASE Output_Dir "Release" -# PROP BASE Intermediate_Dir "Release" -# PROP BASE Target_Dir "" -# PROP Use_MFC 0 -# PROP Use_Debug_Libraries 0 -# PROP Output_Dir "Release" -# PROP Intermediate_Dir "Release" -# PROP Ignore_Export_Lib 0 -# PROP Target_Dir "" -# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "DLLOPENJPEG_EXPORTS" /Yu"stdafx.h" /FD /c -# ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "OPJ_EXPORTS" /FD /c -# SUBTRACT CPP /YX /Yc /Yu -# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 -# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 -# ADD BASE RSC /l 0x40c /d "NDEBUG" -# ADD RSC /l 0x40c /d "NDEBUG" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"Release/OpenJPEG.dll" -# Begin Special Build Tool -SOURCE="$(InputPath)" -PostBuild_Cmds=if not exist dist mkdir dist copy libopenjpeg\openjpeg.h dist copy Release\OpenJPEG.dll dist copy Release\OpenJPEG.lib dist -# End Special Build Tool - -!ELSEIF "$(CFG)" == "DllOpenJPEG - Win32 Debug" - -# PROP BASE Use_MFC 0 -# PROP BASE Use_Debug_Libraries 1 -# PROP BASE Output_Dir "Debug" -# PROP BASE Intermediate_Dir "Debug" -# PROP BASE Target_Dir "" -# PROP Use_MFC 0 -# PROP Use_Debug_Libraries 1 -# PROP Output_Dir "Debug" -# PROP Intermediate_Dir "Debug" -# PROP Ignore_Export_Lib 0 -# PROP Target_Dir "" -# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "DLLOPENJPEG_EXPORTS" /Yu"stdafx.h" /FD /GZ /c -# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "OPJ_EXPORTS" /FD /GZ /c -# SUBTRACT CPP /YX /Yc /Yu -# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 -# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 -# ADD BASE RSC /l 0x40c /d "_DEBUG" -# ADD RSC /l 0x40c /d "_DEBUG" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"Debug/OpenJPEGd.dll" /pdbtype:sept -# Begin Special Build Tool -SOURCE="$(InputPath)" -PostBuild_Cmds=if not exist dist mkdir dist copy libopenjpeg\openjpeg.h dist copy Debug\OpenJPEGd.dll dist copy Debug\OpenJPEGd.lib dist -# End Special Build Tool - -!ENDIF - -# Begin Target - -# Name "DllOpenJPEG - Win32 Release" -# Name "DllOpenJPEG - Win32 Debug" -# Begin Group "Source Files" - -# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" -# Begin Source File - -SOURCE=.\libopenjpeg\bio.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\cio.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\dwt.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\event.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\image.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\j2k.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\j2k_lib.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\jp2.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\jpt.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\mct.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\mqc.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\openjpeg.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\pi.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\raw.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\t1.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\t2.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\tcd.c -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\tgt.c -# End Source File -# End Group -# Begin Group "Header Files" - -# PROP Default_Filter "h;hpp;hxx;hm;inl" -# Begin Source File - -SOURCE=.\libopenjpeg\bio.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\cio.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\dwt.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\event.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\fix.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\image.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\int.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\j2k.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\j2k_lib.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\jp2.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\jpt.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\mct.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\mqc.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\openjpeg.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\opj_includes.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\pi.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\raw.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\t1.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\t2.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\tcd.h -# End Source File -# Begin Source File - -SOURCE=.\libopenjpeg\tgt.h -# End Source File -# End Group -# Begin Group "Resource Files" - -# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" -# Begin Source File - -SOURCE=.\OpenJPEG.rc -# End Source File -# End Group -# End Target -# End Project diff --git a/jp3d/DllJp3dVM.sln b/jp3d/DllJp3dVM.sln deleted file mode 100755 index 7b97dad9..00000000 --- a/jp3d/DllJp3dVM.sln +++ /dev/null @@ -1,21 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 8.00 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DllOpenJPEG", "DllJp3dVM.vcproj", "{790A6CE5-CE92-4A59-ADF7-54A92760BD6C}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfiguration) = preSolution - Debug = Debug - Release = Release - EndGlobalSection - GlobalSection(ProjectConfiguration) = postSolution - {790A6CE5-CE92-4A59-ADF7-54A92760BD6C}.Debug.ActiveCfg = Debug|Win32 - {790A6CE5-CE92-4A59-ADF7-54A92760BD6C}.Debug.Build.0 = Debug|Win32 - {790A6CE5-CE92-4A59-ADF7-54A92760BD6C}.Release.ActiveCfg = Release|Win32 - {790A6CE5-CE92-4A59-ADF7-54A92760BD6C}.Release.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - EndGlobalSection - GlobalSection(ExtensibilityAddIns) = postSolution - EndGlobalSection -EndGlobal diff --git a/jp3d/DllJp3dVM.vcproj b/jp3d/DllJp3dVM.vcproj deleted file mode 100755 index 27c2108c..00000000 --- a/jp3d/DllJp3dVM.vcproj +++ /dev/null @@ -1,278 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/jp3d/LICENSE.txt b/jp3d/LICENSE.txt deleted file mode 100755 index 4716ce35..00000000 --- a/jp3d/LICENSE.txt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, HervŽ Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez, Image Processing Laboratory (LPI) - University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ diff --git a/jp3d/LibJp3dVM.sln b/jp3d/LibJp3dVM.sln deleted file mode 100755 index 7309c7ab..00000000 --- a/jp3d/LibJp3dVM.sln +++ /dev/null @@ -1,21 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 8.00 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibJp3dVM", "LibJp3dVM.vcproj", "{6F3FB035-8F4E-4794-B091-0F0A20223BE7}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfiguration) = preSolution - Debug = Debug - Release = Release - EndGlobalSection - GlobalSection(ProjectConfiguration) = postSolution - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Debug.ActiveCfg = Debug|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Debug.Build.0 = Debug|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Release.ActiveCfg = Release|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Release.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - EndGlobalSection - GlobalSection(ExtensibilityAddIns) = postSolution - EndGlobalSection -EndGlobal diff --git a/jp3d/LibJp3dVM.vcproj b/jp3d/LibJp3dVM.vcproj deleted file mode 100755 index 37953075..00000000 --- a/jp3d/LibJp3dVM.vcproj +++ /dev/null @@ -1,249 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/jp3d/Makefile b/jp3d/Makefile deleted file mode 100755 index f2b66cf0..00000000 --- a/jp3d/Makefile +++ /dev/null @@ -1,72 +0,0 @@ -# Linux makefile for OpenJPEG - -VER_MAJOR = 1 -VER_MINOR = 0.0 - -SRCS = ./libopenjpeg/bio.c ./libopenjpeg/cio.c ./libopenjpeg/dwt.c ./libopenjpeg/event.c ./libopenjpeg/image.c ./libopenjpeg/j2k.c ./libopenjpeg/j2k_lib.c ./libopenjpeg/jp2.c ./libopenjpeg/jpt.c ./libopenjpeg/mct.c ./libopenjpeg/mqc.c ./libopenjpeg/openjpeg.c ./libopenjpeg/pi.c ./libopenjpeg/raw.c ./libopenjpeg/t1.c ./libopenjpeg/t2.c ./libopenjpeg/tcd.c ./libopenjpeg/tgt.c -INCLS = ./libopenjpeg/bio.h ./libopenjpeg/cio.h ./libopenjpeg/dwt.h ./libopenjpeg/event.h ./libopenjpeg/fix.h ./libopenjpeg/image.h ./libopenjpeg/int.h ./libopenjpeg/j2k.h ./libopenjpeg/j2k_lib.h ./libopenjpeg/jp2.h ./libopenjpeg/jpt.h ./libopenjpeg/mct.h ./libopenjpeg/mqc.h ./libopenjpeg/openjpeg.h ./libopenjpeg/pi.h ./libopenjpeg/raw.h ./libopenjpeg/t1.h ./libopenjpeg/t2.h ./libopenjpeg/tcd.h ./libopenjpeg/tgt.h ./libopenjpeg/opj_includes.h -INCLUDE = -Ilibopenjpeg - -# General configuration variables: -CC = gcc -AR = ar - -INSTALLDIR = /usr/lib - -# Converts cr/lf to just lf -DOS2UNIX = dos2unix - -COMPILERFLAGS = -O3 -LIBRARIES = -lstdc++ - -MODULES = $(SRCS:.c=.o) -CFLAGS = $(COMPILERFLAGS) $(INCLUDE) - -TARGET = openjpeg -STATICLIB = lib$(TARGET).a -SHAREDLIB = lib$(TARGET)-$(VER_MAJOR).$(VER_MINOR).so -LIBNAME = lib$(TARGET).so.$(VER_MAJOR) - - - -default: all - -all: dist - -dist: OpenJPEG - mkdir dist - cp *.a dist/ - cp *.so dist/ - cp libopenjpeg/openjpeg.h dist/ - -dos2unix: - @$(DOS2UNIX) $(SRCS) $(INCLS) - -OpenJPEG: $(STATICLIB) $(SHAREDLIB) - -.c.o: - $(CC) $(CFLAGS) -c $< -o $@ - -$(STATICLIB): $(MODULES) - $(AR) r $@ $(MODULES) - -$(SHAREDLIB): $(MODULES) - $(CC) -s -shared -Wl,-soname,$(LIBNAME) -o $@ $(MODULES) $(LIBRARIES) - -install: - install -m 644 -o root -g root $(STATICLIB) $(INSTALLDIR) - install -m 755 -o root -g root $(SHAREDLIB) $(INSTALLDIR) - ln -sf $(SHAREDLIB) $(INSTALLDIR)/$(LIBNAME) - ldconfig - -clean: - rm -rf core dist/ u2dtmp* $(MODULES) $(STATICLIB) $(SHAREDLIB) $(LIBNAME) - -osx: - make -f Makefile.osx - -osxinstall: - make -f Makefile.osx install - -osxclean: - make -f Makefile.osx clean diff --git a/jp3d/README.txt b/jp3d/README.txt deleted file mode 100755 index ce04072e..00000000 --- a/jp3d/README.txt +++ /dev/null @@ -1,285 +0,0 @@ -=============================================================================== - JPEG2000 Part 10 (ISO/IEC 15444-10 JP3D) Verification Model - - Version 1.1 -=============================================================================== - - -1. Scope -================ - -This document describes the installation and the use of the JP3D VM decoder and encoder under several operating systems (Linux, Unix, Windows, ...). Version 1.1 contains a complete JPEG 2000 Part 10 encoder, as well as a decoder. -The supported functionalities are compliant with the JPEG2000 part 10 algorithm as described in the WD 6.0. -The provided encoder and the decoder are compatible also with the International standard IS 15444-1 (core coding system). -This implementation has been developped from OpenJPEG implementation of JPEG2000 standard, and for this reason it is written in C language. - -If you find some bugs or if you have problems using the encoder/decoder, please send an e-mail to jp3d@lpi.tel.uva.es - -2. Installing the code -====================================================== - -- After decompressing the zip file provided, you should find -at least the following files in the created 'jp3d_vm' directory: - - * libjp3dvm - This directory contains all library related code - * codec - This directory contains all codec related code - * tcltk - This directory contains the API scripts - * README - The file you are reading - * LICENCE - Copyright statement of the JP3D VM software - -2.1. Compiling the source code in Windows -------------------------------------------- - -This version has been compiled with Visual Studio 2003 using -the projects included in the distribution: - - * LibJp3dVM.vcproj - Creates the library with all the JP3D functionalities - * jp3d_vm_enc.vcproj - Test encoder - * jp3d_vm_dec.vcproj - Test decoder - -2.2. Compiling the source code in Unix-like systems -------------------------------------------- - -Library compilation ------------------------- -This version of the library has been tested under the following OS: -- Fedora Core - -The installation process is as simple as this : -1) Enter the 'jp3d_vm' directory -2) Build the distribution : -make -make install -3) Clean all files produced during the build process -make clean - -Codec compilation ------------------------- -Once you've built the library, you should compile the JP3D codec. - -1) Go to the 'codec' directory -2) Build the encoder and decoder programs: - -gcc convert.c volume_to_jp3d.c -o jp3d_vm_enc -I ../libjp3dvm/ -lm -ljp3dvm -gcc convert.c jp3d_to_volume.c -o jp3d_vm_dec -I ../libjp3dvm/ -lm -ljp3dvm - -Note: You should add '-L ../libjp3dvm/' to those lines if you -did not use the 'install' target (and the 'clean' target neither...). - -3. Running the JP3D VM -==================================================== - -3.1. JP3D ENCODER -==================================================== - -Required arguments ------------------------- - - * Input file(s): -i Involume [*.bin, *.pgx] - -Specifies the volume to compress. Accepted formats are *.BIN (raw binary data) or *.PGX files. -Both formats need some particular settings: - - a) BIN format. As it has no header, volume characteristics will be obtained from a .IMG file. Its location will be specified through the following argument: - -m Involumeinfo.IMG - This file shall have the following structure, with the appropiate value in each case (bit per voxel, color map, dimensions in X,Y,Z): - o Bpp %d - o Color Map %d - o Dimensions %d %d %d - - b) PGX format. Program will consider it as a volume slice. In order to denote a volume through a sequence of slices, you can define the input filename with the common pattern of the set of PGX files followed by a dash (as a wildcard character for the sequence numbers). - - * Output file: -o Outfile [*.jp3d, *j2k] - -Specifies the name of the file where the codestream will be saved. -Part 1 compliant codestream will be created when an outfile has .j2k format. - -Options --------- - - * Rate values : -r 20,10,5 - This option offers the possibility to define the compression rate to apply. - Each value is a factor of compression (i.e. 20 : 1) and will generate a different quality layer. A lossless compression will be signified by the value 1. - NOTE : The order used to define the different levels of compression is important and must be from left to right in descending order. - - * Quality values : -q 30,35,40 - This option offers the possibility to define the quality level to achieve. Each value is a psnr, to be given in dB, and represents a quality layer. - NOTE : The order used to define the different psnr-values is important and must be from left to right in ascending order. - - - * Number of resolutions : -n 3,3,2 - This option offers the possibility to define the number of resolution levels computed for each dimension of the volume through the discret wavelet transform (DWT). Resolution in axial dimension can have a different value than in horizontal and vertical cases, but must be lower. - DEFAULT VALUE : 3,3,1 - - - * Switch modes : -M 3 - This option offers the possibility to use a mode switch during the encoding process: - o BYPASS(LAZY) [1] - o RESET [2] - o RESTART(TERMALL) [4] - o VSC [8] - o ERTERM(SEGTERM) [16] - o SEGMARK(SEGSYM) [32] - o 3D_CONTEXT [64] - For several mode switch just sum corresponding values: i.e. -M 38 => RESTART(4) + RESET(2) + SEGMARK(32) - DEFAULT VALUE: 0 - - - * Progression order : -p LRCP - This option offers the possibility to specify the progression order. Possible progression orders are : LRCP, RLCP, RPCL, PCRL and CPRL. - DEFAULT VALUE: LRCP. - - - * Code-block size : -b 32,32,32 - This option offers the possibility to define the size of the code-block. The dimension must respect the constraint defined in the JPEG-2000 standard. The maximum value autorized is 64x64x64. - DEFAULT VALUE: 64,64,64 - - - * Precinct size : -c [128,128,128],[128,128,128],... - This option offers the possibility to define the size of the precincts at each resolution. Multiple records may be supplied, in which case the first record refers to the highest resolution level and subsequent records to lower resolution levels. The last specified record is right-shifted for each remaining lower resolution levels. - NOTE : specified values must be power of 2. - DEFAULT VALUE: 2^15 x 2^15 x 2^15 - - - * Tile size : -t 512,512,512 - This option offers the possibility to divide the volume in several tiles. The three values define the width, the heigth and the depth of the tile respectivily. - DEFAULT VALUE: Volume dimensions (one tile) - - - * Subsampling factor : -s 2,2,2 - This option offers the possibility to apply a subsampling factor for X, Y and Z axis. Value higher than 2 can be a source of error ! - DEFAULT VALUE: 1,1,1 - - - * SOP marker before each packet : -SOP - This option offers the possibility to add a specific marker before each packet. It is the marker SOP (Start of packet). If the option is not used no SOP marker will be added. - - - * EPH marker after each packet header : -EPH - This option offers the possibility to add a specific marker at the head of each packet header. It is the marker EPH (End of packet Header). If the option is not used no EPH marker will be added. - - - * Offset of the volume origin : -d 150,300,10 - This option offers the possibility to move the origine of the volume in X, Y and/or Z axis. The division in tile could be modified as the anchor point for tiling will be different than the volume origin. - NOTE : the offset of the volume can not be higher than the tile dimension if the tile option is used. - DEFAULT VALUE: 0,0,0 - - - * Offset of the tile origin : -T 100,75,5 - This option offers the possibility to move the anchor point of the volume in X, Y and/or Z axis. - NOTE : the tile anchor point can not be on the volume area. - DEFAULT VALUE: 0,0,0 - - - * Display the help menu : -help - This option displays on screen the content of this page - -Additional options ----------------------------------- - - * Encoding information file: -x index_name.idx - This option offers the possibility to create a text file with some structured information generated through the encoding. The name of the file must be specified, with .idx extension. The information structure is the following: - o Volume size: - + VolW + VolH + VolD - o Progression Order: - + Prog - o Tile size: - + TileW + TileH + TileD - o Number of components: - + NumComp - o Number of layers: - + NumLayer - o Number of decompositions (=(number of resolutions - 1)): - + NumDWTx + NumDWTy + NumDWTz - o Precinct size: - + [Precinct_width(NumDWT),Precinct_height(NumDWT),Precinct_depth(NumDWT)] - + [Precinct_width(NumDWT-1),Precinct_height(NumDWT-1),Precinct_depth(NumDWT-1)] - + ... - + [Precinct_width(0),Precinct_height(0),Precinct_depth(0)] - o Main Header end position: - + MH_EndPos - o Codestream size: - + CSSize - o Tile 0 information: - + TileNum (0) - + StartPos - + TileHeader_EndPos - + EndPos - + TotalDisto (this is the sum of the distortion reductions brought by each packet belonging to this tile) - + NumPix (this is the number of pixels in the tile) - + MaxMSE (=TotalDisto/NumPix) - o Tile1 information: - + TileNum (1) - + ... - o ... - o Tile N information: - + TileNum (N) - + ... - o Packet 0 from Tile 0 information: - + PackNum (0) - + TileNum (0) - + LayerNum - + ResNum - + CompNum - + PrecNum - + StartPos - + EndPos - + Disto (distortion reduction brought by this packet) - o Packet 1 from Tile 0 information: - + PackNum (1) - + ... - o ... - o Packet M from Tile 0 information - o Packet 0 from Tile 1 information - o ... - o Packet M from Tile N information - o Maximum distortion reduction on the whole volume: - + MaxDisto - o Total distortion on the whole volume (sum of the distortion reductions from all packets in the volume): - + TotalDisto - -3.2. JP3D DECODER -==================================================== - -Required arguments ------------------------- - - * Infile : -i compressed file - Currently accepts JP3D and J2K-files. The file type is identified based on its suffix (*.jp3d, *.j2k). - - - * Outfile(s) : -o decompressed file(s) - Currently accepts BIN-files and PGX-files. Binary data is written to the file (not ascii). - If a BIN-file is defined, decoder will create automatically the volume characteristic file appending a .IMG extension to the provided output filename. - If a PGX-file is defined, decoder will understand this as a file pattern, appending corresponding indice from 0 to the number of decoded slices. - NOTE : There will be as many output files as there are components: an indice starting from 0 will then be appended to the output filename, just before the extension. - - -Options available ------------------------- - - * Reduce factor : -r 1,1,0 - Set the number of highest resolution levels to be discarded in each dimension. The decoded volume size is effectively divided by 2 to the power of the number of discarded levels. - NOTE : The reduce factor is limited by the smallest total number of decomposition levels among tiles. - - - * Layer number : -l 2 - Set the maximum number of quality layers to decode. If there are less quality layers than the specified number, all the quality layers are decoded. - - - * Performance comparisons : -O original-file - This option offers the possibility to compute some quality results for the decompressed volume, like the PSNR value achieved or the global SSIM value. Needs the original file in order to compare with the new one. - NOTE: Only valid when -r option is 0,0,0 (both original and decompressed volumes have same resolutions) - NOTE: If original file is .BIN file, the volume characteristics file shall be defined with the -m option. - (i.e. -O original-BIN-file -m original-IMG-file) - - - * Byte order (Big-endian / Little-endian) : -BE - This option offers the possibility to save the decompressed volume with a predefined byte order. - DEFAULT VALUE: Little-endian - - - * Display the help menu : -help - This option displays on screen the content of this page - - diff --git a/jp3d/codec/CMakeLists.txt b/jp3d/codec/CMakeLists.txt deleted file mode 100644 index 8d6599c1..00000000 --- a/jp3d/codec/CMakeLists.txt +++ /dev/null @@ -1,53 +0,0 @@ -# Build the demo app, small examples - -# First thing define the common source: -SET(common_SRCS - convert.c - ) -# Then check if getopt is present: -INCLUDE (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake) -SET(DONT_HAVE_GETOPT 1) -IF(UNIX) #I am pretty sure only *nix sys have this anyway - CHECK_INCLUDE_FILE("getopt.h" CMAKE_HAVE_GETOPT_H) - # Seems like we need the contrary: - IF(CMAKE_HAVE_GETOPT_H) - SET(DONT_HAVE_GETOPT 0) - ENDIF(CMAKE_HAVE_GETOPT_H) -ENDIF(UNIX) - -# If not getopt was found then add it to the lib: -IF(DONT_HAVE_GETOPT) - ADD_DEFINITIONS(-DDONT_HAVE_GETOPT) - SET(common_SRCS - ${common_SRCS} - compat/getopt.c - ) -ENDIF(DONT_HAVE_GETOPT) - - -# Headers file are located here: -INCLUDE_DIRECTORIES( - ${CMAKE_CURRENT_SOURCE_DIR}/../libjp3dvm - ) - -# Do the proper thing when building static...if only there was configured -# headers or def files instead -#IF(NOT BUILD_SHARED_LIBS) -# ADD_DEFINITIONS(-DOPJ_STATIC) -#ENDIF(NOT BUILD_SHARED_LIBS) - -#FIND_PACKAGE(TIFF REQUIRED) - -# Loop over all executables: -FOREACH(exe jp3d_to_volume volume_to_jp3d) - ADD_EXECUTABLE(${exe} ${exe}.c ${common_SRCS}) - TARGET_LINK_LIBRARIES(${exe} ${OPJ_PREFIX}openjp3dvm) # ${TIFF_LIBRARIES}) - # On unix you need to link to the math library: - IF(UNIX) - TARGET_LINK_LIBRARIES(${exe} m) - ENDIF(UNIX) - # Install exe - INSTALL_TARGETS(/bin/ ${exe}) -ENDFOREACH(exe) - - diff --git a/jp3d/codec/convert.c b/jp3d/codec/convert.c deleted file mode 100755 index 07ecd929..00000000 --- a/jp3d/codec/convert.c +++ /dev/null @@ -1,997 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include -#include -#include "openjpeg.h" -#ifdef WIN32 -#include "dirent.h" -#else -#include -#endif /* WIN32 */ - - - -void dump_volume(FILE *fd, opj_volume_t * vol) { - int compno; - fprintf(fd, "volume {\n"); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1, vol->z1); - fprintf(fd, " numcomps=%d\n", vol->numcomps); - for (compno = 0; compno < vol->numcomps; compno++) { - opj_volume_comp_t *comp = &vol->comps[compno]; - fprintf(fd, " comp %d {\n", compno); - fprintf(fd, " dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz); - fprintf(fd, " prec=%d\n", comp->prec); - fprintf(fd, " sgnd=%d\n", comp->sgnd); - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); -} - -/* - * Get logarithm of an integer and round downwards. - * - * log2(a) - */ -static int int_floorlog2(int a) { - int l; - for (l = 0; a > 1; l++) { - a >>= 1; - } - return l; -} - -/* - * Divide an integer by a power of 2 and round upwards. - * - * a divided by 2^b - */ -static int int_ceildivpow2(int a, int b) { - return (a + (1 << b) - 1) >> b; -} - -/* - * Divide an integer and round upwards. - * - * a divided by b - */ -static int int_ceildiv(int a, int b) { - return (a + b - 1) / b; -} - - -/* -->> -->> -->> -->> - -PGX IMAGE FORMAT - -<<-- <<-- <<-- <<-- */ - - -unsigned char readuchar(FILE * f) -{ - unsigned char c1; - fread(&c1, 1, 1, f); - return c1; -} - -unsigned short readushort(FILE * f, int bigendian) -{ - unsigned char c1, c2; - fread(&c1, 1, 1, f); - fread(&c2, 1, 1, f); - if (bigendian) - return (c1 << 8) + c2; - else - return (c2 << 8) + c1; -} - -unsigned int readuint(FILE * f, int bigendian) -{ - unsigned char c1, c2, c3, c4; - fread(&c1, 1, 1, f); - fread(&c2, 1, 1, f); - fread(&c3, 1, 1, f); - fread(&c4, 1, 1, f); - if (bigendian) - return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4; - else - return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1; -} -/*****************************************/ -static unsigned short ShortSwap(unsigned short v) -{ - unsigned char c1, c2; - c1 = v & 0xff; - c2 = (v >> 8) & 0xff; - return (c1 << 8) + c2; -} - -static unsigned int LongSwap (unsigned int i) -{ - unsigned char b1, b2, b3, b4; - b1 = i & 255; - b2 = ( i >> 8 ) & 255; - b3 = ( i>>16 ) & 255; - b4 = ( i>>24 ) & 255; - return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4; -} -/*****************************************/ - -opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters) { - - FILE *f = NULL; - int w, h, prec; - unsigned long offset; - int i, s, numcomps, maxvalue, sliceno, slicepos, maxslice = 0; - - OPJ_COLOR_SPACE color_space; - opj_volume_cmptparm_t cmptparm; // maximum of 1 component - opj_volume_t * volume = NULL; - - char endian1,endian2,sign; - char signtmp[32]; - char temp[32]; - opj_volume_comp_t *comp = NULL; - - DIR *dirp; - struct dirent *direntp; - - char *tmp = NULL, *tmp2 = NULL, - *point = NULL, *pgx = NULL; - char tmpdirpath[MAX_PATH]; - char dirpath[MAX_PATH]; - char pattern[MAX_PATH]; - char pgxfiles[MAX_SLICES][MAX_PATH]; - int pgxslicepos[MAX_SLICES]; - char tmpno[3]; - - numcomps = 1; - color_space = CLRSPC_GRAY; - sliceno = 0; - maxvalue = 0; - memset(pgxfiles, 0, MAX_SLICES * MAX_PATH * sizeof(char)); - memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t)); - - /* Separación del caso de un único slice frente al de muchos */ - if ((tmp = strrchr(relpath,'-')) == NULL){ - //fprintf(stdout,"[INFO] A volume of only one slice....\n"); - sliceno = 1; - maxslice = 1; - strcpy(pgxfiles[0],relpath); - - } else { - //Fetch only the path - strcpy(tmpdirpath,relpath); - if ((tmp = strrchr(tmpdirpath,'/')) != NULL){ - tmp++; *tmp='\0'; - strcpy(dirpath,tmpdirpath); - } else { - strcpy(dirpath,"./"); - } - - //Fetch the pattern of the volume slices - if ((tmp = strrchr (relpath,'/')) != NULL) - tmp++; - else - tmp = relpath; - if ((tmp2 = strrchr(tmp,'-')) != NULL) - *tmp2='\0'; - else{ - fprintf(stdout, "[ERROR] tmp2 ha dado null. no ha encontrado el * %s %s",tmp,relpath); - return NULL; - } - strcpy(pattern,tmp); - - dirp = opendir( dirpath ); - if (dirp == NULL){ - fprintf(stdout, "[ERROR] Infile must be a .pgx file or a directory that contain pgx files"); - return NULL; - } - - /*Read all .pgx files of directory */ - while ( (direntp = readdir( dirp )) != NULL ) - { - /* Found a directory, but ignore . and .. */ - if(strcmp(".",direntp->d_name) == 0 || strcmp("..",direntp->d_name) == 0) - continue; - - if( ((pgx = strstr(direntp->d_name,pattern)) != NULL) && ((tmp2 = strstr(direntp->d_name,".pgx")) != NULL) ){ - - strcpy(tmp,dirpath); - tmp = strcat(tmp,direntp->d_name); - - //Obtenemos el index de la secuencia de slices - if ((tmp2 = strpbrk (direntp->d_name, "0123456789")) == NULL) - continue; - i = 0; - while (tmp2 != NULL) { - tmpno[i++] = *tmp2; - point = tmp2; - tmp2 = strpbrk (tmp2+1,"0123456789"); - }tmpno[i]='\0'; - - //Comprobamos que no estamos leyendo algo raro como pattern.jp3d - if ((point = strpbrk (point,".")) == NULL){ - break; - } - //Slicepos --> index de slice; Sliceno --> no de slices hasta el momento - slicepos = atoi(tmpno); - pgxslicepos[sliceno] = slicepos - 1; - sliceno++; - if (slicepos>maxslice) - maxslice = slicepos; - - //Colocamos el slices en su posicion correspondiente - strcpy(pgxfiles[slicepos-1],tmp); - } - } - - }/* else if pattern*.pgx */ - - if (!sliceno) { - fprintf(stdout,"[ERROR] No slices with this pattern founded !! Please check input volume name\n"); - return NULL; - } - /*if ( maxslice != sliceno) { - fprintf(stdout,"[ERROR] Slices are not sequentially numbered !! Please rename them accordingly\n"); - return NULL; - }*/ - - for (s=0;svolume_offset_x0; - cmptparm.y0 = parameters->volume_offset_y0; - cmptparm.z0 = parameters->volume_offset_z0; - cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1; - cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1; - cmptparm.l = !cmptparm.z0 ? (sliceno - 1) * parameters->subsampling_dz + 1 : cmptparm.z0 + (sliceno - 1) * parameters->subsampling_dz + 1; - - if (sign == '-') { - cmptparm.sgnd = 1; - } else { - cmptparm.sgnd = 0; - } - cmptparm.prec = prec; - cmptparm.bpp = prec; - cmptparm.dcoffset = parameters->dcoffset; - cmptparm.dx = parameters->subsampling_dx; - cmptparm.dy = parameters->subsampling_dy; - cmptparm.dz = parameters->subsampling_dz; - - /* create the volume */ - volume = opj_volume_create(numcomps, &cmptparm, color_space); - if(!volume) { - fclose(f); - return NULL; - } - /* set volume offset and reference grid */ - volume->x0 = cmptparm.x0; - volume->y0 = cmptparm.y0; - volume->z0 = cmptparm.z0; - volume->x1 = cmptparm.w; - volume->y1 = cmptparm.h; - volume->z1 = cmptparm.l; - - /* set volume data :only one component, that is a volume*/ - comp = &volume->comps[0]; - - }//if sliceno==1 - - offset = w * h * s; - - for (i = 0; i < w * h; i++) { - int v; - if (comp->prec <= 8) { - if (!comp->sgnd) { - v = readuchar(f); - } else { - v = (char) readuchar(f); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - v = readushort(f, cmptparm.bigendian); - } else { - v = (short) readushort(f, cmptparm.bigendian); - } - } else { - if (!comp->sgnd) { - v = readuint(f, cmptparm.bigendian); - } else { - v = (int) readuint(f, cmptparm.bigendian); - } - } - if (v > maxvalue) - maxvalue = v; - comp->data[i + offset] = v; - - } - fclose(f); - } // for s --> sliceno - comp->bpp = int_floorlog2(maxvalue) + 1; - if (sliceno != 1) - closedir( dirp ); - //dump_volume(stdout, volume); - return volume; -} - - -int volumetopgx(opj_volume_t * volume, char *outfile) { - int w, wr, wrr, h, hr, hrr, l, lr, lrr; - int i, j, compno, offset, sliceno; - FILE *fdest = NULL; - - for (compno = 0; compno < volume->numcomps; compno++) { - opj_volume_comp_t *comp = &volume->comps[compno]; - char name[256]; - int nbytes = 0; - char *tmp = outfile; - while (*tmp) { - tmp++; - } - while (*tmp!='.') { - tmp--; - } - *tmp='\0'; - for(sliceno = 0; sliceno < volume->z1 - volume->z0; sliceno++) { - - if (volume->numcomps > 1) { - sprintf(name, "%s%d-%d.pgx", outfile, sliceno+1, compno); - } else if ((volume->z1 - volume->z0) > 1) { - sprintf(name, "%s%d.pgx", outfile, sliceno+1); - } else { - sprintf(name, "%s.pgx", outfile); - } - - fdest = fopen(name, "wb"); - if (!fdest) { - fprintf(stdout, "[ERROR] Failed to open %s for writing \n", name); - return 1; - } - - fprintf(stdout,"[INFO] Writing in %s (%s)\n",name,volume->comps[0].bigendian ? "Bigendian" : "Little-endian"); - - w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx); - wr = volume->comps[compno].w; - wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]); - - h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy); - hr = volume->comps[compno].h; - hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]); - - l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz); - lr = volume->comps[compno].l; - lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]); - - fprintf(fdest, "PG %c%c %c%d %d %d\n", comp->bigendian ? 'M':'L', comp->bigendian ? 'L':'M',comp->sgnd ? '-' : '+', comp->prec, wr, hr); - if (comp->prec <= 8) { - nbytes = 1; - } else if (comp->prec <= 16) { - nbytes = 2; - } else { - nbytes = 4; - } - - offset = (sliceno / lrr * l) + (sliceno % lrr); - offset = wrr * hrr * offset; - //fprintf(stdout,"%d %d %d %d\n",offset,wrr*hrr,wrr,w); - for (i = 0; i < wrr * hrr; i++) { - int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset]; - if (volume->comps[0].bigendian) { - for (j = nbytes - 1; j >= 0; j--) { - char byte = (char) ((v >> (j * 8)) & 0xff); - fwrite(&byte, 1, 1, fdest); - } - } else { - for (j = 0; j <= nbytes - 1; j++) { - char byte = (char) ((v >> (j * 8)) & 0xff); - fwrite(&byte, 1, 1, fdest); - } - } - } - - fclose(fdest); - }//for sliceno - }//for compno - - return 0; -} - -/* -->> -->> -->> -->> - -BIN IMAGE FORMAT - -<<-- <<-- <<-- <<-- */ - -opj_volume_t* bintovolume(char *filename, char *fileimg, opj_cparameters_t *parameters) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - int subsampling_dz = parameters->subsampling_dz; - - int i, compno, w, h, l, numcomps = 1; - int prec, max = 0; - -// char temp[32]; - char line[100]; - int bigendian; - - FILE *f = NULL; - FILE *fimg = NULL; - OPJ_COLOR_SPACE color_space; - opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */ - opj_volume_t * volume = NULL; - opj_volume_comp_t *comp = NULL; - - bigendian = 0; - color_space = CLRSPC_GRAY; - - fimg = fopen(fileimg,"r"); - if (!fimg) { - fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", fileimg); - return 0; - } - - fseek(fimg, 0, SEEK_SET); - while (!feof(fimg)) { - fgets(line,100,fimg); - //fprintf(stdout,"%s %d \n",line,feof(fimg)); - if (strncmp(line,"Bpp",3) == 0){ - sscanf(line,"%*s%*[ \t]%d",&prec); - } else if (strncmp(line,"Color",5) == 0){ - sscanf(line, "%*s%*[ \t]%d",&color_space); - } else if (strncmp(line,"Dim",3) == 0){ - sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d",&w,&h,&l); - } - } - //fscanf(fimg, "Bpp%[ \t]%d%[ \t\n]",temp,&prec,temp); - //fscanf(fimg, "Color Map%[ \t]%d%[ \n\t]Dimensions%[ \t]%d%[ \t]%d%[ \t]%d%[ \n\t]",temp,&color_space,temp,temp,&w,temp,&h,temp,&l,temp); - //fscanf(fimg, "Resolution(mm)%[ \t]%d%[ \t]%d%[ \t]%d%[ \n\t]",temp,&subsampling_dx,temp,&subsampling_dy,temp,&subsampling_dz,temp); - - #ifdef VERBOSE - fprintf(stdout, "[INFO] %d \t %d %d %d \t %3.2f %2.2f %2.2f \t %d \n",color_space,w,h,l,subsampling_dx,subsampling_dy,subsampling_dz,prec); - #endif - fclose(fimg); - - /* initialize volume components */ - memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t)); - - cmptparm.prec = prec; - cmptparm.bpp = prec; - cmptparm.sgnd = 0; - cmptparm.bigendian = bigendian; - cmptparm.dcoffset = parameters->dcoffset; - cmptparm.dx = subsampling_dx; - cmptparm.dy = subsampling_dy; - cmptparm.dz = subsampling_dz; - cmptparm.w = w; - cmptparm.h = h; - cmptparm.l = l; - - /* create the volume */ - volume = opj_volume_create(numcomps, &cmptparm, color_space); - if(!volume) { - fprintf(stdout,"[ERROR] Unable to create volume"); - fclose(f); - return NULL; - } - - /* set volume offset and reference grid */ - volume->x0 = parameters->volume_offset_x0; - volume->y0 = parameters->volume_offset_y0; - volume->z0 = parameters->volume_offset_z0; - volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1; - volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1; - volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1; - - /* set volume data */ - f = fopen(filename, "rb"); - if (!f) { - fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename); - return 0; - } - - /* BINARY */ - for (compno = 0; compno < volume->numcomps; compno++) { - int whl = w * h * l; - /* set volume data */ - comp = &volume->comps[compno]; - - /*if (comp->prec <= 8) { - if (!comp->sgnd) { - unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char)); - fread(data, 1, whl, f); - for (i = 0; i < whl; i++) { - comp->data[i] = data[i]; - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } else { - char *data = (char *) malloc(whl); - fread(data, 1, whl, f); - for (i = 0; i < whl; i++) { - comp->data[i] = data[i]; - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short)); - int leido = fread(data, 2, whl, f); - if (!leido) { - free(data); fclose(f); - return NULL; - } - - for (i = 0; i < whl; i++) { - if (bigendian) //(c1 << 8) + c2; - comp->data[i] = data[i]; - else{ //(c2 << 8) + c1; - comp->data[i] = ShortSwap(data[i]); - } - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } else { - short *data = (short *) malloc(whl); - int leido = fread(data, 2, whl, f); - if (!leido) { - free(data); fclose(f); - return NULL; - } - for (i = 0; i < whl; i++) { - if (bigendian){ //(c1 << 8) + c2; - comp->data[i] = data[i]; - }else{ //(c2 << 8) + c1; - comp->data[i] = (short) ShortSwap((unsigned short) data[i]); - } - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } - } else { - if (!comp->sgnd) { - unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int)); - int leido = fread(data, 4, whl, f); - if (!leido) { - free(data); fclose(f); - return NULL; - } for (i = 0; i < whl; i++) { - if (!bigendian) - comp->data[i] = LongSwap(data[i]); - else - comp->data[i] = data[i]; - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } else { - int leido = fread(comp->data, 4, whl, f); - if (!leido) { - fclose(f); - return NULL; - } - for (i = 0; i < whl; i++) { - if (!bigendian) - comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]); - if (comp->data[i] > max) - max = comp->data[i]; - } - } - }*/ - - for (i = 0; i < whl; i++) { - int v; - if (comp->prec <= 8) { - if (!comp->sgnd) { - v = readuchar(f); - } else { - v = (char) readuchar(f); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - v = readushort(f, bigendian); - } else { - v = (short) readushort(f, bigendian); - } - } else { - if (!comp->sgnd) { - v = readuint(f, bigendian); - } else { - v = (int) readuint(f, bigendian); - } - } - if (v > max) - max = v; - comp->data[i] = v; - } - comp->bpp = int_floorlog2(max) + 1; - } - fclose(f); - return volume; -} - -int volumetobin(opj_volume_t * volume, char *outfile) { - int w, wr, wrr, h, hr, hrr, l, lr, lrr, max; - int i,j, compno, nbytes; - int offset, sliceno; - FILE *fdest = NULL; - FILE *fimgdest = NULL; -// char *imgtemp; - char name[256]; - - for (compno = 0; compno < 1; compno++) { //Only one component - - fdest = fopen(outfile, "wb"); - if (!fdest) { - fprintf(stdout, "[ERROR] Failed to open %s for writing\n", outfile); - return 1; - } - fprintf(stdout,"[INFO] Writing outfile %s (%s) \n",outfile, volume->comps[0].bigendian ? "Bigendian" : "Little-endian"); - - w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx); - wr = volume->comps[compno].w; - wrr = int_ceildivpow2(volume->comps[compno].w, volume->comps[compno].factor[0]); - - h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy); - hr = volume->comps[compno].h; - hrr = int_ceildivpow2(volume->comps[compno].h, volume->comps[compno].factor[1]); - - l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz); - lr = volume->comps[compno].l; - lrr = int_ceildivpow2(volume->comps[compno].l, volume->comps[compno].factor[2]); - - max = (volume->comps[compno].prec <= 8) ? 255 : (1 << volume->comps[compno].prec) - 1; - - volume->comps[compno].x0 = int_ceildivpow2(volume->comps[compno].x0 - int_ceildiv(volume->x0, volume->comps[compno].dx), volume->comps[compno].factor[0]); - volume->comps[compno].y0 = int_ceildivpow2(volume->comps[compno].y0 - int_ceildiv(volume->y0, volume->comps[compno].dy), volume->comps[compno].factor[1]); - volume->comps[compno].z0 = int_ceildivpow2(volume->comps[compno].z0 - int_ceildiv(volume->z0, volume->comps[compno].dz), volume->comps[compno].factor[2]); - - if (volume->comps[0].prec <= 8) { - nbytes = 1; - } else if (volume->comps[0].prec <= 16) { - nbytes = 2; - } else { - nbytes = 4; - } - - //fprintf(stdout,"w %d wr %d wrr %d h %d hr %d hrr %d l %d lr %d lrr %d max %d nbytes %d\n Factor %d %d %d",w,wr,wrr,h,hr,hrr,l,lr,lrr,max,nbytes,volume->comps[compno].factor[0],volume->comps[compno].factor[1],volume->comps[compno].factor[2]); - - for(sliceno = 0; sliceno < lrr; sliceno++) { - offset = (sliceno / lrr * l) + (sliceno % lrr); - offset = wrr * hrr * offset; - for (i = 0; i < wrr * hrr; i++) { - int v = volume->comps[0].data[(i / wrr * w) + (i % wrr) + offset]; - if (volume->comps[0].bigendian) { - for (j = nbytes - 1; j >= 0; j--) { - char byte = (char) ((v >> (j * 8)) & 0xff); - fwrite(&byte, 1, 1, fdest); - } - } else { - for (j = 0; j <= nbytes - 1; j++) { - char byte = (char) ((v >> (j * 8)) & 0xff); - fwrite(&byte, 1, 1, fdest); - } - } - } - } - - } - - fclose(fdest); - - sprintf(name,"%s.img",outfile); - fimgdest = fopen(name, "w"); - if (!fimgdest) { - fprintf(stdout, "[ERROR] Failed to open %s for writing\n", name); - return 1; - } - fprintf(fimgdest, "Bpp\t%d\nColor Map\t2\nDimensions\t%d\t%d\t%d\nResolution(mm)\t%d\t%d\t%d\t\n", - volume->comps[0].prec,wrr,hrr,lrr,volume->comps[0].dx,volume->comps[0].dy,volume->comps[0].dz); - - fclose(fimgdest); - return 0; -} -/* -->> -->> -->> -->> - -IMG IMAGE FORMAT - -<<-- <<-- <<-- <<-- */ -opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - int subsampling_dz = parameters->subsampling_dz; - - int i, compno, w, h, l, numcomps = 1; - int prec, max = 0, min = 0; - float dx, dy, dz; - char filename[100], tmpdirpath[100], dirpath[100], *tmp; - char line[100], datatype[100]; - int bigendian; - - FILE *f = NULL; - FILE *fimg = NULL; - OPJ_COLOR_SPACE color_space; - opj_volume_cmptparm_t cmptparm; /* maximum of 1 component */ - opj_volume_t * volume = NULL; - opj_volume_comp_t *comp = NULL; - - bigendian = 0; - color_space = CLRSPC_GRAY; - - fimg = fopen(fileimg,"r"); - if (!fimg) { - fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", fileimg); - return 0; - } - - //Fetch only the path - strcpy(tmpdirpath,fileimg); - if ((tmp = strrchr(tmpdirpath,'/')) != NULL){ - tmp++; *tmp='\0'; - strcpy(dirpath,tmpdirpath); - } else { - strcpy(dirpath,"./"); - } - - fseek(fimg, 0, SEEK_SET); - while (!feof(fimg)) { - fgets(line,100,fimg); - //fprintf(stdout,"%s %d \n",line,feof(fimg)); - if (strncmp(line,"Image",5) == 0){ - sscanf(line,"%*s%*[ \t]%s",datatype); - } else if (strncmp(line,"File",4) == 0){ - sscanf(line,"%*s %*s%*[ \t]%s",filename); - strcat(dirpath, filename); - strcpy(filename,dirpath); - } else if (strncmp(line,"Min",3) == 0){ - sscanf(line,"%*s %*s%*[ \t]%d%*[ \t]%d",&min,&max); - prec = int_floorlog2(max - min + 1); - } else if (strncmp(line,"Bpp",3) == 0){ - sscanf(line,"%*s%*[ \t]%d",&prec); - } else if (strncmp(line,"Color",5) == 0){ - sscanf(line, "%*s %*s%*[ \t]%d",&color_space); - } else if (strncmp(line,"Dim",3) == 0){ - sscanf(line, "%*s%*[ \t]%d%*[ \t]%d%*[ \t]%d",&w,&h,&l); - } else if (strncmp(line,"Res",3) == 0){ - sscanf(line,"%*s%*[ \t]%f%*[ \t]%f%*[ \t]%f",&dx,&dy,&dz); - } - - } - #ifdef VERBOSE - fprintf(stdout, "[INFO] %s %d \t %d %d %d \t %f %f %f \t %d %d %d \n",filename,color_space,w,h,l,dx,dy,dz,max,min,prec); - #endif - fclose(fimg); - - /* error control */ - if ( !prec || !w || !h || !l ){ - fprintf(stderr,"[ERROR] Unable to read IMG file correctly. Found some null values."); - return NULL; - } - - /* initialize volume components */ - memset(&cmptparm, 0, sizeof(opj_volume_cmptparm_t)); - - cmptparm.prec = prec; - cmptparm.bpp = prec; - cmptparm.sgnd = 0; - cmptparm.bigendian = bigendian; - cmptparm.dcoffset = parameters->dcoffset; - cmptparm.dx = subsampling_dx; - cmptparm.dy = subsampling_dy; - cmptparm.dz = subsampling_dz; - cmptparm.w = w; - cmptparm.h = h; - cmptparm.l = l; - - /* create the volume */ - volume = opj_volume_create(numcomps, &cmptparm, color_space); - if(!volume) { - fprintf(stdout,"[ERROR] Unable to create volume"); - return NULL; - } - - /* set volume offset and reference grid */ - volume->x0 = parameters->volume_offset_x0; - volume->y0 = parameters->volume_offset_y0; - volume->z0 = parameters->volume_offset_z0; - volume->x1 = parameters->volume_offset_x0 + (w - 1) * subsampling_dx + 1; - volume->y1 = parameters->volume_offset_y0 + (h - 1) * subsampling_dy + 1; - volume->z1 = parameters->volume_offset_z0 + (l - 1) * subsampling_dz + 1; - - max = 0; - /* set volume data */ - f = fopen(filename, "rb"); - if (!f) { - fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename); - fclose(f); - return 0; - } - - /* BINARY */ - for (compno = 0; compno < volume->numcomps; compno++) { - int whl = w * h * l; - /* set volume data */ - comp = &volume->comps[compno]; - - /*if (comp->prec <= 8) { - if (!comp->sgnd) { - unsigned char *data = (unsigned char *) malloc(whl * sizeof(unsigned char)); - fread(data, 1, whl, f); - for (i = 0; i < whl; i++) { - comp->data[i] = data[i]; - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } else { - char *data = (char *) malloc(whl); - fread(data, 1, whl, f); - for (i = 0; i < whl; i++) { - comp->data[i] = data[i]; - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - unsigned short *data = (unsigned short *) malloc(whl * sizeof(unsigned short)); - int leido = fread(data, 2, whl, f); - if (!leido) { - free(data); fclose(f); - return NULL; - } - - for (i = 0; i < whl; i++) { - if (bigendian) //(c1 << 8) + c2; - comp->data[i] = data[i]; - else{ //(c2 << 8) + c1; - comp->data[i] = ShortSwap(data[i]); - } - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } else { - short *data = (short *) malloc(whl); - int leido = fread(data, 2, whl, f); - if (!leido) { - free(data); fclose(f); - return NULL; - } - for (i = 0; i < whl; i++) { - if (bigendian){ //(c1 << 8) + c2; - comp->data[i] = data[i]; - }else{ //(c2 << 8) + c1; - comp->data[i] = (short) ShortSwap((unsigned short) data[i]); - } - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } - } else { - if (!comp->sgnd) { - unsigned int *data = (unsigned int *) malloc(whl * sizeof(unsigned int)); - int leido = fread(data, 4, whl, f); - if (!leido) { - free(data); fclose(f); - return NULL; - } for (i = 0; i < whl; i++) { - if (!bigendian) - comp->data[i] = LongSwap(data[i]); - else - comp->data[i] = data[i]; - if (comp->data[i] > max) - max = comp->data[i]; - } - free(data); - } else { - int leido = fread(comp->data, 4, whl, f); - if (!leido) { - fclose(f); - return NULL; - } - for (i = 0; i < whl; i++) { - if (!bigendian) - comp->data[i] = (int) LongSwap((unsigned int) comp->data[i]); - if (comp->data[i] > max) - max = comp->data[i]; - } - } - }*/ - - for (i = 0; i < whl; i++) { - int v; - if (comp->prec <= 8) { - if (!comp->sgnd) { - v = readuchar(f); - } else { - v = (char) readuchar(f); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - v = readushort(f, bigendian); - } else { - v = (short) readushort(f, bigendian); - } - } else { - if (!comp->sgnd) { - v = readuint(f, bigendian); - } else { - v = (int) readuint(f, bigendian); - } - } - if (v > max) - max = v; - comp->data[i] = v; - } - comp->bpp = int_floorlog2(max) + 1; - } - fclose(f); - return volume; -} - diff --git a/jp3d/codec/convert.h b/jp3d/codec/convert.h deleted file mode 100755 index 18ae2127..00000000 --- a/jp3d/codec/convert.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, HervŽ Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __JP3D_CONVERT_H -#define __JP3D_CONVERT_H - -/** -Load a single volume component encoded in PGX file format -@param filename Name of the PGX file to load -@param parameters *List ?* -@return Returns a greyscale volume if successful, returns NULL otherwise -*/ -opj_volume_t* pgxtovolume(char *filename, opj_cparameters_t *parameters); - -int volumetopgx(opj_volume_t *volume, char *outfile); - -opj_volume_t* bintovolume(char *filename,char *fileimg, opj_cparameters_t *parameters); - -int volumetobin(opj_volume_t *volume, char *outfile); - -opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters); - -#endif /* __J2K_CONVERT_H */ - diff --git a/jp3d/codec/dirent.h b/jp3d/codec/dirent.h deleted file mode 100755 index a67e5874..00000000 --- a/jp3d/codec/dirent.h +++ /dev/null @@ -1,676 +0,0 @@ -/* - * uce-dirent.h - operating system independent dirent implementation - * - * Copyright (C) 1998-2002 Toni Ronkko - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * ``Software''), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * - * May 28 1998, Toni Ronkko - * - * $Id: uce-dirent.h,v 1.7 2002/05/13 10:48:35 tr Exp $ - * - * $Log: uce-dirent.h,v $ - * Revision 1.7 2002/05/13 10:48:35 tr - * embedded some source code directly to the header so that no source - * modules need to be included in the MS Visual C project using the - * interface, removed all the dependencies to other headers of the `uce' - * library so that the header can be made public - * - * Revision 1.6 2002/04/12 16:22:04 tr - * Unified Compiling Environment (UCE) replaced `std' library - * - * Revision 1.5 2001/07/20 16:33:40 tr - * moved to `std' library and re-named defines accordingly - * - * Revision 1.4 2001/07/10 16:47:18 tronkko - * revised comments - * - * Revision 1.3 2001/01/11 13:16:43 tr - * using ``uce-machine.h'' for finding out defines such as `FREEBSD' - * - * Revision 1.2 2000/10/08 16:00:41 tr - * copy of FreeBSD man page - * - * Revision 1.1 2000/07/10 05:53:16 tr - * Initial revision - * - * Revision 1.2 1998/07/19 18:29:14 tr - * Added error reporting capabilities and some asserts. - * - * Revision 1.1 1998/07/04 16:27:51 tr - * Initial revision - * - * - * MSVC 1.0 scans automatic dependencies incorrectly when your project - * contains this very header. The problem is that MSVC cannot handle - * include directives inside #if..#endif block those are never entered. - * Since this header ought to compile in many different operating systems, - * there had to be several conditional blocks that are compiled only in - * operating systems for what they were designed for. MSVC 1.0 cannot - * handle inclusion of sys/dir.h in a part that is compiled only in Apollo - * operating system. To fix the problem you need to insert DIR.H into - * SYSINCL.DAT located in MSVC\BIN directory and restart visual C++. - * Consult manuals for more informaton about the problem. - * - * Since many UNIX systems have dirent.h we assume to have one also. - * However, if your UNIX system does not have dirent.h you can download one - * for example at: http://ftp.uni-mannheim.de/ftp/GNU/dirent/dirent.tar.gz. - * You can also see if you have one of dirent.h, direct.h, dir.h, ndir.h, - * sys/dir.h and sys/ndir.h somewhere. Try defining HAVE_DIRENT_H, - * HAVE_DIRECT_H, HAVE_DIR_H, HAVE_NDIR_H, HAVE_SYS_DIR_H and - * HAVE_SYS_NDIR_H according to the files found. - */ -#ifndef DIRENT_H -#define DIRENT_H -#define DIRENT_H_INCLUDED - -/* find out platform */ -#if defined(MSDOS) /* MS-DOS */ -#elif defined(__MSDOS__) /* Turbo C/Borland */ -# define MSDOS -#elif defined(__DOS__) /* Watcom */ -# define MSDOS -#endif - -#if defined(WIN32) /* MS-Windows */ -#elif defined(__NT__) /* Watcom */ -# define WIN32 -#elif defined(_WIN32) /* Microsoft */ -# define WIN32 -#elif defined(__WIN32__) /* Borland */ -# define WIN32 -#endif - -/* - * See what kind of dirent interface we have unless autoconf has already - * determinated that. - */ -#if !defined(HAVE_DIRENT_H) && !defined(HAVE_DIRECT_H) && !defined(HAVE_SYS_DIR_H) && !defined(HAVE_NDIR_H) && !defined(HAVE_SYS_NDIR_H) && !defined(HAVE_DIR_H) -# if defined(_MSC_VER) /* Microsoft C/C++ */ - /* no dirent.h */ -# elif defined(__BORLANDC__) /* Borland C/C++ */ -# define HAVE_DIRENT_H -# define VOID_CLOSEDIR -# elif defined(__TURBOC__) /* Borland Turbo C */ - /* no dirent.h */ -# elif defined(__WATCOMC__) /* Watcom C/C++ */ -# define HAVE_DIRECT_H -# elif defined(__apollo) /* Apollo */ -# define HAVE_SYS_DIR_H -# elif defined(__hpux) /* HP-UX */ -# define HAVE_DIRENT_H -# elif defined(__alpha) || defined(__alpha__) /* Alpha OSF1 */ -# error "not implemented" -# elif defined(__sgi) /* Silicon Graphics */ -# define HAVE_DIRENT_H -# elif defined(sun) || defined(_sun) /* Sun Solaris */ -# define HAVE_DIRENT_H -# elif defined(__FreeBSD__) /* FreeBSD */ -# define HAVE_DIRENT_H -# elif defined(__linux__) /* Linux */ -# define HAVE_DIRENT_H -# elif defined(__GNUC__) /* GNU C/C++ */ -# define HAVE_DIRENT_H -# else -# error "not implemented" -# endif -#endif - -/* include proper interface headers */ -#if defined(HAVE_DIRENT_H) -# include -# ifdef FREEBSD -# define NAMLEN(dp) ((int)((dp)->d_namlen)) -# else -# define NAMLEN(dp) ((int)(strlen((dp)->d_name))) -# endif - -#elif defined(HAVE_NDIR_H) -# include -# define NAMLEN(dp) ((int)((dp)->d_namlen)) - -#elif defined(HAVE_SYS_NDIR_H) -# include -# define NAMLEN(dp) ((int)((dp)->d_namlen)) - -#elif defined(HAVE_DIRECT_H) -# include -# define NAMLEN(dp) ((int)((dp)->d_namlen)) - -#elif defined(HAVE_DIR_H) -# include -# define NAMLEN(dp) ((int)((dp)->d_namlen)) - -#elif defined(HAVE_SYS_DIR_H) -# include -# include -# ifndef dirent -# define dirent direct -# endif -# define NAMLEN(dp) ((int)((dp)->d_namlen)) - -#elif defined(MSDOS) || defined(WIN32) - - /* figure out type of underlaying directory interface to be used */ -# if defined(WIN32) -# define DIRENT_WIN32_INTERFACE -# elif defined(MSDOS) -# define DIRENT_MSDOS_INTERFACE -# else -# error "missing native dirent interface" -# endif - - /*** WIN32 specifics ***/ -# if defined(DIRENT_WIN32_INTERFACE) -# include -# if !defined(DIRENT_MAXNAMLEN) -# define DIRENT_MAXNAMLEN (MAX_PATH) -# endif - - - /*** MS-DOS specifics ***/ -# elif defined(DIRENT_MSDOS_INTERFACE) -# include - - /* Borland defines file length macros in dir.h */ -# if defined(__BORLANDC__) -# include -# if !defined(DIRENT_MAXNAMLEN) -# define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT)) -# endif -# if !defined(_find_t) -# define _find_t find_t -# endif - - /* Turbo C defines ffblk structure in dir.h */ -# elif defined(__TURBOC__) -# include -# if !defined(DIRENT_MAXNAMLEN) -# define DIRENT_MAXNAMLEN ((MAXFILE)+(MAXEXT)) -# endif -# define DIRENT_USE_FFBLK - - /* MSVC */ -# elif defined(_MSC_VER) -# if !defined(DIRENT_MAXNAMLEN) -# define DIRENT_MAXNAMLEN (12) -# endif - - /* Watcom */ -# elif defined(__WATCOMC__) -# if !defined(DIRENT_MAXNAMLEN) -# if defined(__OS2__) || defined(__NT__) -# define DIRENT_MAXNAMLEN (255) -# else -# define DIRENT_MAXNAMLEN (12) -# endif -# endif - -# endif -# endif - - /*** generic MS-DOS and MS-Windows stuff ***/ -# if !defined(NAME_MAX) && defined(DIRENT_MAXNAMLEN) -# define NAME_MAX DIRENT_MAXNAMLEN -# endif -# if NAME_MAX < DIRENT_MAXNAMLEN -# error "assertion failed: NAME_MAX >= DIRENT_MAXNAMLEN" -# endif - - - /* - * Substitute for real dirent structure. Note that `d_name' field is a - * true character array although we have it copied in the implementation - * dependent data. We could save some memory if we had declared `d_name' - * as a pointer refering the name within implementation dependent data. - * We have not done that since some code may rely on sizeof(d_name) to be - * something other than four. Besides, directory entries are typically so - * small that it takes virtually no time to copy them from place to place. - */ - typedef struct dirent { - char d_name[NAME_MAX + 1]; - - /*** Operating system specific part ***/ -# if defined(DIRENT_WIN32_INTERFACE) /*WIN32*/ - WIN32_FIND_DATA data; -# elif defined(DIRENT_MSDOS_INTERFACE) /*MSDOS*/ -# if defined(DIRENT_USE_FFBLK) - struct ffblk data; -# else - struct _find_t data; -# endif -# endif - } dirent; - - /* DIR substitute structure containing directory name. The name is - * essential for the operation of ``rewinndir'' function. */ - typedef struct DIR { - char *dirname; /* directory being scanned */ - dirent current; /* current entry */ - int dirent_filled; /* is current un-processed? */ - - /*** Operating system specific part ***/ -# if defined(DIRENT_WIN32_INTERFACE) - HANDLE search_handle; -# elif defined(DIRENT_MSDOS_INTERFACE) -# endif - } DIR; - -# ifdef __cplusplus -extern "C" { -# endif - -/* supply prototypes for dirent functions */ -static DIR *opendir (const char *dirname); -static struct dirent *readdir (DIR *dirp); -static int closedir (DIR *dirp); -static void rewinddir (DIR *dirp); - -/* - * Implement dirent interface as static functions so that the user does not - * need to change his project in any way to use dirent function. With this - * it is sufficient to include this very header from source modules using - * dirent functions and the functions will be pulled in automatically. - */ -#include -#include -#include -#include -#include - -/* use ffblk instead of _find_t if requested */ -#if defined(DIRENT_USE_FFBLK) -# define _A_ARCH (FA_ARCH) -# define _A_HIDDEN (FA_HIDDEN) -# define _A_NORMAL (0) -# define _A_RDONLY (FA_RDONLY) -# define _A_SUBDIR (FA_DIREC) -# define _A_SYSTEM (FA_SYSTEM) -# define _A_VOLID (FA_LABEL) -# define _dos_findnext(dest) findnext(dest) -# define _dos_findfirst(name,flags,dest) findfirst(name,dest,flags) -#endif - -static int _initdir (DIR *p); -static const char *_getdirname (const struct dirent *dp); -static void _setdirname (struct DIR *dirp); - -/* - * - * open directory stream for reading - * DIR *opendir (const char *dirname); - * - * Open named directory stream for read and return pointer to the - * internal working area that is used for retrieving individual directory - * entries. The internal working area has no fields of your interest. - * - * Returns a pointer to the internal working area or NULL in case the - * directory stream could not be opened. Global `errno' variable will set - * in case of error as follows: - * - * - * [EACESS |Permission denied. - * [EMFILE |Too many open files used by the process. - * [ENFILE |Too many open files in system. - * [ENOENT |Directory does not exist. - * [ENOMEM |Insufficient memory. - * [ENOTDIR |dirname does not refer to directory. This value is not - * reliable on MS-DOS and MS-Windows platforms. Many - * implementations return ENOENT even when the name refers to a - * file.] - *
- *
- */ -static DIR *opendir(const char *dirname) -{ - DIR *dirp; - assert (dirname != NULL); - - dirp = (DIR*)malloc (sizeof (struct DIR)); - if (dirp != NULL) { - char *p; - - /* allocate room for directory name */ - dirp->dirname = (char*) malloc (strlen (dirname) + 1 + strlen ("\\*.*")); - if (dirp->dirname == NULL) { - /* failed to duplicate directory name. errno set by malloc() */ - free (dirp); - return NULL; - } - /* Copy directory name while appending directory separator and "*.*". - * Directory separator is not appended if the name already ends with - * drive or directory separator. Directory separator is assumed to be - * '/' or '\' and drive separator is assumed to be ':'. */ - strcpy (dirp->dirname, dirname); - p = strchr (dirp->dirname, '\0'); - if (dirp->dirname < p && - *(p - 1) != '\\' && *(p - 1) != '/' && *(p - 1) != ':') - { - strcpy (p++, "\\"); - } -# ifdef DIRENT_WIN32_INTERFACE - strcpy (p, "*"); /*scan files with and without extension in win32*/ -# else - strcpy (p, "*.*"); /*scan files with and without extension in DOS*/ -# endif - - /* open stream */ - if (_initdir (dirp) == 0) { - /* initialization failed */ - free (dirp->dirname); - free (dirp); - return NULL; - } - } - return dirp; -} - - -/* - * - * read a directory entry - * struct dirent *readdir (DIR *dirp); - * - * Read individual directory entry and return pointer to a structure - * containing the name of the entry. Individual directory entries returned - * include normal files, sub-directories, pseudo-directories "." and ".." - * and also volume labels, hidden files and system files in MS-DOS and - * MS-Windows. You might want to use stat(2) function to determinate which - * one are you dealing with. Many dirent implementations already contain - * equivalent information in dirent structure but you cannot depend on - * this. - * - * The dirent structure contains several system dependent fields that - * generally have no interest to you. The only interesting one is char - * d_name[] that is also portable across different systems. The d_name - * field contains the name of the directory entry without leading path. - * While d_name is portable across different systems the actual storage - * capacity of d_name varies from system to system and there is no portable - * way to find out it at compile time as different systems define the - * capacity of d_name with different macros and some systems do not define - * capacity at all (besides actual declaration of the field). If you really - * need to find out storage capacity of d_name then you might want to try - * NAME_MAX macro. The NAME_MAX is defined in POSIX standard althought - * there are many MS-DOS and MS-Windows implementations those do not define - * it. There are also systems that declare d_name as "char d_name[1]" and - * then allocate suitable amount of memory at run-time. Thanks to Alain - * Decamps (Alain.Decamps@advalvas.be) for pointing it out to me. - * - * This all leads to the fact that it is difficult to allocate space - * for the directory names when the very same program is being compiled on - * number of operating systems. Therefore I suggest that you always - * allocate space for directory names dynamically. - * - * - * Returns a pointer to a structure containing name of the directory entry - * in `d_name' field or NULL if there was an error. In case of an error the - * global `errno' variable will set as follows: - * - * - * [EBADF |dir parameter refers to an invalid directory stream. This value - * is not set reliably on all implementations.] - *
- *
- */ -static struct dirent * -readdir (DIR *dirp) -{ - assert(dirp != NULL); - if (dirp == NULL) { - errno = EBADF; - return NULL; - } - -#if defined(DIRENT_WIN32_INTERFACE) - if (dirp->search_handle == INVALID_HANDLE_VALUE) { - /* directory stream was opened/rewound incorrectly or it ended normally */ - errno = EBADF; - return NULL; - } -#endif - - if (dirp->dirent_filled != 0) { - /* - * Directory entry has already been retrieved and there is no need to - * retrieve a new one. Directory entry will be retrieved in advance - * when the user calls readdir function for the first time. This is so - * because real dirent has separate functions for opening and reading - * the stream whereas Win32 and DOS dirents open the stream - * automatically when we retrieve the first file. Therefore, we have to - * save the first file when opening the stream and later we have to - * return the saved entry when the user tries to read the first entry. - */ - dirp->dirent_filled = 0; - } else { - /* fill in entry and return that */ -#if defined(DIRENT_WIN32_INTERFACE) - if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) { - /* Last file has been processed or an error occured */ - FindClose (dirp->search_handle); - dirp->search_handle = INVALID_HANDLE_VALUE; - errno = ENOENT; - return NULL; - } - -# elif defined(DIRENT_MSDOS_INTERFACE) - if (_dos_findnext (&dirp->current.data) != 0) { - /* _dos_findnext and findnext will set errno to ENOENT when no - * more entries could be retrieved. */ - return NULL; - } -# endif - - _setdirname (dirp); - assert (dirp->dirent_filled == 0); - } - return &dirp->current; -} - - -/* - * - * close directory stream. - * int closedir (DIR *dirp); - * - * Close directory stream opened by the `opendir' function. Close of - * directory stream invalidates the DIR structure as well as previously read - * dirent entry. - * - * The function typically returns 0 on success and -1 on failure but - * the function may be declared to return void on same systems. At least - * Borland C/C++ and some UNIX implementations use void as a return type. - * The dirent wrapper tries to define VOID_CLOSEDIR whenever closedir is - * known to return nothing. The very same definition is made by the GNU - * autoconf if you happen to use it. - * - * The global `errno' variable will set to EBADF in case of error. - * - */ -static int -closedir (DIR *dirp) -{ - int retcode = 0; - - /* make sure that dirp points to legal structure */ - assert (dirp != NULL); - if (dirp == NULL) { - errno = EBADF; - return -1; - } - - /* free directory name and search handles */ - if (dirp->dirname != NULL) free (dirp->dirname); - -#if defined(DIRENT_WIN32_INTERFACE) - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - if (FindClose (dirp->search_handle) == FALSE) { - /* Unknown error */ - retcode = -1; - errno = EBADF; - } - } -#endif - - /* clear dirp structure to make sure that it cannot be used anymore*/ - memset (dirp, 0, sizeof (*dirp)); -# if defined(DIRENT_WIN32_INTERFACE) - dirp->search_handle = INVALID_HANDLE_VALUE; -# endif - - free (dirp); - return retcode; -} - - -/* - * - * rewind directory stream to the beginning - * void rewinddir (DIR *dirp); - * - * Rewind directory stream to the beginning so that the next call of - * readdir() returns the very first directory entry again. However, note - * that next call of readdir() may not return the same directory entry as it - * did in first time. The directory stream may have been affected by newly - * created files. - * - * Almost every dirent implementation ensure that rewinddir will update - * the directory stream to reflect any changes made to the directory entries - * since the previous ``opendir'' or ``rewinddir'' call. Keep an eye on - * this if your program depends on the feature. I know at least one dirent - * implementation where you are required to close and re-open the stream to - * see the changes. - * - * Returns nothing. If something went wrong while rewinding, you will - * notice it later when you try to retrieve the first directory entry. - */ -static void -rewinddir (DIR *dirp) -{ - /* make sure that dirp is legal */ - assert (dirp != NULL); - if (dirp == NULL) { - errno = EBADF; - return; - } - assert (dirp->dirname != NULL); - - /* close previous stream */ -#if defined(DIRENT_WIN32_INTERFACE) - if (dirp->search_handle != INVALID_HANDLE_VALUE) { - if (FindClose (dirp->search_handle) == FALSE) { - /* Unknown error */ - errno = EBADF; - } - } -#endif - - /* re-open previous stream */ - if (_initdir (dirp) == 0) { - /* initialization failed but we cannot deal with error. User will notice - * error later when she tries to retrieve first directory enty. */ - /*EMPTY*/; - } -} - - -/* - * Open native directory stream object and retrieve first file. - * Be sure to close previous stream before opening new one. - */ -static int -_initdir (DIR *dirp) -{ - assert (dirp != NULL); - assert (dirp->dirname != NULL); - dirp->dirent_filled = 0; - -# if defined(DIRENT_WIN32_INTERFACE) - /* Open stream and retrieve first file */ - dirp->search_handle = FindFirstFile (dirp->dirname, &dirp->current.data); - if (dirp->search_handle == INVALID_HANDLE_VALUE) { - /* something went wrong but we don't know what. GetLastError() could - * give us more information about the error, but then we should map - * the error code into errno. */ - errno = ENOENT; - return 0; - } - -# elif defined(DIRENT_MSDOS_INTERFACE) - if (_dos_findfirst (dirp->dirname, - _A_SUBDIR | _A_RDONLY | _A_ARCH | _A_SYSTEM | _A_HIDDEN, - &dirp->current.data) != 0) - { - /* _dos_findfirst and findfirst will set errno to ENOENT when no - * more entries could be retrieved. */ - return 0; - } -# endif - - /* initialize DIR and it's first entry */ - _setdirname (dirp); - dirp->dirent_filled = 1; - return 1; -} - - -/* - * Return implementation dependent name of the current directory entry. - */ -static const char * -_getdirname (const struct dirent *dp) -{ -#if defined(DIRENT_WIN32_INTERFACE) - return dp->data.cFileName; - -#elif defined(DIRENT_USE_FFBLK) - return dp->data.ff_name; - -#else - return dp->data.name; -#endif -} - - -/* - * Copy name of implementation dependent directory entry to the d_name field. - */ -static void -_setdirname (struct DIR *dirp) { - /* make sure that d_name is long enough */ - assert (strlen (_getdirname (&dirp->current)) <= NAME_MAX); - - strncpy (dirp->current.d_name, - _getdirname (&dirp->current), - NAME_MAX); - dirp->current.d_name[NAME_MAX] = '\0'; /*char d_name[NAME_MAX+1]*/ -} - -# ifdef __cplusplus -} -# endif -# define NAMLEN(dp) ((int)(strlen((dp)->d_name))) - -#else -# error "missing dirent interface" -#endif - - -#endif /*DIRENT_H*/ diff --git a/jp3d/codec/getopt.c b/jp3d/codec/getopt.c deleted file mode 100755 index 69addc97..00000000 --- a/jp3d/codec/getopt.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 1987, 1993, 1994 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* last review : october 29th, 2002 */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; -#endif /* LIBC_SCCS and not lint */ - -#include -#include -#include - -int opterr = 1, /* if error message should be printed */ - optind = 1, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ - -#define BADCH (int)'?' -#define BADARG (int)':' -#define EMSG "" - -/* - * getopt -- - * Parse argc/argv argument vector. - */ -int getopt(int nargc, char *const *nargv, const char *ostr) { - - # define __progname nargv[0] /* program name */ - - static char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ - - if (optreset || !*place) { /* update scanning pointer */ - optreset = 0; - if (optind >= nargc || *(place = nargv[optind]) != '-') { - place = EMSG; - return (-1); - } - if (place[1] && *++place == '-') { /* found "--" */ - ++optind; - place = EMSG; - return (-1); - } - } /* option letter okay? */ - - if ((optopt = (int) *place++) == (int) ':' || !(oli = strchr(ostr, optopt))) { - /* if the user didn't specify '-' as an option, assume it means -1. */ - if (optopt == (int) '-') - return (-1); - if (!*place) - ++optind; - if (opterr && *ostr != ':') - (void) fprintf(stdout,"[ERROR] %s: illegal option -- %c\n", __progname, optopt); - return (BADCH); - } - - if (*++oli != ':') { /* don't need argument */ - optarg = NULL; - if (!*place) - ++optind; - } else { /* need an argument */ - if (*place) /* no white space */ - optarg = place; - else if (nargc <= ++optind) { /* no arg */ - place = EMSG; - if (*ostr == ':') - return (BADARG); - if (opterr) - (void) fprintf(stdout, "[ERROR] %s: option requires an argument -- %c\n", __progname, optopt); - return (BADCH); - } else /* white space */ - optarg = nargv[optind]; - place = EMSG; - ++optind; - } - return (optopt); /* dump back option letter */ -} diff --git a/jp3d/codec/getopt.h b/jp3d/codec/getopt.h deleted file mode 100755 index 23299d1b..00000000 --- a/jp3d/codec/getopt.h +++ /dev/null @@ -1,14 +0,0 @@ -/* last review : october 29th, 2002 */ - -#ifndef _GETOPT_H_ -#define _GETOPT_H_ - -extern int opterr; -extern int optind; -extern int optopt; -extern int optreset; -extern char *optarg; - -extern int getopt(int nargc, char *const *nargv, const char *ostr); - -#endif /* _GETOPT_H_ */ diff --git a/jp3d/codec/jp3d_to_volume.c b/jp3d/codec/jp3d_to_volume.c deleted file mode 100755 index 81dc1c78..00000000 --- a/jp3d/codec/jp3d_to_volume.c +++ /dev/null @@ -1,535 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include -#include -#include - -#include "openjpeg.h" -#include "getopt.h" -#include "convert.h" - -#ifndef WIN32 -#define stricmp strcasecmp -#define strnicmp strncasecmp -#endif - -/* ----------------------------------------------------------------------- */ -static double calc_PSNR(opj_volume_t *original, opj_volume_t *decoded) -{ - int max, i, k, compno = 0, size; - double sum, total = 0; - int global = 1; - - max = (original->comps[compno].prec <= 8) ? 255 : (1 << original->comps[compno].prec) - 1; - if (global) { - size = (original->x1 - original->x0) * (original->y1 - original->y0) * (original->z1 - original->z0); - - for (compno = 0; compno < original->numcomps; compno++) { - for(sum = 0, i = 0; i < size; ++i) { - if ((decoded->comps[compno].data[i] < 0) || (decoded->comps[compno].data[i] > max)) - fprintf(stdout,"[WARNING] Data out of range during PSNR computing...\n"); - else - sum += (original->comps[compno].data[i] - decoded->comps[compno].data[i]) * (original->comps[compno].data[i] - decoded->comps[compno].data[i]); - } - } - sum /= size; - total = ((sum==0.0) ? 0.0 : 10 * log10(max * max / sum)); - } else { - size = (original->x1 - original->x0) * (original->y1 - original->y0); - - for (k = 0; k < original->z1 - original->z0; k++) { - int offset = k * size; - for (sum = 0, compno = 0; compno < original->numcomps; compno++) { - for(i = 0; i < size; ++i) { - if ((decoded->comps[compno].data[i + offset] < 0) || (decoded->comps[compno].data[i + offset] > max)) - fprintf(stdout,"[WARNING] Data out of range during PSNR computing...\n"); - else - sum += (original->comps[compno].data[i + offset] - decoded->comps[compno].data[i + offset]) * (original->comps[compno].data[i + offset] - decoded->comps[compno].data[i + offset]); - } - } - sum /= size; - total = total + ((sum==0.0) ? 0.0 : 10 * log10(max * max / sum)); - } - - } - if(total == 0) /* perfect reconstruction, PSNR should return infinity */ - return -1.0; - - return total; - //return 20 * log10((max - 1) / sqrt(sum)); -} - -static double calc_SSIM(opj_volume_t *original, opj_volume_t *decoded) -{ - int max, i, compno = 0, size, sizeM; - double sum; - double mux = 0.0, muy = 0.0, sigmax = 0.0, sigmay = 0.0, - sigmaxy = 0.0, structx = 0.0, structy = 0.0; - double lcomp,ccomp,scomp; - double C1,C2,C3; - - max = (original->comps[compno].prec <= 8) ? 255 : (1 << original->comps[compno].prec) - 1; - size = (original->x1 - original->x0) * (original->y1 - original->y0) * (original->z1 - original->z0); - - //MSSIM - -// sizeM = size / (original->z1 - original->z0); - - sizeM = size; - for(sum = 0, i = 0; i < sizeM; ++i) { - // First, the luminance of each signal is compared. - mux += original->comps[compno].data[i]; - muy += decoded->comps[compno].data[i]; - } - mux /= sizeM; - muy /= sizeM; - - //We use the standard deviation (the square root of variance) as an estimate of the signal contrast. - for(sum = 0, i = 0; i < sizeM; ++i) { - // First, the luminance of each signal is compared. - sigmax += (original->comps[compno].data[i] - mux) * (original->comps[compno].data[i] - mux); - sigmay += (decoded->comps[compno].data[i] - muy) * (decoded->comps[compno].data[i] - muy); - sigmaxy += (original->comps[compno].data[i] - mux) * (decoded->comps[compno].data[i] - muy); - } - sigmax /= sizeM - 1; - sigmay /= sizeM - 1; - sigmaxy /= sizeM - 1; - - sigmax = sqrt(sigmax); - sigmay = sqrt(sigmay); - sigmaxy = sqrt(sigmaxy); - - //Third, the signal is normalized (divided) by its own standard deviation, - //so that the two signals being compared have unit standard deviation. - - //Luminance comparison - C1 = (0.01 * max) * (0.01 * max); - lcomp = ((2 * mux * muy) + C1)/((mux*mux) + (muy*mux) + C1); - //Constrast comparison - C2 = (0.03 * max) * (0.03 * max); - ccomp = ((2 * sigmax * sigmay) + C2)/((sigmax*sigmax) + (sigmay*sigmay) + C2); - //Structure comparison - C3 = C2 / 2; - scomp = (sigmaxy + C3) / (sigmax * sigmay + C3); - //Similarity measure - - sum = lcomp * ccomp * scomp; - return sum; -} - -void decode_help_display() { - fprintf(stdout,"HELP\n----\n\n"); - fprintf(stdout,"- the -h option displays this help information on screen\n\n"); - - fprintf(stdout,"List of parameters for the JPEG 2000 encoder:\n"); - fprintf(stdout,"\n"); - fprintf(stdout," Required arguments \n"); - fprintf(stdout," ---------------------------- \n"); - fprintf(stdout," -i ( *.jp3d, *.j3d )\n"); - fprintf(stdout," Currently accepts J3D-files. The file type is identified based on its suffix.\n"); - fprintf(stdout," -o ( *.pgx, *.bin )\n"); - fprintf(stdout," Currently accepts PGX-files and BIN-files. Binary data is written to the file (not ascii). \n"); - fprintf(stdout," If a PGX filename is given, there will be as many output files as slices; \n"); - fprintf(stdout," an indice starting from 0 will then be appended to the output filename,\n"); - fprintf(stdout," just before the \"pgx\" extension.\n"); - fprintf(stdout," -m ( *.img ) \n"); - fprintf(stdout," Required only for BIN-files. Ascii data of volume characteristics is written. \n"); - fprintf(stdout,"\n"); - fprintf(stdout," Optional \n"); - fprintf(stdout," ---------------------------- \n"); - fprintf(stdout," -h \n "); - fprintf(stdout," Display the help information\n"); - fprintf(stdout," -r \n"); - fprintf(stdout," Set the number of highest resolution levels to be discarded on each dimension. \n"); - fprintf(stdout," The volume resolution is effectively divided by 2 to the power of the\n"); - fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n"); - fprintf(stdout," smallest total number of decomposition levels among tiles.\n"); - fprintf(stdout," -l \n"); - fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n"); - fprintf(stdout," less quality layers than the specified number, all the quality layers\n"); - fprintf(stdout," are decoded. \n"); - fprintf(stdout," -O original-file \n"); - fprintf(stdout," This option offers the possibility to compute some quality results \n"); - fprintf(stdout," for the decompressed volume, like the PSNR value achieved or the global SSIM value. \n"); - fprintf(stdout," Needs the original file in order to compare with the new one.\n"); - fprintf(stdout," NOTE: Only valid when -r option is 0,0,0 (both original and decompressed volumes have same resolutions) \n"); - fprintf(stdout," NOTE: If original file is .BIN file, the volume characteristics file shall be defined with the -m option. \n"); - fprintf(stdout," (i.e. -O original-BIN-file -m original-IMG-file) \n"); - fprintf(stdout," -BE \n"); - fprintf(stdout," Define that the recovered volume data will be saved with big endian byte order.\n"); - fprintf(stdout," By default, little endian byte order is used.\n"); - fprintf(stdout,"\n"); -} - -/* -------------------------------------------------------------------------- */ - -int get_file_format(char *filename) { - int i; - static const char *extension[] = {"pgx", "bin", "j3d", "jp3d", "j2k", "img"}; - static const int format[] = { PGX_DFMT, BIN_DFMT, J3D_CFMT, J3D_CFMT, J2K_CFMT, IMG_DFMT}; - char * ext = strrchr(filename, '.') + 1; - for(i = 0; i < sizeof(format); i++) { - if(strnicmp(ext, extension[i], 3) == 0) { - return format[i]; - } - } - - return -1; -} - -/* -------------------------------------------------------------------------- */ - -int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters) { - /* parse the command line */ - - while (1) { - int c = getopt(argc, argv, "i:o:O:r:l:B:m:h"); - if (c == -1) - break; - switch (c) { - case 'i': /* input file */ - { - char *infile = optarg; - parameters->decod_format = get_file_format(infile); - switch(parameters->decod_format) { - case J3D_CFMT: - case J2K_CFMT: - break; - default: - fprintf(stdout, "[ERROR] Unknown format for infile %s [only *.j3d]!! \n", infile); - return 1; - break; - } - strncpy(parameters->infile, infile, MAX_PATH); - fprintf(stdout, "[INFO] Infile: %s \n", parameters->infile); - - } - break; - - case 'm': /* img file */ - { - char *imgfile = optarg; - int imgformat = get_file_format(imgfile); - switch(imgformat) { - case IMG_DFMT: - break; - default: - fprintf(stdout, "[ERROR] Unrecognized format for imgfile : %s [accept only *.img] !!\n\n", imgfile); - return 1; - break; - } - strncpy(parameters->imgfile, imgfile, MAX_PATH); - fprintf(stdout, "[INFO] Imgfile: %s Format: %d\n", parameters->imgfile, imgformat); - } - break; - - /* ----------------------------------------------------- */ - - case 'o': /* output file */ - { - char *outfile = optarg; - parameters->cod_format = get_file_format(outfile); - switch(parameters->cod_format) { - case PGX_DFMT: - case BIN_DFMT: - break; - default: - fprintf(stdout, "[ERROR] Unrecognized format for outfile : %s [accept only *.pgx or *.bin] !!\n\n", outfile); - return 1; - break; - } - strncpy(parameters->outfile, outfile, MAX_PATH); - fprintf(stdout, "[INFO] Outfile: %s \n", parameters->outfile); - - } - break; - - /* ----------------------------------------------------- */ - - case 'O': /* Original image for PSNR computing */ - { - char *original = optarg; - parameters->orig_format = get_file_format(original); - switch(parameters->orig_format) { - case PGX_DFMT: - case BIN_DFMT: - break; - default: - fprintf(stdout, "[ERROR] Unrecognized format for original file : %s [accept only *.pgx or *.bin] !!\n\n", original); - return 1; - break; - } - strncpy(parameters->original, original, MAX_PATH); - fprintf(stdout, "[INFO] Original file: %s \n", parameters->original); - } - break; - - /* ----------------------------------------------------- */ - - case 'r': /* reduce option */ - { - //sscanf(optarg, "%d, %d, %d", ¶meters->cp_reduce[0], ¶meters->cp_reduce[1], ¶meters->cp_reduce[2]); - int aux; - aux = sscanf(optarg, "%d,%d,%d", ¶meters->cp_reduce[0], ¶meters->cp_reduce[1], ¶meters->cp_reduce[2]); - if (aux == 2) - parameters->cp_reduce[2] = 0; - else if (aux == 1) { - parameters->cp_reduce[1] = parameters->cp_reduce[0]; - parameters->cp_reduce[2] = 0; - }else if (aux == 0){ - parameters->cp_reduce[0] = 0; - parameters->cp_reduce[1] = 0; - parameters->cp_reduce[2] = 0; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'l': /* layering option */ - { - sscanf(optarg, "%d", ¶meters->cp_layer); - } - break; - - /* ----------------------------------------------------- */ - - case 'B': /* BIGENDIAN vs. LITTLEENDIAN */ - { - parameters->bigendian = 1; - } - break; - - /* ----------------------------------------------------- */ - - case 'L': /* BIGENDIAN vs. LITTLEENDIAN */ - { - parameters->decod_format = LSE_CFMT; - } - break; - - /* ----------------------------------------------------- */ - - case 'h': /* display an help description */ - { - decode_help_display(); - return 1; - } - break; - - /* ----------------------------------------------------- */ - - default: - fprintf(stdout,"[WARNING] This option is not valid \"-%c %s\"\n",c, optarg); - break; - } - } - - /* check for possible errors */ - - if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { - fprintf(stdout,"[ERROR] At least one required argument is missing\n Check jp3d_to_volume -help for usage information\n"); - return 1; - } - - return 0; -} - -/* -------------------------------------------------------------------------- */ - -/** -sample error callback expecting a FILE* client object -*/ -void error_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[ERROR] %s", msg); -} -/** -sample warning callback expecting a FILE* client object -*/ -void warning_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[WARNING] %s", msg); -} -/** -sample debug callback expecting no client object -*/ -void info_callback(const char *msg, void *client_data) { - fprintf(stdout, "[INFO] %s", msg); -} - -/* -------------------------------------------------------------------------- */ - -int main(int argc, char **argv) { - - opj_dparameters_t parameters; /* decompression parameters */ - opj_event_mgr_t event_mgr; /* event manager */ - opj_volume_t *volume = NULL; - - opj_volume_t *original = NULL; - opj_cparameters_t cparameters; /* original parameters */ - - FILE *fsrc = NULL; - unsigned char *src = NULL; - int file_length; - int decodeok; - double psnr, ssim; - - opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */ - opj_cio_t *cio = NULL; - - /* configure the event callbacks (not required) */ - memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); - event_mgr.error_handler = error_callback; - event_mgr.warning_handler = warning_callback; - event_mgr.info_handler = info_callback; - - /* set decoding parameters to default values */ - opj_set_default_decoder_parameters(¶meters); - - /* parse input and get user decoding parameters */ - strcpy(parameters.original,"NULL"); - strcpy(parameters.imgfile,"NULL"); - if(parse_cmdline_decoder(argc, argv, ¶meters) == 1) { - return 0; - } - - /* read the input file and put it in memory */ - /* ---------------------------------------- */ - fprintf(stdout, "[INFO] Loading %s file \n",parameters.decod_format==J3D_CFMT ? ".jp3d" : ".j2k"); - fsrc = fopen(parameters.infile, "rb"); - if (!fsrc) { - fprintf(stdout, "[ERROR] Failed to open %s for reading\n", parameters.infile); - return 1; - } - fseek(fsrc, 0, SEEK_END); - file_length = ftell(fsrc); - fseek(fsrc, 0, SEEK_SET); - src = (unsigned char *) malloc(file_length); - fread(src, 1, file_length, fsrc); - fclose(fsrc); - - /* decode the code-stream */ - /* ---------------------- */ - if (parameters.decod_format == J3D_CFMT || parameters.decod_format == J2K_CFMT) { - /* get a JP3D or J2K decoder handle */ - if (parameters.decod_format == J3D_CFMT) - dinfo = opj_create_decompress(CODEC_J3D); - else if (parameters.decod_format == J2K_CFMT) - 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); - - /* 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, src, file_length); - - /* decode the stream and fill the volume structure */ - volume = opj_decode(dinfo, cio); - if(!volume) { - fprintf(stdout, "[ERROR] jp3d_to_volume: failed to decode volume!\n"); - opj_destroy_decompress(dinfo); - opj_cio_close(cio); - return 1; - } - - /* close the byte stream */ - opj_cio_close(cio); - } - - /* free the memory containing the code-stream */ - free(src); - src = NULL; - - /* create output volume */ - /* ------------------- */ - - switch (parameters.cod_format) { - case PGX_DFMT: /* PGX */ - decodeok = volumetopgx(volume, parameters.outfile); - if (decodeok) - fprintf(stdout,"[ERROR] Unable to write decoded volume into pgx files\n"); - break; - - case BIN_DFMT: /* BMP */ - decodeok = volumetobin(volume, parameters.outfile); - if (decodeok) - fprintf(stdout,"[ERROR] Unable to write decoded volume into pgx files\n"); - break; - } - switch (parameters.orig_format) { - case PGX_DFMT: /* PGX */ - if (strcmp("NULL",parameters.original) != 0){ - fprintf(stdout,"Loading original file %s \n",parameters.original); - cparameters.subsampling_dx = 1; cparameters.subsampling_dy = 1; cparameters.subsampling_dz = 1; - cparameters.volume_offset_x0 = 0;cparameters.volume_offset_y0 = 0;cparameters.volume_offset_z0 = 0; - original = pgxtovolume(parameters.original,&cparameters); - } - break; - - case BIN_DFMT: /* BMP */ - if (strcmp("NULL",parameters.original) != 0 && strcmp("NULL",parameters.imgfile) != 0){ - fprintf(stdout,"Loading original file %s %s\n",parameters.original,parameters.imgfile); - cparameters.subsampling_dx = 1; cparameters.subsampling_dy = 1; cparameters.subsampling_dz = 1; - cparameters.volume_offset_x0 = 0;cparameters.volume_offset_y0 = 0;cparameters.volume_offset_z0 = 0; - original = bintovolume(parameters.original,parameters.imgfile,&cparameters); - } - break; - } - - fprintf(stdout, "[RESULT] Volume: %d x %d x %d (x %d bpv)\n ", - (volume->comps[0].w >> volume->comps[0].factor[0]), - (volume->comps[0].h >> volume->comps[0].factor[1]), - (volume->comps[0].l >> volume->comps[0].factor[2]),volume->comps[0].prec); - - if(original){ - psnr = calc_PSNR(original,volume); - ssim = calc_SSIM(original,volume); - if (psnr < 0.0) - fprintf(stdout, " PSNR: Inf , SSMI %f -- Perfect reconstruction!\n",ssim); - else - fprintf(stdout, " PSNR: %f , SSIM %f \n",psnr,ssim); - } - /* free remaining structures */ - if(dinfo) { - opj_destroy_decompress(dinfo); - } - - /* free volume data structure */ - opj_volume_destroy(volume); - - return 0; -} - diff --git a/jp3d/codec/jp3d_vm_dec.ncb b/jp3d/codec/jp3d_vm_dec.ncb deleted file mode 100755 index fc66b661..00000000 Binary files a/jp3d/codec/jp3d_vm_dec.ncb and /dev/null differ diff --git a/jp3d/codec/jp3d_vm_dec.sln b/jp3d/codec/jp3d_vm_dec.sln deleted file mode 100755 index e696c5f7..00000000 --- a/jp3d/codec/jp3d_vm_dec.sln +++ /dev/null @@ -1,30 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 8.00 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jp3d_vm_dec", "jp3d_vm_dec.vcproj", "{E0C1B905-5B10-4C9A-AF55-2D8144D518AB}" - ProjectSection(ProjectDependencies) = postProject - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibJp3dVM", "..\LibJp3dVM.vcproj", "{6F3FB035-8F4E-4794-B091-0F0A20223BE7}" - ProjectSection(ProjectDependencies) = postProject - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfiguration) = preSolution - Debug = Debug - Release = Release - EndGlobalSection - GlobalSection(ProjectConfiguration) = postSolution - {E0C1B905-5B10-4C9A-AF55-2D8144D518AB}.Debug.ActiveCfg = Debug|Win32 - {E0C1B905-5B10-4C9A-AF55-2D8144D518AB}.Debug.Build.0 = Debug|Win32 - {E0C1B905-5B10-4C9A-AF55-2D8144D518AB}.Release.ActiveCfg = Release|Win32 - {E0C1B905-5B10-4C9A-AF55-2D8144D518AB}.Release.Build.0 = Release|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Debug.ActiveCfg = Debug|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Debug.Build.0 = Debug|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Release.ActiveCfg = Release|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Release.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - EndGlobalSection - GlobalSection(ExtensibilityAddIns) = postSolution - EndGlobalSection -EndGlobal diff --git a/jp3d/codec/jp3d_vm_dec.suo b/jp3d/codec/jp3d_vm_dec.suo deleted file mode 100755 index 28c2e255..00000000 Binary files a/jp3d/codec/jp3d_vm_dec.suo and /dev/null differ diff --git a/jp3d/codec/jp3d_vm_dec.vcproj b/jp3d/codec/jp3d_vm_dec.vcproj deleted file mode 100755 index 142c68d4..00000000 --- a/jp3d/codec/jp3d_vm_dec.vcproj +++ /dev/null @@ -1,157 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/jp3d/codec/jp3d_vm_enc.ncb b/jp3d/codec/jp3d_vm_enc.ncb deleted file mode 100755 index 7f0733b7..00000000 Binary files a/jp3d/codec/jp3d_vm_enc.ncb and /dev/null differ diff --git a/jp3d/codec/jp3d_vm_enc.sln b/jp3d/codec/jp3d_vm_enc.sln deleted file mode 100755 index a0e6a748..00000000 --- a/jp3d/codec/jp3d_vm_enc.sln +++ /dev/null @@ -1,35 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 8.00 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jp3d_vm_enc", "jp3d_vm_enc.vcproj", "{A9704A2E-3B93-4BAA-9229-02FC93D27201}" - ProjectSection(ProjectDependencies) = postProject - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibJp3dVM", "..\LibJp3dVM.vcproj", "{6F3FB035-8F4E-4794-B091-0F0A20223BE7}" - ProjectSection(ProjectDependencies) = postProject - {6F3FB035-8F4E-4794-B091-0F0A20223BE7} = {6F3FB035-8F4E-4794-B091-0F0A20223BE7} - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfiguration) = preSolution - Debug = Debug - Release = Release - EndGlobalSection - GlobalSection(ProjectConfiguration) = postSolution - {A9704A2E-3B93-4BAA-9229-02FC93D27201}.Debug.ActiveCfg = Debug|Win32 - {A9704A2E-3B93-4BAA-9229-02FC93D27201}.Debug.Build.0 = Debug|Win32 - {A9704A2E-3B93-4BAA-9229-02FC93D27201}.Release.ActiveCfg = Release|Win32 - {A9704A2E-3B93-4BAA-9229-02FC93D27201}.Release.Build.0 = Release|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Debug.ActiveCfg = Debug|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Debug.Build.0 = Debug|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Release.ActiveCfg = Release|Win32 - {6F3FB035-8F4E-4794-B091-0F0A20223BE7}.Release.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - EndGlobalSection - GlobalSection(ExtensibilityAddIns) = postSolution - EndGlobalSection -EndGlobal diff --git a/jp3d/codec/jp3d_vm_enc.suo b/jp3d/codec/jp3d_vm_enc.suo deleted file mode 100755 index 702d0dd8..00000000 Binary files a/jp3d/codec/jp3d_vm_enc.suo and /dev/null differ diff --git a/jp3d/codec/jp3d_vm_enc.vcproj b/jp3d/codec/jp3d_vm_enc.vcproj deleted file mode 100755 index 4c24b352..00000000 --- a/jp3d/codec/jp3d_vm_enc.vcproj +++ /dev/null @@ -1,157 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/jp3d/codec/volume_to_jp3d.c b/jp3d/codec/volume_to_jp3d.c deleted file mode 100755 index 84f01e83..00000000 --- a/jp3d/codec/volume_to_jp3d.c +++ /dev/null @@ -1,903 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include -#include - -#include "openjpeg.h" -#include "getopt.h" -#include "convert.h" - -#ifndef WIN32 -#define stricmp strcasecmp -#define strnicmp strncasecmp -#endif - -/* ----------------------------------------------------------------------- */ - -void encode_help_display() { - fprintf(stdout,"List of parameters for the JPEG2000 Part 10 encoder:\n"); - fprintf(stdout,"------------\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"Required Parameters (except with -h):\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-i : source file (-i source.bin or source*.pgx) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-m : source characteristics file (-m imgfile.img) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-o : destination file (-o dest.jp3d) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"Optional Parameters:\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-h : display the help information \n "); - fprintf(stdout,"\n"); - fprintf(stdout,"-n : number of resolutions (-n 3,3,3) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-I : use the irreversible transforms: ICT + DWT 9-7 (-I) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-C : coding algorithm (-C 2EB) [2EB, 3EB] \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n "); - fprintf(stdout," - The rate specified for each quality level is the desired compression factor.\n"); - fprintf(stdout," - Rate 1 means lossless compression\n"); - fprintf(stdout," (options -r and -q cannot be used together)\n "); - fprintf(stdout,"\n"); - fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n "); - fprintf(stdout," (options -r and -q cannot be used together)\n "); - fprintf(stdout,"\n"); - fprintf(stdout,"-b : size of code block (-b 32,32,32) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-c : size of precinct (-c 128,128,128) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-t : size of tile (-t 512,512,512) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-s : subsampling factor (-s 2,2,2) [-s X,Y,Z] \n"); - fprintf(stdout," - Remark: subsampling bigger than 2 can produce error\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-SOP : write SOP marker before each packet \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-EPH : write EPH marker after each header packet \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-M : code-block style (-M 0) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n"); - fprintf(stdout," 8=VSC 16=PTERM 32=SEGSYM 64=3DCTXT] \n"); - fprintf(stdout," Indicate multiple modes by adding their values. \n"); - fprintf(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-D : define DC offset (-D 12) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n"); - fprintf(stdout," for component c=%%d [%%d = 0,1,2]\n"); - fprintf(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-d : offset of the origin of the volume (-d 150,300,100) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-l : offset of the origin of the tiles (-l 100,75,25) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"DEFAULT CODING:\n"); - fprintf(stdout,"------------\n"); - fprintf(stdout,"\n"); - fprintf(stdout," * Lossless\n"); - fprintf(stdout," * 1 tile\n"); - fprintf(stdout," * Size of precinct : 2^15 x 2^15 x 2^15 (means 1 precinct)\n"); - fprintf(stdout," * Size of code-block : 64 x 64 x 64\n"); - fprintf(stdout," * Number of resolutions in x, y and z axis: 3\n"); - fprintf(stdout," * No SOP marker in the codestream\n"); - fprintf(stdout," * No EPH marker in the codestream\n"); - fprintf(stdout," * No sub-sampling in x, y or z direction\n"); - fprintf(stdout," * No mode switch activated\n"); - fprintf(stdout," * Progression order: LRCP\n"); - fprintf(stdout," * No index file\n"); - fprintf(stdout," * No ROI upshifted\n"); - fprintf(stdout," * No offset of the origin of the volume\n"); - fprintf(stdout," * No offset of the origin of the tiles\n"); - fprintf(stdout," * Reversible DWT 5-3 on each 2D slice\n"); - fprintf(stdout," * Coding algorithm: 2D-EBCOT \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"REMARKS:\n"); - fprintf(stdout,"---------\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"- The markers written to the main_header are : SOC SIZ COD QCD COM.\n"); - fprintf(stdout,"- COD and QCD markers will never appear in the tile_header.\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"- You need enough disk space memory (twice the original) to encode \n"); - fprintf(stdout,"the volume,i.e. for a 1.5 GB volume you need a minimum of 3GB of disk memory)\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"- When loading *.pgx files, a relative path to directory is needed for input argument \n"); - fprintf(stdout," followed by the common prefix of the slices and a '*' character representing sequential numeration.\n"); - fprintf(stdout,"( -i relativepath/slices*.pgx )\n"); - fprintf(stdout,"\n"); - fprintf(stdout," - The index file has the structure below:\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"\t Image_height Image_width Image_depth\n"); - fprintf(stdout,"\t Progression order: 0 (LRCP)\n"); - fprintf(stdout,"\t Tiles_size_X Tiles_size_Y Tiles_size_Z\n"); - fprintf(stdout,"\t Components_nb\n"); - fprintf(stdout,"\t Layers_nb\n"); - fprintf(stdout,"\t Decomposition_levels\n"); - fprintf(stdout,"\t [Precincts_size_X_res_Nr Precincts_size_Y_res_Nr Precincts_size_Z_res_Nr]\n\t ...\n"); - fprintf(stdout,"\t [Precincts_size_X_res_0 Precincts_size_Y_res_0 Precincts_size_Z_res_0]\n"); - fprintf(stdout,"\t Main_header_end_position\n"); - fprintf(stdout,"\t Codestream_size\n"); - fprintf(stdout,"\t Tile_0 [start_pos end_header end_pos TotalDisto NumPix MaxMSE]\n"); - fprintf(stdout,"\t ...\n"); - fprintf(stdout,"\t Tile_Nt [ '' '' '' '' '' '' ]\n"); - fprintf(stdout,"\t Tpacket_0 [Tile layer res. comp. prec. start_pos end_pos disto]\n"); - fprintf(stdout,"\t ...\n"); - fprintf(stdout,"\t Tpacket_Np ['' '' '' '' '' '' '' '' ]\n"); - fprintf(stdout,"\t MaxDisto\n"); - fprintf(stdout,"\t TotalDisto\n\n"); - fprintf(stdout,"\n"); - -} - -OPJ_PROG_ORDER give_progression(char progression[4]) { - if(strncmp(progression, "LRCP", 4) == 0) { - return LRCP; - } - if(strncmp(progression, "RLCP", 4) == 0) { - return RLCP; - } - if(strncmp(progression, "RPCL", 4) == 0) { - return RPCL; - } - if(strncmp(progression, "PCRL", 4) == 0) { - return PCRL; - } - if(strncmp(progression, "CPRL", 4) == 0) { - return CPRL; - } - - return PROG_UNKNOWN; -} - -OPJ_TRANSFORM give_transform(char transform[4]) { - if(strncmp(transform, "2DWT", 4) == 0) { - return TRF_2D_DWT; - } - if(strncmp(transform, "3DWT", 4) == 0) { - return TRF_3D_DWT; - } - return TRF_UNKNOWN; -} - -OPJ_ENTROPY_CODING give_coding(char coding[3]) { - - if(strncmp(coding, "2EB", 3) == 0) { - return ENCOD_2EB; - } - if(strncmp(coding, "3EB", 3) == 0) { - return ENCOD_3EB; - } - /*if(strncmp(coding, "2GR", 3) == 0) { - return ENCOD_2GR; - } - if(strncmp(coding, "3GR", 3) == 0) { - return ENCOD_3GR; - }*/ - - return ENCOD_UNKNOWN; -} - -int get_file_format(char *filename) { - int i; - static const char *extension[] = {"pgx", "bin", "img", "j3d", "jp3d", "j2k"}; - static const int format[] = { PGX_DFMT, BIN_DFMT, IMG_DFMT, J3D_CFMT, J3D_CFMT, J2K_CFMT}; - char * ext = strrchr(filename, '.') + 1; - if (ext) { - for(i = 0; i < sizeof(format)/sizeof(*format); i++) { - if(strnicmp(ext, extension[i], 3) == 0) { - return format[i]; - } - } - } - - return -1; -} - -/* ------------------------------------------------------------------------------------ */ - -int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters) { - int i, value; - - /* parse the command line */ - - while (1) { - int c = getopt(argc, argv, "i:m:o:r:q:f:t:n:c:b:x:p:s:d:hP:S:E:M:D:R:l:T:C:A:I"); - if (c == -1) - break; - switch (c) { - case 'i': /* input file */ - { - char *infile = optarg; - parameters->decod_format = get_file_format(infile); - switch(parameters->decod_format) { - case PGX_DFMT: - case BIN_DFMT: - case IMG_DFMT: - break; - default: - fprintf(stdout, "[ERROR] Unrecognized format for infile : %s [accept only *.pgx or *.bin] !!\n\n", infile); - return 1; - break; - } - strncpy(parameters->infile, infile, MAX_PATH); - fprintf(stdout, "[INFO] Infile: %s \n", parameters->infile); - - } - break; - - /* ----------------------------------------------------- */ - case 'm': /* input IMG file */ - { - char *imgfile = optarg; - int imgformat = get_file_format(imgfile); - switch(imgformat) { - case IMG_DFMT: - break; - default: - fprintf(stdout, "[ERROR] Unrecognized format for imgfile : %s [accept only *.img] !!\n\n", imgfile); - return 1; - break; - } - strncpy(parameters->imgfile, imgfile, MAX_PATH); - fprintf(stdout, "[INFO] Imgfile: %s Format: %d\n", parameters->imgfile, imgformat); - } - break; - - /* ----------------------------------------------------- */ - case 'o': /* output file */ - { - char *outfile = optarg; - parameters->cod_format = get_file_format(outfile); - switch(parameters->cod_format) { - case J3D_CFMT: - case J2K_CFMT: - case LSE_CFMT: - break; - default: - fprintf(stdout, "[ERROR] Unknown output format volume %s [only *.j2k, *.lse3d or *.jp3d]!! \n", outfile); - return 1; - break; - } - strncpy(parameters->outfile, outfile, MAX_PATH); - fprintf(stdout, "[INFO] Outfile: %s \n", parameters->outfile); - } - break; - - /* ----------------------------------------------------- */ - - case 'r': /* define compression rates for each layer */ - { - char *s = optarg; - while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) == 1) { - parameters->tcp_numlayers++; - while (*s && *s != ',') { - s++; - } - if (!*s) - break; - s++; - } - parameters->cp_disto_alloc = 1; - } - break; - - /* ----------------------------------------------------- */ - - case 'q': /* define distorsion (PSNR) for each layer */ - { - char *s = optarg; - while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers]) == 1) { - parameters->tcp_numlayers++; - while (*s && *s != ',') { - s++; - } - if (!*s) - break; - s++; - } - parameters->cp_fixed_quality = 1; - } - break; - - /* ----------------------------------------------------- */ - - case 'f': - { - fprintf(stdout, "/---------------------------------------------------\\\n"); - fprintf(stdout, "| Fixed layer allocation option not implemented !! |\n"); - fprintf(stdout, "\\---------------------------------------------------/\n"); - /*int *row = NULL, *col = NULL; - int numlayers = 0, matrix_width = 0; - - char *s = optarg; - sscanf(s, "%d", &numlayers); - s++; - if (numlayers > 9) - s++; - - parameters->tcp_numlayers = numlayers; - matrix_width = parameters->numresolution[0] + parameters->numresolution[1] + parameters->numresolution[2]; - parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int)); - s = s + 2; - - for (i = 0; i < numlayers; i++) { - row = ¶meters->cp_matrice[i * matrix_width]; - col = row; - parameters->tcp_rates[i] = 1; - sscanf(s, "%d,", &col[0]); - s += 2; - if (col[0] > 9) - s++; - col[1] = 0; - col[2] = 0; - for (j = 1; j < matrix_width; j++) { - col += 3; j+=2; - sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]); - s += 6; - if (col[0] > 9) - s++; - if (col[1] > 9) - s++; - if (col[2] > 9) - s++; - } - if (i < numlayers - 1) - s++; - } - parameters->cp_fixed_alloc = 1; */ - } - break; - - /* ----------------------------------------------------- */ - - case 't': /* tiles */ - { - if (sscanf(optarg, "%d,%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy, ¶meters->cp_tdz) !=3) { - fprintf(stdout, "[ERROR] '-t' 'dimensions of tiles' argument error ! [-t tdx,tdy,tdz]\n"); - return 1; - } - parameters->tile_size_on = true; - } - break; - - /* ----------------------------------------------------- */ - - case 'n': /* resolution */ - { - int aux; - aux = sscanf(optarg, "%d,%d,%d", ¶meters->numresolution[0], ¶meters->numresolution[1], ¶meters->numresolution[2]); - if (aux == 2) - parameters->numresolution[2] = 1; - else if (aux == 1) { - parameters->numresolution[1] = parameters->numresolution[0]; - parameters->numresolution[2] = 1; - }else if (aux == 0){ - parameters->numresolution[0] = 1; - parameters->numresolution[1] = 1; - parameters->numresolution[2] = 1; - } - } - break; - - /* ----------------------------------------------------- */ - case 'c': /* precinct dimension */ - { - char sep; - int res_spec = 0; - int aux; - char *s = optarg; - do { - sep = 0; - aux = sscanf(s, "[%d,%d,%d]%c", ¶meters->prct_init[0][res_spec], ¶meters->prct_init[1][res_spec], ¶meters->prct_init[2][res_spec], &sep); - if (sep == ',' && aux != 4) { - fprintf(stdout, "[ERROR] '-c' 'dimensions of precincts' argument error ! [-c [prcx_res0,prcy_res0,prcz_res0],...,[prcx_resN,prcy_resN,prcz_resN]]\n"); - return 1; - } - parameters->csty |= 0x01; - res_spec++; - s = strpbrk(s, "]") + 2; - } - while (sep == ','); - parameters->res_spec = res_spec; /* number of precinct size specifications */ - } - break; - - /* ----------------------------------------------------- */ - - case 'b': /* code-block dimension */ - { - int cblockw_init = 0, cblockh_init = 0, cblockl_init = 0; - if (sscanf(optarg, "%d,%d,%d", &cblockw_init, &cblockh_init, &cblockl_init) != 3) { - fprintf(stdout, "[ERROR] '-b' 'dimensions of codeblocks' argument error ! [-b cblkx,cblky,cblkz]\n"); - return 1; - } - if (cblockw_init * cblockh_init * cblockl_init > (1<<18) || cblockw_init > 1024 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4 || cblockl_init > 1024 || cblockl_init < 4) { - fprintf(stdout,"[ERROR] Size of code_block error (option -b) !!\n\nRestriction :\n * width*height*length<=4096\n * 4<=width,height,length<= 1024\n\n"); - return 1; - } - parameters->cblock_init[0] = cblockw_init; - parameters->cblock_init[1] = cblockh_init; - parameters->cblock_init[2] = cblockl_init; - } - break; - - /* ----------------------------------------------------- */ - - case 'x': /* creation of index file */ - { - char *index = optarg; - strncpy(parameters->index, index, MAX_PATH); - parameters->index_on = 1; - } - break; - - /* ----------------------------------------------------- */ - - case 'p': /* progression order */ - { - char progression[4]; - - strncpy(progression, optarg, 4); - parameters->prog_order = give_progression(progression); - if (parameters->prog_order == -1) { - fprintf(stdout, "[ERROR] Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 's': /* subsampling factor */ - { - if (sscanf(optarg, "%d,%d,%d", ¶meters->subsampling_dx, ¶meters->subsampling_dy, ¶meters->subsampling_dz) != 2) { - fprintf(stdout, "[ERROR] '-s' sub-sampling argument error ! [-s dx,dy,dz]\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'd': /* coordonnate of the reference grid */ - { - if (sscanf(optarg, "%d,%d,%d", ¶meters->volume_offset_x0, ¶meters->volume_offset_y0, ¶meters->volume_offset_z0) != 3) { - fprintf(stdout, "[ERROR] -d 'coordonnate of the reference grid' argument error !! [-d x0,y0,z0]\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'h': /* display an help description */ - { - encode_help_display(); - return 1; - } - break; - - /* ----------------------------------------------------- */ - - case 'P': /* POC */ - { - int numpocs = 0; /* number of progression order change (POC) default 0 */ - opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */ - - char *s = optarg; - POC = parameters->POC; - - fprintf(stdout, "/----------------------------------\\\n"); - fprintf(stdout, "| POC option not fully tested !! |\n"); - fprintf(stdout, "\\----------------------------------/\n"); - - while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile, - &POC[numpocs].resno0, &POC[numpocs].compno0, - &POC[numpocs].layno1, &POC[numpocs].resno1, - &POC[numpocs].compno1, POC[numpocs].progorder) == 7) { - POC[numpocs].prg = give_progression(POC[numpocs].progorder); - /* POC[numpocs].tile; */ - numpocs++; - while (*s && *s != '/') { - s++; - } - if (!*s) { - break; - } - s++; - } - parameters->numpocs = numpocs; - } - break; - - /* ------------------------------------------------------ */ - - case 'S': /* SOP marker */ - { - parameters->csty |= 0x02; - } - break; - - /* ------------------------------------------------------ */ - - case 'E': /* EPH marker */ - { - parameters->csty |= 0x04; - } - break; - - /* ------------------------------------------------------ */ - - case 'M': /* Codification mode switch */ - { - fprintf(stdout, "[INFO] Mode switch option not fully tested !!\n"); - value = 0; - if (sscanf(optarg, "%d", &value) == 1) { - for (i = 0; i <= 6; i++) { - int cache = value & (1 << i); - if (cache) - parameters->mode |= (1 << i); - } - } - } - break; - - /* ------------------------------------------------------ */ - - case 'D': /* DCO */ - { - if (sscanf(optarg, "%d", ¶meters->dcoffset) != 1) { - fprintf(stdout, "[ERROR] DC offset error !! [-D %d]\n",parameters->dcoffset); - return 1; - } - } - break; - - /* ------------------------------------------------------ */ - - case 'R': /* ROI */ - { - if (sscanf(optarg, "OI:c=%d,U=%d", ¶meters->roi_compno, ¶meters->roi_shift) != 2) { - fprintf(stdout, "[ERROR] ROI error !! [-ROI:c='compno',U='shift']\n"); - return 1; - } - } - break; - - /* ------------------------------------------------------ */ - - case 'l': /* Tile offset */ - { - if (sscanf(optarg, "%d,%d,%d", ¶meters->cp_tx0, ¶meters->cp_ty0, ¶meters->cp_tz0) != 3) { - fprintf(stdout, "[ERROR] -l 'tile offset' argument error !! [-l X0,Y0,Z0]"); - return 1; - } - } - break; - - /* ------------------------------------------------------ - - case 'T': // Tranformation of original data (2D-DWT/3D-DWT/3D-RLS/2D-DWT+1D-RLS) - { - char transform[4]; - - strncpy(transform, optarg, 4); - parameters->transform_format = give_transform(transform); - if (parameters->transform_format == -1) { - fprintf(stdout, "[ERROR] -T 'Transform domain' argument error !! [-T 2DWT, 3DWT, 3RLS or 3LSE only]"); - return 1; - } - } - break; - - ------------------------------------------------------ */ - - case 'C': /* Coding of transformed data */ - { - char coding[3]; - - strncpy(coding, optarg, 3); - parameters->encoding_format = give_coding(coding); - if (parameters->encoding_format == -1) { - fprintf(stdout, "[ERROR] -C 'Coding algorithm' argument error !! [-C 2EB, 3EB, 2GR, 3GR or GRI only]"); - return 1; - } - } - break; - - /* ------------------------------------------------------ */ - - case 'I': /* reversible or not */ - { - parameters->irreversible = 1; - } - break; - - default: - fprintf(stdout, "[ERROR] This option is not valid \"-%c %s\"\n", c, optarg); - return 1; - } - } - - /* check for possible errors */ - - if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { - fprintf(stdout, "usage: jp3d_vm_enc -i volume-file -o jp3d-file (+ options)\n"); - return 1; - } - - if((parameters->decod_format == BIN_DFMT) && (parameters->imgfile[0] == 0)) { - fprintf(stdout, "usage: jp3d_vm_enc -i bin-volume-file -m img-file -o jp3d-file (+ options)\n"); - return 1; - } - - if((parameters->decod_format != BIN_DFMT) && (parameters->decod_format != PGX_DFMT) && (parameters->decod_format != IMG_DFMT)) { - fprintf(stdout, "usage: jp3d_vm_enc -i input-volume-file [*.bin,*.pgx,*.img] -o jp3d-file [*.jp3d,*.j2k] (+ options)\n"); - return 1; - } - if((parameters->cod_format != J3D_CFMT) && (parameters->cod_format != J2K_CFMT)) { - fprintf(stdout, "usage: jp3d_vm_enc -i input-volume-file [*.bin,*.pgx,*.img] -o jp3d-file [*.jp3d,*.j2k] (+ options)\n"); - return 1; - } - - if((parameters->encoding_format == ENCOD_2GR || parameters->encoding_format == ENCOD_3GR) && parameters->transform_format != TRF_3D_LSE && parameters->transform_format != TRF_3D_RLS) { - fprintf(stdout, "[ERROR] Entropy coding options -C [2GR,3GR] are only compatible with predictive-based transform algorithms: -T [3RLS,3LSE].\n"); - return 1; - } - if (parameters->encoding_format == ENCOD_3EB) - parameters->mode |= (1 << 6); - - if ((parameters->mode >> 6) & 1) { - parameters->encoding_format = ENCOD_3EB; - } - - if((parameters->numresolution[2] == 0 || (parameters->numresolution[1] == 0) || (parameters->numresolution[0] == 0))) { - fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions must be greater than 1 in order to perform DWT.\n"); - return 1; - } - if (parameters->numresolution[1] != parameters->numresolution[0]) { - fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions in X and Y axis must be the same in this implementation.\n"); - return 1; - } - - if (parameters->numresolution[2] > parameters->numresolution[0]) { - fprintf(stdout, "[ERROR] -n 'resolution levels' argument error ! Resolutions in Z axis must be lower than in X-Y axis.\n"); - return 1; - } - - if (parameters->dcoffset >= 128 && parameters->dcoffset <= -128) { - fprintf(stdout, "[ERROR] -D 'DC offset' argument error ! Value must be -128<=DCO<=128.\n"); - return 1; - } - - if(parameters->numresolution[2] != 1) { - parameters->transform_format = TRF_3D_DWT; - //fprintf(stdout, "[Warning] Resolution level in axial dim > 1 : 3D-DWT will be performed... \n"); - } else if (parameters->numresolution[2] == 1) { - parameters->transform_format = TRF_2D_DWT; - //fprintf(stdout, "[Warning] Resolution level in axial dim == 1 : 2D-DWT will be performed... \n"); - } - - if ((parameters->cod_format == J2K_CFMT) && (parameters->transform_format != TRF_2D_DWT || parameters->encoding_format != ENCOD_2EB)) { - fprintf(stdout, "[WARNING] Incompatible options -o *.j2k and defined transform or encoding algorithm. Latter will be ignored\n"); - parameters->transform_format = TRF_2D_DWT; - parameters->encoding_format = ENCOD_2EB; - } - - if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality) && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_quality))) { - fprintf(stdout, "[ERROR] Options -r and -q cannot be used together !!\n"); - return 1; - } /* mod fixed_quality */ - - /* if no rate entered, lossless by default */ - if (parameters->tcp_numlayers == 0) { - parameters->tcp_rates[0] = 0.0; /* MOD antonin : losslessbug */ - parameters->tcp_numlayers++; - parameters->cp_disto_alloc = 1; - } - - if((parameters->cp_tx0 > parameters->volume_offset_x0) || (parameters->cp_ty0 > parameters->volume_offset_y0) || (parameters->cp_tz0 > parameters->volume_offset_z0)) { - fprintf(stdout, "[ERROR] Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) TZO(%d)<=IMG_Z0(%d)\n", - parameters->cp_tx0, parameters->volume_offset_x0, parameters->cp_ty0, parameters->volume_offset_y0, - parameters->cp_tz0, parameters->volume_offset_z0); - return 1; - } - - for (i = 0; i < parameters->numpocs; i++) { - if (parameters->POC[i].prg == -1) { - fprintf(stdout,"[ERROR] Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",i + 1); - } - } - return 0; -} - -/* -------------------------------------------------------------------------- */ - -/** -sample error callback expecting a FILE* client object -*/ -void error_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[ERROR] %s", msg); -} -/** -sample warning callback expecting a FILE* client object -*/ -void warning_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[WARNING] %s", msg); -} -/** -sample debug callback expecting a FILE* client object -*/ -void info_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[INFO] %s", msg); -} - -/* -------------------------------------------------------------------------- */ - -int main(int argc, char **argv) { - bool bSuccess; - bool delete_comment = true; - opj_cparameters_t parameters; /* compression parameters */ - opj_event_mgr_t event_mgr; /* event manager */ - opj_volume_t *volume = NULL; - - /* - configure the event callbacks (not required) - setting of each callback is optionnal - */ - memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); - event_mgr.error_handler = error_callback; - event_mgr.warning_handler = warning_callback; - event_mgr.info_handler = info_callback; - - /* set encoding parameters to default values */ - opj_set_default_encoder_parameters(¶meters); - - /* parse input and get user encoding parameters */ - if(parse_cmdline_encoder(argc, argv, ¶meters) == 1) { - return 0; - } - - if(parameters.cp_comment == NULL) { - parameters.cp_comment = "Created by OpenJPEG version JP3D"; - /* no need to delete parameters.cp_comment on exit */ - delete_comment = false; - } - - /* encode the destination volume */ - /* ---------------------------- */ - if (parameters.cod_format == J3D_CFMT || parameters.cod_format == J2K_CFMT) { - int codestream_length, pixels, bitsin; - opj_cio_t *cio = NULL; - FILE *f = NULL; - opj_cinfo_t* cinfo = NULL; - - /* decode the source volume */ - /* ----------------------- */ - switch (parameters.decod_format) { - case PGX_DFMT: - fprintf(stdout, "[INFO] Loading pgx file(s)\n"); - volume = pgxtovolume(parameters.infile, ¶meters); - if (!volume) { - fprintf(stdout, "[ERROR] Unable to load pgx files\n"); - return 1; - } - break; - - case BIN_DFMT: - fprintf(stdout, "[INFO] Loading bin file\n"); - volume = bintovolume(parameters.infile, parameters.imgfile, ¶meters); - if (!volume) { - fprintf(stdout, "[ERROR] Unable to load bin file\n"); - return 1; - } - break; - - case IMG_DFMT: - fprintf(stdout, "[INFO] Loading img file\n"); - volume = imgtovolume(parameters.infile, ¶meters); - if (!volume) { - fprintf(stderr, "[ERROR] Unable to load img file\n"); - return 1; - } - break; - } - - /* get a JP3D or J2K compressor handle */ - if (parameters.cod_format == J3D_CFMT) - cinfo = opj_create_compress(CODEC_J3D); - else if (parameters.cod_format == J2K_CFMT) - cinfo = opj_create_compress(CODEC_J2K); - - /* catch events using our callbacks and give a local context */ - opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stdout); - - /* setup the encoder parameters using the current volume and using user parameters */ - opj_setup_encoder(cinfo, ¶meters, volume); - - /* open a byte stream for writing */ - /* allocate memory for all tiles */ - cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); - - /* encode the volume */ - //fprintf(stdout, "[INFO] Encode the volume\n"); - bSuccess = opj_encode(cinfo, cio, volume, parameters.index); - if (!bSuccess) { - opj_cio_close(cio); - fprintf(stdout, "[ERROR] Failed to encode volume\n"); - return 1; - } - codestream_length = cio_tell(cio); - pixels =(volume->x1 - volume->x0) * (volume->y1 - volume->y0) * (volume->z1 - volume->z0); - bitsin = pixels * volume->comps[0].prec; - fprintf(stdout, "[RESULT] Volume: %d x %d x %d (x %d bpv)\n Codestream: %d B, Ratio: %5.3f bpv, (%5.3f : 1) \n", - (volume->x1 - volume->x0),(volume->y1 - volume->y0),(volume->z1 - volume->z0),volume->comps[0].prec, - codestream_length, ((double)codestream_length * 8.0/(double)pixels), ((double)bitsin/(8.0*(double)codestream_length))); - - /* write the buffer to disk */ - f = fopen(parameters.outfile, "wb"); - if (!f) { - fprintf(stdout, "[ERROR] Failed to open %s for writing\n", parameters.outfile); - return 1; - } - fwrite(cio->buffer, 1, codestream_length, f); - fclose(f); - - /* close and free the byte stream */ - opj_cio_close(cio); - - /* free remaining compression structures */ - opj_destroy_compress(cinfo); - } else { - fprintf(stdout, "[ERROR] Cod_format != JP3d !!! \n"); - return 1; - } - - /* free user parameters structure */ - if(delete_comment) { - if(parameters.cp_comment) free(parameters.cp_comment); - } - if(parameters.cp_matrice) free(parameters.cp_matrice); - - /* free volume data */ - opj_volume_destroy(volume); - - return 0; -} diff --git a/jp3d/libjp3dvm/CMakeLists.txt b/jp3d/libjp3dvm/CMakeLists.txt deleted file mode 100644 index 17a094af..00000000 --- a/jp3d/libjp3dvm/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -INCLUDE_REGULAR_EXPRESSION("^.*$") -# Defines the source code for the library -SET(JP3DVM_SRCS -bio.c cio.c dwt.c event.c jp3d.c jp3d_lib.c mct.c mqc.c openjpeg.c pi.c raw.c t1.c t1_3d.c t2.c tcd.c tgt.c volume.c -) - -# Pass proper definition to preprocessor to generate shared lib -IF(WIN32) - IF(BUILD_SHARED_LIBS) - ADD_DEFINITIONS(-DOPJ_EXPORTS) - ELSE(BUILD_SHARED_LIBS) - ADD_DEFINITIONS(-DOPJ_STATIC) - ENDIF(BUILD_SHARED_LIBS) -ENDIF(WIN32) - -# Create the library -#ADD_LIBRARY(${OPENJPEG_LIBRARY_NAME} ${OPENJPEG_SRCS}) -ADD_LIBRARY(${OPJ_PREFIX}openjp3dvm ${JP3DVM_SRCS}) - -# Install library -INSTALL_TARGETS(/lib/ ${OPJ_PREFIX}openjp3dvm) - -# Install includes files -INSTALL_FILES(/include .h) diff --git a/jp3d/libjp3dvm/bio.c b/jp3d/libjp3dvm/bio.c deleted file mode 100755 index 3ac93158..00000000 --- a/jp3d/libjp3dvm/bio.c +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup BIO BIO - Individual bit input-output stream */ -/*@{*/ - -/** @name Local static functions */ -/*@{*/ - -/** -Write a bit -@param bio BIO handle -@param b Bit to write (0 or 1) -*/ -static void bio_putbit(opj_bio_t *bio, int b); -/** -Read a bit -@param bio BIO handle -@return Returns the read bit -*/ -static int bio_getbit(opj_bio_t *bio); -/** -Write a byte -@param bio BIO handle -@return Returns 0 if successful, returns 1 otherwise -*/ -static int bio_byteout(opj_bio_t *bio); -/** -Read a byte -@param bio BIO handle -@return Returns 0 if successful, returns 1 otherwise -*/ -static int bio_bytein(opj_bio_t *bio); - -/*@}*/ - -/*@}*/ - - -/* -========================================================== - local functions -========================================================== -*/ - -static int bio_byteout(opj_bio_t *bio) { - bio->buf = (bio->buf << 8) & 0xffff; - bio->ct = bio->buf == 0xff00 ? 7 : 8; - if (bio->bp >= bio->end) { - return 1; - } - *bio->bp++ = bio->buf >> 8; - return 0; -} - -static int bio_bytein(opj_bio_t *bio) { - bio->buf = (bio->buf << 8) & 0xffff; - bio->ct = bio->buf == 0xff00 ? 7 : 8; - if (bio->bp >= bio->end) { - return 1; - } - bio->buf |= *bio->bp++; - return 0; -} - -static void bio_putbit(opj_bio_t *bio, int b) { - if (bio->ct == 0) { - bio_byteout(bio); - } - bio->ct--; - bio->buf |= b << bio->ct; -} - -/* MOD antonin */ -static int bio_getbit(opj_bio_t *bio) { -/* DOM */ - if (bio->ct == 0) { - bio_bytein(bio); - } - bio->ct--; - return (bio->buf >> bio->ct) & 1; -} - -/* -========================================================== - Bit Input/Output interface -========================================================== -*/ - -opj_bio_t* bio_create() { - opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t)); - return bio; -} - -void bio_destroy(opj_bio_t *bio) { - if(bio) { - opj_free(bio); - } -} - -int bio_numbytes(opj_bio_t *bio) { - return (bio->bp - bio->start); -} - -void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) { - bio->start = bp; - bio->end = bp + len; - bio->bp = bp; - bio->buf = 0; - bio->ct = 8; -} - -void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) { - bio->start = bp; - bio->end = bp + len; - bio->bp = bp; - bio->buf = 0; - bio->ct = 0; -} - -void bio_write(opj_bio_t *bio, int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { - bio_putbit(bio, (v >> i) & 1); - } -} - -int bio_read(opj_bio_t *bio, int n) { - int i, v; - v = 0; - for (i = n - 1; i >= 0; i--) { - v += bio_getbit(bio) << i; - } - return v; -} - -int bio_flush(opj_bio_t *bio) { - bio->ct = 0; - if (bio_byteout(bio)) { - return 1; - } - if (bio->ct == 7) { - bio->ct = 0; - if (bio_byteout(bio)) { - return 1; - } - } - return 0; -} - -int bio_inalign(opj_bio_t *bio) { - bio->ct = 0; - if ((bio->buf & 0xff) == 0xff) { - if (bio_bytein(bio)) { - return 1; - } - bio->ct = 0; - } - return 0; -} diff --git a/jp3d/libjp3dvm/bio.h b/jp3d/libjp3dvm/bio.h deleted file mode 100755 index c65103bd..00000000 --- a/jp3d/libjp3dvm/bio.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __BIO_H -#define __BIO_H -/** -@file bio.h -@brief Implementation of an individual bit input-output (BIO) - -The functions in BIO.C have for goal to realize an individual bit input - output. -*/ - -/** @defgroup BIO BIO - Individual bit input-output stream */ -/*@{*/ - -/** -Individual bit input-output stream (BIO) -*/ -typedef struct opj_bio { -/** pointer to the start of the buffer */ - unsigned char *start; -/** pointer to the end of the buffer */ - unsigned char *end; -/** pointer to the present position in the buffer */ - unsigned char *bp; -/** temporary place where each byte is read or written */ - unsigned int buf; -/** coder : number of bits free to write. decoder : number of bits read */ - int ct; -} opj_bio_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a new BIO handle -@return Returns a new BIO handle if successful, returns NULL otherwise -*/ -opj_bio_t* bio_create(); -/** -Destroy a previously created BIO handle -@param bio BIO handle to destroy -*/ -void bio_destroy(opj_bio_t *bio); -/** -Number of bytes written. -@param bio BIO handle -@return Returns the number of bytes written -*/ -int bio_numbytes(opj_bio_t *bio); -/** -Init encoder -@param bio BIO handle -@param bp Output buffer -@param len Output buffer length -*/ -void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len); -/** -Init decoder -@param bio BIO handle -@param bp Input buffer -@param len Input buffer length -*/ -void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len); -/** -Write bits -@param bio BIO handle -@param v Value of bits -@param n Number of bits to write -*/ -void bio_write(opj_bio_t *bio, int v, int n); -/** -Read bits -@param bio BIO handle -@param n Number of bits to read -@return Returns the corresponding read number -*/ -int bio_read(opj_bio_t *bio, int n); -/** -Flush bits -@param bio BIO handle -@return Returns 1 if successful, returns 0 otherwise -*/ -int bio_flush(opj_bio_t *bio); -/** -Passes the ending bits (coming from flushing) -@param bio BIO handle -@return Returns 1 if successful, returns 0 otherwise -*/ -int bio_inalign(opj_bio_t *bio); -/** -Read a bit -@param bio BIO handle -@return Returns the read bit -*/ -/* MOD antonin */ -//int bio_getbit(opj_bio_t *bio); -/* DOM */ -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __BIO_H */ - diff --git a/jp3d/libjp3dvm/cio.c b/jp3d/libjp3dvm/cio.c deleted file mode 100755 index 8814a33b..00000000 --- a/jp3d/libjp3dvm/cio.c +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/* ----------------------------------------------------------------------- */ - -opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) { - opj_cp_t *cp = NULL; - opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t)); - if(!cio) return NULL; - cio->cinfo = cinfo; - if(buffer && length) { - /* wrap a user buffer containing the encoded image */ - cio->openmode = OPJ_STREAM_READ; - cio->buffer = buffer; - cio->length = length; - } - else if(!buffer && !length && cinfo) { - /* allocate a buffer for the encoded image */ - cio->openmode = OPJ_STREAM_WRITE; - switch(cinfo->codec_format) { - case CODEC_J3D: - case CODEC_J2K: - cp = ((opj_j3d_t*)cinfo->j3d_handle)->cp; - break; - default: - opj_free(cio); - return NULL; - } - cio->length = cp->tdx * cp->tdy * cp->tdz * cp->tw * cp->th * cp->tl * 4; - cio->buffer = (unsigned char *)opj_malloc(cio->length); - if(!cio->buffer) { - opj_free(cio); - return NULL; - } - } - else { - opj_free(cio); - return NULL; - } - - /* Initialize byte IO */ - cio->start = cio->buffer; - cio->end = cio->buffer + cio->length; - cio->bp = cio->buffer; - - return cio; -} - -void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) { - if(cio) { - if(cio->openmode == OPJ_STREAM_WRITE) { - /* destroy the allocated buffer */ - opj_free(cio->buffer); - } - /* destroy the cio */ - opj_free(cio); - } -} - - -/* ----------------------------------------------------------------------- */ - -/* - * Get position in byte stream. - */ -int OPJ_CALLCONV cio_tell(opj_cio_t *cio) { - return cio->bp - cio->start; -} - -/* - * Set position in byte stream. - * - * pos : position, in number of bytes, from the beginning of the stream - */ -void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) { - cio->bp = cio->start + pos; -} - -/* - * Number of bytes left before the end of the stream. - */ -int cio_numbytesleft(opj_cio_t *cio) { - return cio->end - cio->bp; -} - -/* - * Get pointer to the current position in the stream. - */ -unsigned char *cio_getbp(opj_cio_t *cio) { - return cio->bp; -} - -/* - * Write a byte. - */ -bool cio_byteout(opj_cio_t *cio, unsigned char v) { - if (cio->bp >= cio->end) { - opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n"); - return false; - } - *cio->bp++ = v; - return true; -} - -/* - * Read a byte. - */ -unsigned char cio_bytein(opj_cio_t *cio) { - if (cio->bp >= cio->end) { - opj_event_msg(cio->cinfo, EVT_ERROR, "read error\n"); - return 0; - } - return *cio->bp++; -} - -/* - * Write some bytes. - * - * v : value to write - * n : number of bytes to write - */ -unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { - if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) ) - return 0; - } - return n; -} - -/* - * Read some bytes. - * - * n : number of bytes to read - * - * return : value of the n bytes read - */ -unsigned int cio_read(opj_cio_t *cio, int n) { - int i; - unsigned int v; - v = 0; - for (i = n - 1; i >= 0; i--) { - v += cio_bytein(cio) << (i << 3); - } - return v; -} - -/* - * Skip some bytes. - * - * n : number of bytes to skip - */ -void cio_skip(opj_cio_t *cio, int n) { - cio->bp += n; -} - -/* - * Write some bytes. - * - * v : value to write - * n : number of bytes to write - */ -int cio_write_int(opj_cio_t *cio, int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { - if( !cio_byteout(cio, (char) ((v >> (i << 3)) & 0xff)) ) - return 0; - } - return n; -} - -/* - * Read some bytes. - * - * n : number of bytes to read - * - * return : value of the n bytes read - */ -int cio_read_int(opj_cio_t *cio, int n) { - int i; - int v; - v = 0; - for (i = n - 1; i >= 0; i--) { - v += cio_bytein(cio) << (i << 3); - } - return v; -} - diff --git a/jp3d/libjp3dvm/cio.h b/jp3d/libjp3dvm/cio.h deleted file mode 100755 index c6ad109f..00000000 --- a/jp3d/libjp3dvm/cio.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __CIO_H -#define __CIO_H -/** -@file cio.h -@brief Implementation of a byte input-output process (CIO) - -The functions in CIO.C have for goal to realize a byte input / output process. -*/ - -/** @defgroup CIO CIO - byte input-output stream */ -/*@{*/ - -/** @name Funciones generales (see also openjpeg.h) */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Number of bytes left before the end of the stream -@param cio CIO handle -@return Returns the number of bytes before the end of the stream -*/ -int cio_numbytesleft(opj_cio_t *cio); -/** -Get pointer to the current position in the stream -@param cio CIO handle -@return Returns a pointer to the current position -*/ -unsigned char *cio_getbp(opj_cio_t *cio); -/** -Write some bytes -@param cio CIO handle -@param v Value to write -@param n Number of bytes to write -@return Returns the number of bytes written or 0 if an error occured -*/ -unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n); -/** -Read some bytes -@param cio CIO handle -@param n Number of bytes to read -@return Returns the value of the n bytes read -*/ -unsigned int cio_read(opj_cio_t *cio, int n); -/** -Skip some bytes -@param cio CIO handle -@param n Number of bytes to skip -*/ -void cio_skip(opj_cio_t *cio, int n); -/** -Write some bytes -@param cio CIO handle -@param v Signed integer value to write -@param n Number of bytes to write -@return Returns the number of bytes written or 0 if an error occured -*/ -int cio_write_int(opj_cio_t *cio, int v, int n); -/** -Read some bytes -@param cio CIO handle -@param n Number of bytes to read -@return Returns the value of the n bytes read -*/ -int cio_read_int(opj_cio_t *cio, int n); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __CIO_H */ - diff --git a/jp3d/libjp3dvm/dwt.c b/jp3d/libjp3dvm/dwt.c deleted file mode 100755 index 39ee15c2..00000000 --- a/jp3d/libjp3dvm/dwt.c +++ /dev/null @@ -1,1016 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyrigth (c) 2006, Mónica Díez, LPI-UVA, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * NOTE: - * This is a modified version of the openjpeg dwt.c file. - * Average speed improvement compared to the original file (measured on - * my own machine, a P4 running at 3.0 GHz): - * 5x3 wavelets about 2 times faster - * 9x7 wavelets about 3 times faster - * for both, encoding and decoding. - * - * The better performance is caused by doing the 1-dimensional DWT - * within a temporary buffer where the data can be accessed sequential - * for both directions, horizontal and vertical. The 2d vertical DWT was - * the major bottleneck in the former version. - * - * I have also removed the "Add Patrick" part because it is not longer - * needed. - * - * 6/6/2005 - * -Ive (aka Reiner Wahler) - * mail: ive@lilysoft.com - */ - -#include "opj_includes.h" - -/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ -/*@{*/ - -/** @name Local static functions */ -/*@{*/ -unsigned int ops; -/** -Forward lazy transform (horizontal) -*/ -static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas); -/** -Forward lazy transform (vertical) -*/ -static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas); -/** -Forward lazy transform (axial) -*/ -static void dwt_deinterleave_z(int *a, int *b, int dn, int sn, int xy, int cas); -/** -Inverse lazy transform (horizontal) -*/ -static void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas); -/** -Inverse lazy transform (vertical) -*/ -static void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas); -/** -Inverse lazy transform (axial) -*/ -static void dwt_interleave_z(int *a, int *b, int dn, int sn, int xy, int cas); -/** -Forward 5-3 wavelet tranform in 1-D -*/ -static void dwt_encode_53(int *a, int dn, int sn, int cas); -static void dwt_encode_97(int *a, int dn, int sn, int cas); -/** -Inverse 5-3 wavelet tranform in 1-D -*/ -static void dwt_decode_53(int *a, int dn, int sn, int cas); -static void dwt_decode_97(int *a, int dn, int sn, int cas); -/** -Computing of wavelet transform L2 norms for arbitrary transforms -*/ -static double dwt_calc_wtnorms(int orient, int level[3], int dwtid[3], opj_wtfilt_t *wtfiltx, opj_wtfilt_t *wtfilty, opj_wtfilt_t *wtfiltz); -/** -Encoding of quantification stepsize -*/ -static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize); -/*@}*/ - -/*@}*/ - -#define S(i) a[(i)*2] -#define D(i) a[(1+(i)*2)] -#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i))) -#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i))) -/* new */ -#define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i))) -#define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i))) - -/* */ -/* This table contains the norms of the 5-3 wavelets for different bands. */ -/* */ -static double dwt_norm[10][10][10][8]; -static int flagnorm[10][10][10][8]; - -/*static const double dwt_norms[5][8][10] = { - {//ResZ=1 - {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, - {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93} - },{//ResZ=2 - {1.000, 1.8371, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, - {1.2717, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {1.2717, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {.8803, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}, - {1.2717}, - {.8803}, - {.8803}, - {.6093}, - },{ //ResZ=3 - {1.000, 1.8371, 4.5604, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, - {1.2717, 2.6403, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {1.2717, 2.6403, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {.8803, 1.5286, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}, - {1.2717, 2.6403}, - {.8803, 1.5286}, - {.8803, 1.5286}, - {.6093, 0.8850}, - },{ //ResZ=4 - {1.000, 1.8371, 4.5604, 12.4614, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, - {1.2717, 2.6403, 6.7691 , 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {1.2717, 2.6403, 6.7691 , 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, - {.8803, 1.5286, 3.6770 , 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}, - {1.2717, 2.6403, 6.7691 }, - {.8803, 1.5286, 3.6770 }, - {.8803, 1.5286, 3.6770 }, - {.6093, 0.8850, 1.9974 }, - },{ //ResZ=5 - {1.000, 1.8371, 4.5604, 12.4614, 34.9025, 21.34, 42.67, 85.33, 170.7, 341.3}, - {1.2717, 2.6403, 6.7691 , 18.6304 , 11.33, 22.64, 45.25, 90.48, 180.9}, - {1.2717, 2.6403, 6.7691 , 18.6304, 11.33, 22.64, 45.25, 90.48, 180.9}, - {.8803, 1.5286, 3.6770 , 9.9446, 6.019, 12.01, 24.00, 47.97, 95.93}, - {1.2717, 2.6403, 6.7691, 18.6304}, - {.8803, 1.5286, 3.6770, 9.9446 }, - {.8803, 1.5286, 3.6770, 9.9446 }, - {.6093, 0.8850, 1.9974, 5.3083 }, - } -};*/ - -/* */ -/* This table contains the norms of the 9-7 wavelets for different bands. */ -/* */ -/*static const double dwt_norms_real[5][8][10] = { - {//ResZ==1 - {1.000, 1.9659, 4.1224, 8.4167, 16.9356, 33.9249, 67.8772, 135.7680, 271.5430, 543.0894}, - {1.0113, 1.9968, 4.1834, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {1.0113, 1.9968, 4.1834, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {0.5202, 0.9672, 2.0793, 4.3005, 8.6867, 17.4188, 34.8608, 69.7332, 139.4722} - }, { //ResZ==2 - {1.000, 2.7564, 4.1224, 8.4167, 16.9356, 33.9249, 67.8772, 135.7680, 271.5430, 543.0894}, - {1.4179, 1.9968, 4.1834, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {1.4179, 1.9968, 4.1834, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {0.7294, 0.9672, 2.0793, 4.3005, 8.6867, 17.4188, 34.8608, 69.7332, 139.4722}, - {1.4179}, - {0.7294}, - {0.7294}, - {0.3752} //HHH - },{ //ResZ==3 - {1.000, 2.7564, 8.3700, 8.4167, 16.9356, 33.9249, 67.8772, 135.7680, 271.5430, 543.0894}, - {1.4179, 4.0543, 4.1834, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {1.4179, 4.0543, 4.1834, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {0.7294, 1.9638, 2.0793, 4.3005, 8.6867, 17.4188, 34.8608, 69.7332, 139.4722}, - {1.4179, 4.0543}, - {0.7294, 1.9638}, - {0.7294, 1.9638}, - {0.3752, 0.9512} //HHH - },{ //ResZ==4 - {1.000, 2.7564, 8.3700, 24.4183, 16.9356, 33.9249, 67.8772, 135.7680, 271.5430, 543.0894}, - {1.4179, 4.0543, 12.1366, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {1.4179, 4.0543, 12.1366, 8.5341, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {0.7294, 1.9638, 6.0323, 4.3005, 8.6867, 17.4188, 34.8608, 69.7332, 139.4722}, - {1.4179, 4.0543, 12.1366}, - {0.7294, 1.9638, 6.0323}, - {0.7294, 1.9638, 6.0323}, - {0.3752, 0.9512, 2.9982} //HHH - },{ //ResZ==5 - {1.000, 2.7564, 8.3700, 24.4183, 69.6947, 33.9249, 67.8772, 135.7680, 271.5430, 543.0894}, - {1.4179, 4.0543, 12.1366, 35.1203, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {1.4179, 4.0543, 12.1366, 35.1203, 17.1667, 34.3852, 68.7967, 137.6065, 275.2196}, - {0.7294, 1.9638, 6.0323, 17.6977, 8.6867, 17.4188, 34.8608, 69.7332, 139.4722}, - {1.4179, 4.0543, 12.1366, 35.1203}, - {0.7294, 1.9638, 6.0323, 17.6977}, - {0.7294, 1.9638, 6.0323, 17.6977}, - {0.3752, 0.9512, 2.9982, 8.9182} //HHH - } -};*/ - -static opj_atk_t atk_info_wt[] = { - {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/ - {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1,1}},/* WT 5-3 REV*/ - {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1},{1}}}, /* WT 2-2 REV*/ - {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1},{1},{1,0,-1}}}, /* WT 2-6 REV*/ - {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3,22,0,-22,3}}}, /* WT 2-10 REV*/ - {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/ - {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736}, - {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/ - {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}}, /* WT 5-3 IRR*/ - {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}} /* WT 13-7 REV*/ -}; -/* -========================================================== - local functions -========================================================== -*/ - -/* */ -/* Forward lazy transform (horizontal). */ -/* */ -static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) { - int i; - for (i=0; i */ -/* Forward lazy transform (vertical). */ -/* */ -static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) { - int i; - for (i=0; i */ -/* Forward lazy transform (axial). */ -/* */ -static void dwt_deinterleave_z(int *a, int *b, int dn, int sn, int xy, int cas) { - int i; - for (i=0; i */ -/* Inverse lazy transform (horizontal). */ -/* */ -static void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) { - int i; - int *ai = NULL; - int *bi = NULL; - ai = a; - bi = b + cas; - for (i = 0; i < sn; i++) { - *bi = *ai; - bi += 2; - ai++; - } - ai = a + sn; - bi = b + 1 - cas; - for (i = 0; i < dn; i++) { - *bi = *ai; - bi += 2; - ai++; - } -} - -/* */ -/* Inverse lazy transform (vertical). */ -/* */ -static void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) { - int i; - int *ai = NULL; - int *bi = NULL; - ai = a; - bi = b + cas; - for (i = 0; i < sn; i++) { - *bi = *ai; - bi += 2; - ai += x; - } - ai = a + (sn * x); - bi = b + 1 - cas; - for (i = 0; i < dn; i++) { - *bi = *ai; - bi += 2; - ai += x; - } -} - -/* */ -/* Inverse lazy transform (axial). */ -/* */ -static void dwt_interleave_z(int *a, int *b, int dn, int sn, int xy, int cas) { - int i; - int *ai = NULL; - int *bi = NULL; - ai = a; - bi = b + cas; - for (i = 0; i < sn; i++) { - *bi = *ai; - bi += 2; - ai += xy; - } - ai = a + (sn * xy); - bi = b + 1 - cas; - for (i = 0; i < dn; i++) { - *bi = *ai; - bi += 2; - ai += xy; - } -} - - -/* */ -/* Forward 5-3 or 9-7 wavelet tranform in 1-D. */ -/* */ -static void dwt_encode_53(int *a, int dn, int sn, int cas) { - int i; - - if (!cas) { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - //for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1; - //for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2; - for (i = 0; i < dn; i++){ - D(i) -= (S_(i) + S_(i + 1)) >> 1; - //ops += 2; - } - for (i = 0; i < sn; i++){ - S(i) += (D_(i - 1) + D_(i) + 2) >> 2; - //ops += 3; - } - } - } else { - /*if (!sn && dn == 1) - S(0) *= 2; - else { - for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1; - for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2; - }*/ - if (!sn && dn == 1){ - S(0) *= 2; - //ops++; - } else { - for (i = 0; i < dn; i++){ - S(i) -= (DD_(i) + DD_(i - 1)) >> 1; - // ops += 2; - } - for (i = 0; i < sn; i++){ - D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2; - // ops += 3; - } - } - } -} -static void dwt_encode_97(int *a, int dn, int sn, int cas) { - int i; - - if (!cas) { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < dn; i++) - D(i) -= fix_mul(S_(i) + S_(i + 1), 12993); - for (i = 0; i < sn; i++) - S(i) -= fix_mul(D_(i - 1) + D_(i), 434); - for (i = 0; i < dn; i++) - D(i) += fix_mul(S_(i) + S_(i + 1), 7233); - for (i = 0; i < sn; i++) - S(i) += fix_mul(D_(i - 1) + D_(i), 3633); - for (i = 0; i < dn; i++) - D(i) = fix_mul(D(i), 5038); /*5038 */ - for (i = 0; i < sn; i++) - S(i) = fix_mul(S(i), 6659); /*6660 */ - } - } else { - if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < dn; i++) - S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993); - for (i = 0; i < sn; i++) - D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434); - for (i = 0; i < dn; i++) - S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233); - for (i = 0; i < sn; i++) - D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633); - for (i = 0; i < dn; i++) - S(i) = fix_mul(S(i), 5038); /*5038 */ - for (i = 0; i < sn; i++) - D(i) = fix_mul(D(i), 6659); /*6660 */ - } - } -} -/* */ -/* Inverse 5-3 or 9-7 wavelet tranform in 1-D. */ -/* */ -static void dwt_decode_53(int *a, int dn, int sn, int cas) { - int i; - if (!cas) { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2; - for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1; - } - } else { - if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */ - S(0) /= 2; - else { - for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2; - for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1; - } - } -} -static void dwt_decode_97(int *a, int dn, int sn, int cas) { - int i; - - if (!cas) { - if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < sn; i++) - S(i) = fix_mul(S(i), 10078); /* 10076 */ - for (i = 0; i < dn; i++) - D(i) = fix_mul(D(i), 13318); /* 13320 */ - for (i = 0; i < sn; i++) - S(i) -= fix_mul(D_(i - 1) + D_(i), 3633); - for (i = 0; i < dn; i++) - D(i) -= fix_mul(S_(i) + S_(i + 1), 7233); - for (i = 0; i < sn; i++) - S(i) += fix_mul(D_(i - 1) + D_(i), 434); - for (i = 0; i < dn; i++) - D(i) += fix_mul(S_(i) + S_(i + 1), 12994); /* 12993 */ - } - } else { - if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */ - for (i = 0; i < sn; i++) - D(i) = fix_mul(D(i), 10078); /* 10076 */ - for (i = 0; i < dn; i++) - S(i) = fix_mul(S(i), 13318); /* 13320 */ - for (i = 0; i < sn; i++) - D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633); - for (i = 0; i < dn; i++) - S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233); - for (i = 0; i < sn; i++) - D(i) += fix_mul(SS_(i) + SS_(i + 1), 434); - for (i = 0; i < dn; i++) - S(i) += fix_mul(DD_(i) + DD_(i - 1), 12994); /* 12993 */ - } - } -} - - -/* */ -/* Get norm of arbitrary wavelet transform. */ -/* */ -static int upandconv(double *nXPS, double *LPS, int lenXPS, int lenLPS) { - /* Perform the convolution of the vectors. */ - int i,j; - double *tmp = (double *)opj_malloc(2*lenXPS * sizeof(double)); - //Upsample - memset(tmp, 0, 2*lenXPS*sizeof(double)); - for (i = 0; i < lenXPS; i++) { - *(tmp + 2*i) = *(nXPS + i); - *(nXPS + i) = 0; - } - //Convolution - for (i = 0; i < 2*lenXPS; i++) { - for (j = 0; j < lenLPS; j++) { - *(nXPS+i+j) = *(nXPS+i+j) + *(tmp + i) * *(LPS + j); - //fprintf(stdout,"*(tmp + %d) * *(LPS + %d) = %f * %f \n",i,j,*(tmp + i),*(LPS + j)); - } - } - free(tmp); - return 2*lenXPS+lenLPS-1; -} - -static double dwt_calc_wtnorms(int orient, int level[3], int dwtid[3], opj_wtfilt_t *wtfiltX, opj_wtfilt_t *wtfiltY, opj_wtfilt_t *wtfiltZ) { - int i, lenLPS, lenHPS; - double Lx = 0, Ly= 0, Hx= 0, Hy= 0, Lz= 0, Hz= 0; - double *nLPSx, *nHPSx,*nLPSy, *nHPSy,*nLPSz, *nHPSz; - int levelx, levely, levelz; - - levelx = (orient == 0) ? level[0]-1 : level[0]; - levely = (orient == 0) ? level[1]-1 : level[1]; - levelz = (orient == 0) ? level[2]-1 : level[2]; - - //X axis - lenLPS = wtfiltX->lenLPS; - lenHPS = wtfiltX->lenHPS; - for (i = 0; i < levelx; i++) { - lenLPS *= 2; - lenHPS *= 2; - lenLPS += wtfiltX->lenLPS - 1; - lenHPS += wtfiltX->lenLPS - 1; - } - nLPSx = (double *)opj_malloc(lenLPS * sizeof(double)); - nHPSx = (double *)opj_malloc(lenHPS * sizeof(double)); - - memcpy(nLPSx, wtfiltX->LPS, wtfiltX->lenLPS * sizeof(double)); - memcpy(nHPSx, wtfiltX->HPS, wtfiltX->lenHPS * sizeof(double)); - lenLPS = wtfiltX->lenLPS; - lenHPS = wtfiltX->lenHPS; - for (i = 0; i < levelx; i++) { - lenLPS = upandconv(nLPSx, wtfiltX->LPS, lenLPS, wtfiltX->lenLPS); - lenHPS = upandconv(nHPSx, wtfiltX->LPS, lenHPS, wtfiltX->lenLPS); - } - for (i = 0; i < lenLPS; i++) - Lx += nLPSx[i] * nLPSx[i]; - for (i = 0; i < lenHPS; i++) - Hx += nHPSx[i] * nHPSx[i]; - Lx = sqrt(Lx); - Hx = sqrt(Hx); - free(nLPSx); - free(nHPSx); - - //Y axis - if (dwtid[0] != dwtid[1] || level[0] != level[1]){ - lenLPS = wtfiltY->lenLPS; - lenHPS = wtfiltY->lenHPS; - for (i = 0; i < levely; i++) { - lenLPS *= 2; - lenHPS *= 2; - lenLPS += wtfiltY->lenLPS - 1; - lenHPS += wtfiltY->lenLPS - 1; - } - nLPSy = (double *)opj_malloc(lenLPS * sizeof(double)); - nHPSy = (double *)opj_malloc(lenHPS * sizeof(double)); - - memcpy(nLPSy, wtfiltY->LPS, wtfiltY->lenLPS * sizeof(double)); - memcpy(nHPSy, wtfiltY->HPS, wtfiltY->lenHPS * sizeof(double)); - lenLPS = wtfiltY->lenLPS; - lenHPS = wtfiltY->lenHPS; - for (i = 0; i < levely; i++) { - lenLPS = upandconv(nLPSy, wtfiltY->LPS, lenLPS, wtfiltY->lenLPS); - lenHPS = upandconv(nHPSy, wtfiltY->LPS, lenHPS, wtfiltY->lenLPS); - } - for (i = 0; i < lenLPS; i++) - Ly += nLPSy[i] * nLPSy[i]; - for (i = 0; i < lenHPS; i++) - Hy += nHPSy[i] * nHPSy[i]; - Ly = sqrt(Ly); - Hy = sqrt(Hy); - free(nLPSy); - free(nHPSy); - } else { - Ly = Lx; - Hy = Hx; - } - //Z axis - if (levelz >= 0) { - lenLPS = wtfiltZ->lenLPS; - lenHPS = wtfiltZ->lenHPS; - for (i = 0; i < levelz; i++) { - lenLPS *= 2; - lenHPS *= 2; - lenLPS += wtfiltZ->lenLPS - 1; - lenHPS += wtfiltZ->lenLPS - 1; - } - nLPSz = (double *)opj_malloc(lenLPS * sizeof(double)); - nHPSz = (double *)opj_malloc(lenHPS * sizeof(double)); - - memcpy(nLPSz, wtfiltZ->LPS, wtfiltZ->lenLPS * sizeof(double)); - memcpy(nHPSz, wtfiltZ->HPS, wtfiltZ->lenHPS * sizeof(double)); - lenLPS = wtfiltZ->lenLPS; - lenHPS = wtfiltZ->lenHPS; - for (i = 0; i < levelz; i++) { - lenLPS = upandconv(nLPSz, wtfiltZ->LPS, lenLPS, wtfiltZ->lenLPS); - lenHPS = upandconv(nHPSz, wtfiltZ->LPS, lenHPS, wtfiltZ->lenLPS); - } - for (i = 0; i < lenLPS; i++) - Lz += nLPSz[i] * nLPSz[i]; - for (i = 0; i < lenHPS; i++) - Hz += nHPSz[i] * nHPSz[i]; - Lz = sqrt(Lz); - Hz = sqrt(Hz); - free(nLPSz); - free(nHPSz); - } else { - Lz = 1.0; Hz = 1.0; - } - switch (orient) { - case 0: - return Lx * Ly * Lz; - case 1: - return Lx * Hy * Lz; - case 2: - return Hx * Ly * Lz; - case 3: - return Hx * Hy * Lz; - case 4: - return Lx * Ly * Hz; - case 5: - return Lx * Hy * Hz; - case 6: - return Hx * Ly * Hz; - case 7: - return Hx * Hy * Hz; - default: - return -1; - } - -} -static void dwt_getwtfilters(opj_wtfilt_t *wtfilt, int dwtid) { - if (dwtid == 0) { //DWT 9-7 - wtfilt->lenLPS = 7; wtfilt->lenHPS = 9; - wtfilt->LPS = (double *)opj_malloc(wtfilt->lenLPS * sizeof(double)); - wtfilt->HPS = (double *)opj_malloc(wtfilt->lenHPS * sizeof(double)); - wtfilt->LPS[0] = -0.091271763114; wtfilt->HPS[0] = 0.026748757411; - wtfilt->LPS[1] = -0.057543526228; wtfilt->HPS[1] = 0.016864118443; - wtfilt->LPS[2] = 0.591271763114; wtfilt->HPS[2] = -0.078223266529; - wtfilt->LPS[3] = 1.115087052457; wtfilt->HPS[3] = -0.266864118443; - wtfilt->LPS[4] = 0.591271763114; wtfilt->HPS[4] = 0.602949018236; - wtfilt->LPS[5] = -0.057543526228; wtfilt->HPS[5] = -0.266864118443; - wtfilt->LPS[6] = -0.091271763114; wtfilt->HPS[6] = -0.078223266529; - wtfilt->HPS[7] = 0.016864118443; - wtfilt->HPS[8] = 0.026748757411; - } else if (dwtid == 1) { //DWT 5-3 - wtfilt->lenLPS = 3; wtfilt->lenHPS = 5; - wtfilt->LPS = (double *)opj_malloc(wtfilt->lenLPS * sizeof(double)); - wtfilt->HPS = (double *)opj_malloc(wtfilt->lenHPS * sizeof(double)); - wtfilt->LPS[0] = 0.5; wtfilt->HPS[0] = -0.125; - wtfilt->LPS[1] = 1; wtfilt->HPS[1] = -0.25; - wtfilt->LPS[2] = 0.5; wtfilt->HPS[2] = 0.75; - wtfilt->HPS[3] = -0.25; - wtfilt->HPS[4] = -0.125; - } else { - fprintf(stdout,"[ERROR] Sorry, this wavelet hasn't been implemented so far ... Try another one :-)\n"); - exit(1); - } -} -/* */ -/* Encoding of quantization stepsize for each subband. */ -/* */ -static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) { - int p, n; - p = int_floorlog2(stepsize) - 13; - n = 11 - int_floorlog2(stepsize); - bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; - bandno_stepsize->expn = numbps - p; - //if J3D_CCP_QNTSTY_NOQNT --> stepsize = 8192.0 --> p = 0, n = -2 --> mant = 0; expn = (prec+gain) - //else --> bandno_stepsize = (1<<(numbps - expn)) + (1<<(numbps - expn - 11)) * Ub -} - -/* -========================================================== - DWT interface -========================================================== -*/ -/* */ -/* Forward 5-3 wavelet tranform in 3-D. */ -/* */ -void dwt_encode(opj_tcd_tilecomp_t * tilec, int dwtid[3]) { - int i, j, k; - int x, y, z; - int w, h, wh, d; - int level,levelx,levely,levelz,diff; - int *a = NULL; - int *aj = NULL; - int *bj = NULL; - int *cj = NULL; - - ops = 0; - - memset(flagnorm,0,8000*sizeof(int)); - w = tilec->x1-tilec->x0; - h = tilec->y1-tilec->y0; - d = tilec->z1-tilec->z0; - wh = w * h; - levelx = tilec->numresolution[0]-1; - levely = tilec->numresolution[1]-1; - levelz = tilec->numresolution[2]-1; - level = int_max(levelx,int_max(levely,levelz)); - diff = tilec->numresolution[0] - tilec->numresolution[2]; - - a = tilec->data; - - for (x = 0, y = 0, z = 0; x < levelx, y < levely; x++, y++, z++) { - int rw; /* width of the resolution level computed */ - int rh; /* heigth of the resolution level computed */ - int rd; /* depth of the resolution level computed */ - int rw1; /* width of the resolution level once lower than computed one */ - int rh1; /* height of the resolution level once lower than computed one */ - int rd1; /* depth of the resolution level once lower than computed one */ - int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ - int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ - int cas_axl; /* 0 = non inversion on axial filtering 1 = inversion between low-pass and high-pass filtering */ - int dn, sn; - - rw = tilec->resolutions[level - x].x1 - tilec->resolutions[level - x].x0; - rh = tilec->resolutions[level - y].y1 - tilec->resolutions[level - y].y0; - rd = tilec->resolutions[level - z].z1 - tilec->resolutions[level - z].z0; - rw1= tilec->resolutions[level - x - 1].x1 - tilec->resolutions[level - x - 1].x0; - rh1= tilec->resolutions[level - y - 1].y1 - tilec->resolutions[level - y - 1].y0; - rd1= tilec->resolutions[level - z - 1].z1 - tilec->resolutions[level - z - 1].z0; - - cas_col = tilec->resolutions[level - x].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ - cas_row = tilec->resolutions[level - y].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ - cas_axl = tilec->resolutions[level - z].z0 % 2; - - /*fprintf(stdout," x %d y %d z %d \n",x,y,z); - fprintf(stdout," levelx %d levely %d levelz %d \n",levelx,levely,levelz); - fprintf(stdout," z1 %d z0 %d\n",tilec->resolutions[level - z].z1,tilec->resolutions[level - z].z0); - fprintf(stdout," rw %d rh %d rd %d \n rw1 %d rh1 %d rd1 %d \n",rw,rh,rd,rw1,rh1,rd1);*/ - - for (i = 0; i < rd; i++) { - - cj = a + (i * wh); - - //Horizontal - sn = rw1; - dn = rw - rw1; - bj = (int*)opj_malloc(rw * sizeof(int)); - if (dwtid[0] == 0) { - for (j = 0; j < rh; j++) { - aj = cj + j * w; - for (k = 0; k < rw; k++) bj[k] = aj[k]; - dwt_encode_97(bj, dn, sn, cas_row); - dwt_deinterleave_h(bj, aj, dn, sn, cas_row); - } - } else if (dwtid[0] == 1) { - for (j = 0; j < rh; j++) { - aj = cj + j * w; - for (k = 0; k < rw; k++) bj[k] = aj[k]; - dwt_encode_53(bj, dn, sn, cas_row); - dwt_deinterleave_h(bj, aj, dn, sn, cas_row); - } - } - opj_free(bj); - - //Vertical - sn = rh1; - dn = rh - rh1; - bj = (int*)opj_malloc(rh * sizeof(int)); - if (dwtid[1] == 0) { /*DWT 9-7*/ - for (j = 0; j < rw; j++) { - aj = cj + j; - for (k = 0; k < rh; k++) bj[k] = aj[k*w]; - dwt_encode_97(bj, dn, sn, cas_col); - dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col); - } - } else if (dwtid[1] == 1) { /*DWT 5-3*/ - for (j = 0; j < rw; j++) { - aj = cj + j; - for (k = 0; k < rh; k++) bj[k] = aj[k*w]; - dwt_encode_53(bj, dn, sn, cas_col); - dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col); - } - } - opj_free(bj); - } - - if (z < levelz){ - //Axial fprintf(stdout,"Axial DWT Transform %d %d %d\n",z,rd,rd1); - sn = rd1; - dn = rd - rd1; - bj = (int*)opj_malloc(rd * sizeof(int)); - if (dwtid[2] == 0) { - for (j = 0; j < (rw*rh); j++) { - aj = a + j; - for (k = 0; k < rd; k++) bj[k] = aj[k*wh]; - dwt_encode_97(bj, dn, sn, cas_axl); - dwt_deinterleave_z(bj, aj, dn, sn, wh, cas_axl); - } - } else if (dwtid[2] == 1) { - for (j = 0; j < (rw*rh); j++) { - aj = a + j; - for (k = 0; k < rd; k++) bj[k] = aj[k*wh]; - dwt_encode_53(bj, dn, sn, cas_axl); - dwt_deinterleave_z(bj, aj, dn, sn, wh, cas_axl); - } - } - opj_free(bj); - } - } - - //fprintf(stdout,"[INFO] Ops: %d \n",ops); -} - - -/* */ -/* Inverse 5-3 wavelet tranform in 3-D. */ -/* */ -void dwt_decode(opj_tcd_tilecomp_t * tilec, int stops[3], int dwtid[3]) { - int i, j, k; - int x, y, z; - int w, h, wh, d; - int level, levelx, levely, levelz, diff; - int *a = NULL; - int *aj = NULL; - int *bj = NULL; - int *cj = NULL; - - a = tilec->data; - - w = tilec->x1-tilec->x0; - h = tilec->y1-tilec->y0; - d = tilec->z1-tilec->z0; - wh = w * h; - levelx = tilec->numresolution[0]-1; - levely = tilec->numresolution[1]-1; - levelz = tilec->numresolution[2]-1; - level = int_max(levelx,int_max(levely,levelz)); - diff = tilec->numresolution[0] - tilec->numresolution[2]; - -/* General lifting framework -- DCCS-LIWT */ - for (x = level - 1, y = level - 1, z = level - 1; x >= stops[0], y >= stops[1]; x--, y--, z--) { - int rw; /* width of the resolution level computed */ - int rh; /* heigth of the resolution level computed */ - int rd; /* depth of the resolution level computed */ - int rw1; /* width of the resolution level once lower than computed one */ - int rh1; /* height of the resolution level once lower than computed one */ - int rd1; /* depth of the resolution level once lower than computed one */ - int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ - int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ - int cas_axl; /* 0 = non inversion on axial filtering 1 = inversion between low-pass and high-pass filtering */ - int dn, sn; - - rw = tilec->resolutions[level - x].x1 - tilec->resolutions[level - x].x0; - rh = tilec->resolutions[level - y].y1 - tilec->resolutions[level - y].y0; - rd = tilec->resolutions[level - z].z1 - tilec->resolutions[level - z].z0; - rw1= tilec->resolutions[level - x - 1].x1 - tilec->resolutions[level - x - 1].x0; - rh1= tilec->resolutions[level - y - 1].y1 - tilec->resolutions[level - y - 1].y0; - rd1= tilec->resolutions[level - z - 1].z1 - tilec->resolutions[level - z - 1].z0; - - cas_col = tilec->resolutions[level - x].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ - cas_row = tilec->resolutions[level - y].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ - cas_axl = tilec->resolutions[level - z].z0 % 2; - - /*fprintf(stdout," x %d y %d z %d \n",x,y,z); - fprintf(stdout," levelx %d levely %d levelz %d \n",levelx,levely,levelz); - fprintf(stdout," dwtid[0] %d [1] %d [2] %d \n",dwtid[0],dwtid[1],dwtid[2]); - fprintf(stdout," rw %d rh %d rd %d \n rw1 %d rh1 %d rd1 %d \n",rw,rh,rd,rw1,rh1,rd1); - fprintf(stdout,"IDWT Transform %d %d %d %d\n",level, z, rd,rd1);*/ - - if (z >= stops[2] && rd != rd1) { - //fprintf(stdout,"Axial Transform %d %d %d %d\n",levelz, z, rd,rd1); - sn = rd1; - dn = rd - rd1; - bj = (int*)opj_malloc(rd * sizeof(int)); - if (dwtid[2] == 0) { - for (j = 0; j < (rw*rh); j++) { - aj = a + j; - dwt_interleave_z(aj, bj, dn, sn, wh, cas_axl); - dwt_decode_97(bj, dn, sn, cas_axl); - for (k = 0; k < rd; k++) aj[k * wh] = bj[k]; - } - } else if (dwtid[2] == 1) { - for (j = 0; j < (rw*rh); j++) { - aj = a + j; - dwt_interleave_z(aj, bj, dn, sn, wh, cas_axl); - dwt_decode_53(bj, dn, sn, cas_axl); - for (k = 0; k < rd; k++) aj[k * wh] = bj[k]; - } - } - opj_free(bj); - } - - for (i = 0; i < rd; i++) { - //Fetch corresponding slice for doing DWT-2D - cj = tilec->data + (i * wh); - - //Vertical - sn = rh1; - dn = rh - rh1; - bj = (int*)opj_malloc(rh * sizeof(int)); - if (dwtid[1] == 0) { - for (j = 0; j < rw; j++) { - aj = cj + j; - dwt_interleave_v(aj, bj, dn, sn, w, cas_col); - dwt_decode_97(bj, dn, sn, cas_col); - for (k = 0; k < rh; k++) aj[k * w] = bj[k]; - } - } else if (dwtid[1] == 1) { - for (j = 0; j < rw; j++) { - aj = cj + j; - dwt_interleave_v(aj, bj, dn, sn, w, cas_col); - dwt_decode_53(bj, dn, sn, cas_col); - for (k = 0; k < rh; k++) aj[k * w] = bj[k]; - } - } - opj_free(bj); - - //Horizontal - sn = rw1; - dn = rw - rw1; - bj = (int*)opj_malloc(rw * sizeof(int)); - if (dwtid[0]==0) { - for (j = 0; j < rh; j++) { - aj = cj + j*w; - dwt_interleave_h(aj, bj, dn, sn, cas_row); - dwt_decode_97(bj, dn, sn, cas_row); - for (k = 0; k < rw; k++) aj[k] = bj[k]; - } - } else if (dwtid[0]==1) { - for (j = 0; j < rh; j++) { - aj = cj + j*w; - dwt_interleave_h(aj, bj, dn, sn, cas_row); - dwt_decode_53(bj, dn, sn, cas_row); - for (k = 0; k < rw; k++) aj[k] = bj[k]; - } - } - opj_free(bj); - - } - - } - -} - - -/* */ -/* Get gain of wavelet transform. */ -/* */ -int dwt_getgain(int orient, int reversible) { - if (reversible == 1) { - if (orient == 0) - return 0; - else if (orient == 1 || orient == 2 || orient == 4 ) - return 1; - else if (orient == 3 || orient == 5 || orient == 6 ) - return 2; - else - return 3; - } - //else if (reversible == 0){ - return 0; -} - -/* */ -/* Get norm of wavelet transform. */ -/* */ -double dwt_getnorm(int orient, int level[3], int dwtid[3]) { - int levelx = level[0]; - int levely = level[1]; - int levelz = (level[2] < 0) ? 0 : level[2]; - double norm; - - if (flagnorm[levelx][levely][levelz][orient] == 1) { - norm = dwt_norm[levelx][levely][levelz][orient]; - //fprintf(stdout,"[INFO] Level: %d %d %d Orient %d Dwt_norm: %f \n",level[0],level[1],level[2],orient,norm); - } else { - opj_wtfilt_t *wtfiltx =(opj_wtfilt_t *) opj_malloc(sizeof(opj_wtfilt_t)); - opj_wtfilt_t *wtfilty =(opj_wtfilt_t *) opj_malloc(sizeof(opj_wtfilt_t)); - opj_wtfilt_t *wtfiltz =(opj_wtfilt_t *) opj_malloc(sizeof(opj_wtfilt_t)); - //Fetch equivalent filters for each dimension - dwt_getwtfilters(wtfiltx, dwtid[0]); - dwt_getwtfilters(wtfilty, dwtid[1]); - dwt_getwtfilters(wtfiltz, dwtid[2]); - //Calculate the corresponding norm - norm = dwt_calc_wtnorms(orient, level, dwtid, wtfiltx, wtfilty, wtfiltz); - //Save norm in array (no recalculation) - dwt_norm[levelx][levely][levelz][orient] = norm; - flagnorm[levelx][levely][levelz][orient] = 1; - //Free reserved space - opj_free(wtfiltx->LPS); opj_free(wtfilty->LPS); opj_free(wtfiltz->LPS); - opj_free(wtfiltx->HPS); opj_free(wtfilty->HPS); opj_free(wtfiltz->HPS); - opj_free(wtfiltx); opj_free(wtfilty); opj_free(wtfiltz); - //fprintf(stdout,"[INFO] Dwtid: %d %d %d Level: %d %d %d Orient %d Norm: %f \n",dwtid[0],dwtid[1],dwtid[2],level[0],level[1],level[2],orient,norm); - } - return norm; -} -/* */ -/* Calculate explicit stepsizes for DWT. */ -/* */ -void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) { - int totnumbands, bandno, diff; - - assert(tccp->numresolution[0] >= tccp->numresolution[2]); - diff = tccp->numresolution[0] - tccp->numresolution[2]; /*if RESx=RESy != RESz */ - totnumbands = (7 * tccp->numresolution[0] - 6) - 4 * diff; /* 3-D */ - - for (bandno = 0; bandno < totnumbands; bandno++) { - double stepsize; - int resno, level[3], orient, gain; - - /* Bandno: 0 - LLL 1 - LHL - 2 - HLL 3 - HHL - 4 - LLH 5 - LHH - 6 - HLH 7 - HHH */ - - resno = (bandno == 0) ? 0 : ( (bandno <= 3 * diff) ? ((bandno - 1) / 3 + 1) : ((bandno + 4*diff - 1) / 7 + 1)); - orient = (bandno == 0) ? 0 : ( (bandno <= 3 * diff) ? ((bandno - 1) % 3 + 1) : ((bandno + 4*diff - 1) % 7 + 1)); - level[0] = tccp->numresolution[0] - 1 - resno; - level[1] = tccp->numresolution[1] - 1 - resno; - level[2] = tccp->numresolution[2] - 1 - resno; - - /* Gain: 0 - LLL 1 - LHL - 1 - HLL 2 - HHL - 1 - LLH 2 - LHH - 2 - HLH 3 - HHH */ - gain = (tccp->reversible == 0) ? 0 : ( (orient == 0) ? 0 : - ( ((orient == 1) || (orient == 2) || (orient == 4)) ? 1 : - (((orient == 3) || (orient == 5) || (orient == 6)) ? 2 : 3)) ); - - if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) { - stepsize = 1.0; - } else { - double norm = dwt_getnorm(orient,level,tccp->dwtid); //Fetch norms if irreversible transform (by the moment only I9.7) - stepsize = (1 << (gain + 1)) / norm; - } - //fprintf(stdout,"[INFO] Bandno: %d Orient: %d Level: %d %d %d Stepsize: %f\n",bandno,orient,level[0],level[1],level[2],stepsize); - dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]); - } -} - - - diff --git a/jp3d/libjp3dvm/dwt.h b/jp3d/libjp3dvm/dwt.h deleted file mode 100755 index afb1ba4f..00000000 --- a/jp3d/libjp3dvm/dwt.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyrigth (c) 2006, Mónica Díez, LPI-UVA, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __DWT_H -#define __DWT_H -/** -@file dwt.h -@brief Implementation of a discrete wavelet transform (DWT) - -The functions in DWT.C have for goal to realize forward and inverse discret wavelet -transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in -DWT.C are used by some function in TCD.C. -*/ - -/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ -/*@{*/ - -/** -DCCS-LIWT properties -*/ - - -typedef struct opj_wtfilt { - double *LPS; - int lenLPS; - double *HPS; - int lenHPS; -} opj_wtfilt_t; -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Forward 5-3 wavelet tranform in 3-D. -Apply a reversible DWT transform to a component of an volume. -@param tilec Tile component information (current tile) -@param dwtid Number of identification of wavelet kernel(s) used in DWT in each direction -*/ -void dwt_encode(opj_tcd_tilecomp_t * tilec, int dwtid[3]); -/** -Inverse 5-3 wavelet tranform in 3-D. -Apply a reversible inverse DWT transform to a component of an volume. -@param tilec Tile component information (current tile) -@param stops Number of decoded resolution levels in each dimension -@param dwtid Number of identification of wavelet kernel(s) used in DWT in each dimension -*/ -void dwt_decode(opj_tcd_tilecomp_t * tilec, int stops[3], int dwtid[3]); -/* ----------------------------------------------------------------------- */ -/** -Get the gain of a subband for the reversible 3-D DWT. -@param orient Number that identifies the subband (0->LLL, 1->HLL, 2->LHL, 3->HHL, 4->LLH, 5->HLH, 6->LHH, 7->HHH) -@param reversible Wavelet transformation type -@return Returns 0 if orient = 0, returns 1 if orient = 1,2 or 4, returns 2 if orient = 3,5 or 6, returns 3 otherwise -*/ -int dwt_getgain(int orient, int reversible); -/** -Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT or irreversible 9-7 in 3-D. -@param orient Band of the wavelet function -@param level Levels of the wavelet function in X,Y,Z axis -@param dwtid Wavelet transformation identifier -@return Returns the norm of the wavelet function -*/ -double dwt_getnorm(int orient, int level[3], int dwtid[3]); -/* ----------------------------------------------------------------------- */ -/** -Calcula el valor del escalón de cuantificación correspondiente a cada subbanda. -@param tccp Tile component coding parameters -@param prec Precision of data -*/ -void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec); -/*@}*/ - -#endif /* __DWT_H */ diff --git a/jp3d/libjp3dvm/event.c b/jp3d/libjp3dvm/event.c deleted file mode 100755 index 6fe8ae2e..00000000 --- a/jp3d/libjp3dvm/event.c +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -// ========================================================== -// Utility functions -// ========================================================== - -#ifndef WIN32 -static char* -i2a(unsigned i, char *a, unsigned r) { - if (i/r > 0) a = i2a(i/r,a,r); - *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r]; - return a+1; -} - -/** - Transforms integer i into an ascii string and stores the result in a; - string is encoded in the base indicated by r. - @param i Number to be converted - @param a String result - @param r Base of value; must be in the range 2 - 36 - @return Returns a -*/ -static char * -_itoa(int i, char *a, int r) { - r = ((r < 2) || (r > 36)) ? 10 : r; - if(i < 0) { - *a = '-'; - *i2a(-i, a+1, r) = 0; - } - else *i2a(i, a, r) = 0; - return a; -} - -#endif // !WIN32 - -/* ----------------------------------------------------------------------- */ - -opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) { - if(cinfo) { - opj_event_mgr_t *previous = cinfo->event_mgr; - cinfo->event_mgr = event_mgr; - cinfo->client_data = context; - return previous; - } - - return NULL; -} - -bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) { -#define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */ - opj_msg_callback msg_handler = NULL; - - opj_event_mgr_t *event_mgr = cinfo->event_mgr; - if(event_mgr != NULL) { - switch(event_type) { - case EVT_ERROR: - msg_handler = event_mgr->error_handler; - break; - case EVT_WARNING: - msg_handler = event_mgr->warning_handler; - break; - case EVT_INFO: - msg_handler = event_mgr->info_handler; - break; - default: - break; - } - if(msg_handler == NULL) { - return false; - } - } else { - return false; - } - - if ((fmt != NULL) && (event_mgr != NULL)) { - va_list arg; - int str_length, i, j; - char message[MSG_SIZE]; - memset(message, 0, MSG_SIZE); - /* initialize the optional parameter list */ - va_start(arg, fmt); - /* check the length of the format string */ - str_length = (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt); - /* parse the format string and put the result in 'message' */ - for (i = 0, j = 0; i < str_length; ++i) { - if (fmt[i] == '%') { - if (i + 1 < str_length) { - switch(tolower(fmt[i + 1])) { - case '%' : - message[j++] = '%'; - break; - case 'o' : /* octal numbers */ - { - char tmp[16]; - _itoa(va_arg(arg, int), tmp, 8); - strcat(message, tmp); - j += strlen(tmp); - ++i; - break; - } - case 'i' : /* decimal numbers */ - case 'd' : - { - char tmp[16]; - _itoa(va_arg(arg, int), tmp, 10); - strcat(message, tmp); - j += strlen(tmp); - ++i; - break; - } - case 'x' : /* hexadecimal numbers */ - { - char tmp[16]; - _itoa(va_arg(arg, int), tmp, 16); - strcat(message, tmp); - j += strlen(tmp); - ++i; - break; - } - case 's' : /* strings */ - { - char *tmp = va_arg(arg, char*); - strcat(message, tmp); - j += strlen(tmp); - ++i; - break; - } - case 'f' : /* floats */ - { - char tmp[16]; - double value = va_arg(arg, double); - sprintf(tmp, "%f", value); - strcat(message, tmp); - j += strlen(tmp); - ++i; - break; - } - }; - } else { - message[j++] = fmt[i]; - } - } else { - message[j++] = fmt[i]; - }; - } - /* deinitialize the optional parameter list */ - va_end(arg); - - /* output the message to the user program */ - msg_handler(message, cinfo->client_data); - } - - return true; -} - diff --git a/jp3d/libjp3dvm/event.h b/jp3d/libjp3dvm/event.h deleted file mode 100755 index acc4fc3e..00000000 --- a/jp3d/libjp3dvm/event.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __EVENT_H -#define __EVENT_H -/** -@file event.h -@brief Implementation of a event callback system - -The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user. -*/ - -#define EVT_ERROR 1 /**< Error event type */ -#define EVT_WARNING 2 /**< Warning event type */ -#define EVT_INFO 4 /**< Debug event type */ - -/** @defgroup EVENT EVENT - Implementation of a event callback system */ -/*@{*/ - -/** @name Funciones generales (see also openjpeg.h) */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Write formatted data to a string and send the string to a user callback. -@param cinfo Codec context info -@param event_type Event type or callback to use to send the message -@param fmt Format-control string (plus optionnal arguments) -@return Returns true if successful, returns false otherwise -*/ -bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __EVENT_H */ diff --git a/jp3d/libjp3dvm/fix.h b/jp3d/libjp3dvm/fix.h deleted file mode 100755 index f2113b5a..00000000 --- a/jp3d/libjp3dvm/fix.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __FIX_H -#define __FIX_H - -#if defined(_MSC_VER) || defined(__BORLANDC__) -#define int64 __int64 -#else -#define int64 long long -#endif - -/** -@file fix.h -@brief Implementation of operations of specific multiplication (FIX) - -The functions in FIX.H have for goal to realize specific multiplication. -*/ - -/** @defgroup FIX FIX - Implementation of operations of specific multiplication */ -/*@{*/ - -/** -Multiply two fixed-precision rational numbers. -@param a -@param b -@return Returns a * b -*/ -static int fix_mul(int a, int b) { - int64 temp = (int64) a * (int64) b >> 12; - return (int) ((temp >> 1) + (temp & 1)) ; -} - -/*@}*/ - -#endif /* __FIX_H */ diff --git a/jp3d/libjp3dvm/int.h b/jp3d/libjp3dvm/int.h deleted file mode 100755 index 99ab72bb..00000000 --- a/jp3d/libjp3dvm/int.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __INT_H -#define __INT_H -/** -@file int.h -@brief Implementation of operations on integers (INT) - -The functions in INT.H have for goal to realize operations on integers. -*/ - -/** @defgroup INT INT - Implementation of operations on integers */ -/*@{*/ - -/** @name Funciones generales (see also openjpeg.h) */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Get the minimum of two integers -@return Returns a if a < b else b -*/ -static int int_min(int a, int b) { - return a < b ? a : b; -} -/** -Get the maximum of two integers -@return Returns a if a > b else b -*/ -static int int_max(int a, int b) { - return (a > b) ? a : b; -} -/** -Clamp an integer inside an interval -@return -
    -
  • Returns a if (min < a < max) -
  • Returns max if (a > max) -
  • Returns min if (a < min) -
-*/ -static int int_clamp(int a, int min, int max) { - if (a < min) - return min; - if (a > max) - return max; - return a; -} -/** -@return Get absolute value of integer -*/ -static int int_abs(int a) { - return a < 0 ? -a : a; -} - -static double dbl_abs(double a) { - return a < 0 ? -a : a; -} -/** -Divide an integer and round upwards -@return Returns a divided by b -*/ -static int int_ceildiv(int a, int b) { - return (a + b - 1) / b; -} -/** -Divide an integer by a power of 2 and round upwards -@return Returns a divided by 2^b -*/ -static int int_ceildivpow2(int a, int b) { - return (a + (1 << b) - 1) >> b; -} -/** -Divide an integer by a power of 2 and round downwards -@return Returns a divided by 2^b -*/ -static int int_floordivpow2(int a, int b) { - return a >> b; -} -/** -Get logarithm of an integer and round downwards -@return Returns log2(a) -*/ -static int int_floorlog2(int a) { - int l; - for (l = 0; a > 1; l++) { - a >>= 1; - } - return l; -} -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif diff --git a/jp3d/libjp3dvm/jp3d.c b/jp3d/libjp3dvm/jp3d.c deleted file mode 100755 index 4a5527b7..00000000 --- a/jp3d/libjp3dvm/jp3d.c +++ /dev/null @@ -1,2328 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */ -/*@{*/ - -/** @name Funciones locales */ -/*@{*/ - -/** -Write the SOC marker (Start Of Codestream) -@param j3d J3D handle -*/ -static void j3d_write_soc(opj_j3d_t *j3d); -/** -Read the SOC marker (Start of Codestream) -@param j3d J3D handle -*/ -static void j3d_read_soc(opj_j3d_t *j3d); -/** -Write the SIZ marker (2D volume and tile size) -@param j3d J3D handle -*/ -static void j3d_write_siz(opj_j3d_t *j3d); -/** -Read the SIZ marker (2D volume and tile size) -@param j3d J3D handle -*/ -static void j3d_read_siz(opj_j3d_t *j3d); -/** -Write the ZSI marker (3rd volume and tile size) -@param j3d J3D handle -*/ -static void j3d_write_zsi(opj_j3d_t *j3d); -/** -Read the ZSI marker (3rd volume and tile size) -@param j3d J3D handle -*/ -static void j3d_read_zsi(opj_j3d_t *j3d); -/** -Write the COM marker (comment) -@param j3d J3D handle -*/ -static void j3d_write_com(opj_j3d_t *j3d); -/** -Read the COM marker (comment) -@param j3d J3D handle -*/ -static void j3d_read_com(opj_j3d_t *j3d); -/** -Write the value concerning the specified component in the marker COD and COC -@param j3d J3D handle -@param compno Number of the component concerned by the information written -*/ -static void j3d_write_cox(opj_j3d_t *j3d, int compno); -/** -Read the value concerning the specified component in the marker COD and COC -@param j3d J3D handle -@param compno Number of the component concerned by the information read -*/ -static void j3d_read_cox(opj_j3d_t *j3d, int compno); -/** -Write the COD marker (coding style default) -@param j3d J3D handle -*/ -static void j3d_write_cod(opj_j3d_t *j3d); -/** -Read the COD marker (coding style default) -@param j3d J3D handle -*/ -static void j3d_read_cod(opj_j3d_t *j3d); -/** -Write the COC marker (coding style component) -@param j3d J3D handle -@param compno Number of the component concerned by the information written -*/ -static void j3d_write_coc(opj_j3d_t *j3d, int compno); -/** -Read the COC marker (coding style component) -@param j3d J3D handle -*/ -static void j3d_read_coc(opj_j3d_t *j3d); -/** -Write the value concerning the specified component in the marker QCD and QCC -@param j3d J3D handle -@param compno Number of the component concerned by the information written -*/ -static void j3d_write_qcx(opj_j3d_t *j3d, int compno); -/** -Read the value concerning the specified component in the marker QCD and QCC -@param j3d J3D handle -@param compno Number of the component concern by the information read -@param len Length of the information in the QCX part of the marker QCD/QCC -*/ -static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len); -/** -Write the QCD marker (quantization default) -@param j3d J3D handle -*/ -static void j3d_write_qcd(opj_j3d_t *j3d); -/** -Read the QCD marker (quantization default) -@param j3d J3D handle -*/ -static void j3d_read_qcd(opj_j3d_t *j3d); -/** -Write the QCC marker (quantization component) -@param j3d J3D handle -@param compno Number of the component concerned by the information written -*/ -static void j3d_write_qcc(opj_j3d_t *j3d, int compno); -/** -Read the QCC marker (quantization component) -@param j3d J3D handle -*/ -static void j3d_read_qcc(opj_j3d_t *j3d); -/** -Write the POC marker (progression order change) -@param j3d J3D handle -*/ -static void j3d_write_poc(opj_j3d_t *j3d); -/** -Read the POC marker (progression order change) -@param j3d J3D handle -*/ -static void j3d_read_poc(opj_j3d_t *j3d); -/** -Read the CRG marker (component registration) -@param j3d J3D handle -*/ -static void j3d_read_crg(opj_j3d_t *j3d); -/** -Read the TLM marker (tile-part lengths) -@param j3d J3D handle -*/ -static void j3d_read_tlm(opj_j3d_t *j3d); -/** -Read the PLM marker (packet length, main header) -@param j3d J3D handle -*/ -static void j3d_read_plm(opj_j3d_t *j3d); -/** -Read the PLT marker (packet length, tile-part header) -@param j3d J3D handle -*/ -static void j3d_read_plt(opj_j3d_t *j3d); -/** -Read the PPM marker (packet packet headers, main header) -@param j3d J3D handle -*/ -static void j3d_read_ppm(opj_j3d_t *j3d); -/** -Read the PPT marker (packet packet headers, tile-part header) -@param j3d J3D handle -*/ -static void j3d_read_ppt(opj_j3d_t *j3d); -/** -Write the SOT marker (start of tile-part) -@param j3d J3D handle -*/ -static void j3d_write_sot(opj_j3d_t *j3d); -/** -Read the SOT marker (start of tile-part) -@param j3d J3D handle -*/ -static void j3d_read_sot(opj_j3d_t *j3d); -/** -Write the SOD marker (start of data) -@param j3d J3D handle -@param tile_coder Pointer to a TCD handle -*/ -static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder); -/** -Read the SOD marker (start of data) -@param j3d J3D handle -*/ -static void j3d_read_sod(opj_j3d_t *j3d); -/** -Write the RGN marker (region-of-interest) -@param j3d J3D handle -@param compno Number of the component concerned by the information written -@param tileno Number of the tile concerned by the information written -*/ -static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno); -/** -Read the RGN marker (region-of-interest) -@param j3d J3D handle -*/ -static void j3d_read_rgn(opj_j3d_t *j3d); -/** -Write the EOC marker (end of codestream) -@param j3d J3D handle -*/ -static void j3d_write_eoc(opj_j3d_t *j3d); -/** -Read the EOC marker (end of codestream) -@param j3d J3D handle -*/ -static void j3d_read_eoc(opj_j3d_t *j3d); -/** -Read an unknown marker -@param j3d J3D handle -*/ -static void j3d_read_unk(opj_j3d_t *j3d); -/** -Write the CAP marker (extended capabilities) -@param j3d J3D handle -*/ -static void j3d_write_cap(opj_j3d_t *j3d); -/** -Read the CAP marker (extended capabilities) -@param j3d J3D handle -*/ -static void j3d_read_cap(opj_j3d_t *j3d); -/** -Write the DCO marker (Variable DC offset) -@param j3d J3D handle -*/ -static void j3d_write_dco(opj_j3d_t *j3d); -/** -Read the DCO marker (Variable DC offset) -@param j3d J3D handle -*/ -static void j3d_read_dco(opj_j3d_t *j3d); -/** -Write the ATK marker (arbitrary transformation kernel) -@param j3d J3D handle -*/ -static void j3d_write_atk(opj_j3d_t *j3d); -/** -Read the ATK marker (arbitrary transformation kernel) -@param j3d J3D handle -*/ -static void j3d_read_atk(opj_j3d_t *j3d); -/** -Write the CBD marker (component bit depth definition) -@param j3d J3D handle -*/ -static void j3d_write_cbd(opj_j3d_t *j3d); -/** -Read the CBD marker (component bit depth definition) -@param j3d J3D handle -*/ -static void j3d_read_cbd(opj_j3d_t *j3d); -/** -Write the MCT marker (multiple component transfomation definition) -@param j3d J3D handle -*/ -static void j3d_write_mct(opj_j3d_t *j3d); -/** -Read the MCT marker (multiple component transfomation definition) -@param j3d J3D handle -*/ -static void j3d_read_mct(opj_j3d_t *j3d); -/** -Write the MCC marker (multiple component transfomation collection) -@param j3d J3D handle -*/ -static void j3d_write_mcc(opj_j3d_t *j3d); -/** -Read the MCC marker (multiple component transfomation collection) -@param j3d J3D handle -*/ -static void j3d_read_mcc(opj_j3d_t *j3d); -/** -Write the MCO marker (multiple component transfomation ordering) -@param j3d J3D handle -*/ -static void j3d_write_mco(opj_j3d_t *j3d); -/** -Read the MCO marker (multiple component transfomation ordering) -@param j3d J3D handle -*/ -static void j3d_read_mco(opj_j3d_t *j3d); -/** -Write the NLT marker (non-linearity point transformation) -@param j3d J3D handle -*/ -static void j3d_write_nlt(opj_j3d_t *j3d); -/** -Read the NLT marker (non-linearity point transformation) -@param j3d J3D handle -*/ -static void j3d_read_nlt(opj_j3d_t *j3d); -/*@}*/ - -/* ----------------------------------------------------------------------- */ - -void j3d_dump_volume(FILE *fd, opj_volume_t * vol) { - int compno; - fprintf(fd, "volume {\n"); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1, vol->z1); - fprintf(fd, " numcomps=%d\n", vol->numcomps); - for (compno = 0; compno < vol->numcomps; compno++) { - opj_volume_comp_t *comp = &vol->comps[compno]; - fprintf(fd, " comp %d {\n", compno); - fprintf(fd, " dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz); - fprintf(fd, " prec=%d\n", comp->prec); - fprintf(fd, " sgnd=%d\n", comp->sgnd); - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); -} - -void j3d_dump_cp(FILE *fd, opj_volume_t * vol, opj_cp_t * cp) { - int tileno, compno, layno, bandno, resno, numbands; - fprintf(fd, "coding parameters {\n"); - fprintf(fd, " tx0=%d, ty0=%d, tz0=%d\n", cp->tx0, cp->ty0, cp->tz0); - fprintf(fd, " tdx=%d, tdy=%d, tdz=%d\n", cp->tdx, cp->tdy, cp->tdz); - fprintf(fd, " tw=%d, th=%d, tl=%d\n", cp->tw, cp->th, cp->tl); - fprintf(fd, " transform format: %d\n", cp->transform_format); - fprintf(fd, " encoding format: %d\n", cp->encoding_format); - for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) { - opj_tcp_t *tcp = &cp->tcps[tileno]; - fprintf(fd, " tile %d {\n", tileno); - fprintf(fd, " csty=%x\n", tcp->csty); - fprintf(fd, " prg=%d\n", tcp->prg); - fprintf(fd, " numlayers=%d\n", tcp->numlayers); - fprintf(fd, " mct=%d\n", tcp->mct); - fprintf(fd, " rates="); - for (layno = 0; layno < tcp->numlayers; layno++) { - fprintf(fd, "%f ", tcp->rates[layno]); - } - fprintf(fd, "\n"); - fprintf(fd, " first=%d\n", tcp->first); - for (compno = 0; compno < vol->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - fprintf(fd, " comp %d {\n", compno); - fprintf(fd, " csty=%x\n", tccp->csty); - fprintf(fd, " numresx=%d, numresy=%d, numresz=%d\n", tccp->numresolution[0], tccp->numresolution[1], tccp->numresolution[2]); - fprintf(fd, " cblkw=%d, cblkh=%d, cblkl=%d\n", tccp->cblk[0], tccp->cblk[1], tccp->cblk[2]); - fprintf(fd, " cblksty=%x\n", tccp->cblksty); - fprintf(fd, " qntsty=%d\n", tccp->qntsty); - fprintf(fd, " numgbits=%d\n", tccp->numgbits); - fprintf(fd, " roishift=%d\n", tccp->roishift); - fprintf(fd, " reversible=%d\n", tccp->reversible); - fprintf(fd, " dwtidx=%d dwtidy=%d dwtidz=%d\n", tccp->dwtid[0], tccp->dwtid[1], tccp->dwtid[2]); - if (tccp->atk != NULL) { - fprintf(fd, " atk.index=%d\n", tccp->atk->index); - fprintf(fd, " atk.coeff_typ=%d\n", tccp->atk->coeff_typ); - fprintf(fd, " atk.filt_cat=%d\n", tccp->atk->filt_cat); - fprintf(fd, " atk.exten=%d\n", tccp->atk->exten); - fprintf(fd, " atk.minit=%d\n", tccp->atk->minit); - fprintf(fd, " atk.wt_typ=%d\n", tccp->atk->wt_typ); - } - fprintf(fd, " stepsizes of bands="); - numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : - ( (cp->transform_format == TRF_2D_DWT) ? (tccp->numresolution[0] * 3 - 2) : - (tccp->numresolution[0] * 7 - 6) - 4 *(tccp->numresolution[0] - tccp->numresolution[2]) ); - for (bandno = 0; bandno < numbands; bandno++) { - fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,tccp->stepsizes[bandno].expn); - } - fprintf(fd, "\n"); - - if (tccp->csty & J3D_CCP_CSTY_PRT) { - fprintf(fd, " prcw="); - for (resno = 0; resno < tccp->numresolution[0]; resno++) { - fprintf(fd, "%d ", tccp->prctsiz[0][resno]); - } - fprintf(fd, "\n"); - fprintf(fd, " prch="); - for (resno = 0; resno < tccp->numresolution[0]; resno++) { - fprintf(fd, "%d ", tccp->prctsiz[1][resno]); - } - fprintf(fd, "\n"); - fprintf(fd, " prcl="); - for (resno = 0; resno < tccp->numresolution[0]; resno++) { - fprintf(fd, "%d ", tccp->prctsiz[2][resno]); - } - fprintf(fd, "\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); -} - -/* ----------------------------------------------------------------------- -Extended capabilities -------------------------------------------------------------------------*/ - -static void j3d_write_cap(opj_j3d_t *j3d){ - int len,lenp; - - opj_cio_t *cio = j3d->cio; - cio_write(cio, J3D_MS_CAP, 2); /* CAP */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio,J3D_CAP_10, 4); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lsiz */ - cio_seek(cio, lenp + len); - -} -static void j3d_read_cap(opj_j3d_t *j3d){ - int len, Cap; - opj_cio_t *cio = j3d->cio; - /*cio_read(cio, 2); CAP */ - len = cio_read(cio, 2); - Cap = cio_read(cio, 4); -} -static void j3d_write_zsi(opj_j3d_t *j3d) { - int i; - int lenp, len; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - - cio_write(cio, J3D_MS_ZSI, 2); /* ZSI */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, volume->z1, 4); /* Zsiz */ - cio_write(cio, volume->z0, 4); /* Z0siz */ - cio_write(cio, cp->tdz, 4); /* ZTsiz */ - cio_write(cio, cp->tz0, 4); /* ZT0siz */ - for (i = 0; i < volume->numcomps; i++) { - cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */ - } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lsiz */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_zsi(opj_j3d_t *j3d) { - int len, i; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - - len = cio_read(cio, 2); /* Lsiz */ - volume->z1 = cio_read(cio, 4); /* Zsiz */ - volume->z0 = cio_read(cio, 4); /* Z0siz */ - cp->tdz = cio_read(cio, 4); /* ZTsiz */ - cp->tz0 = cio_read(cio, 4); /* ZT0siz */ - for (i = 0; i < volume->numcomps; i++) { - volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */ - } - - //Initialization of volume - cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx); - cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy); - cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz); - cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t)); - cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int)); - cp->tileno_size = 0; - - for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) { - cp->tcps[i].POC = 0; - cp->tcps[i].numpocs = 0; - cp->tcps[i].first = 1; - } - - /* Initialization for PPM marker (Packets header)*/ - cp->ppm = 0; - cp->ppm_data = NULL; - cp->ppm_data_first = NULL; - cp->ppm_previous = 0; - cp->ppm_store = 0; - - j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps); - for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) { - cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps); - } - j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *)); - j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int)); - j3d->state = J3D_STATE_MH; - -} -static void j3d_write_dco(opj_j3d_t *j3d){ - int lenp, len, i; - int dcotype; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - - dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */ - cio_write(cio, J3D_MS_DCO, 2); /* DCO */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, dcotype, 1); - if (dcotype == 0) { - for (i = 0; i < volume->numcomps; i++) - cio_write(cio, volume->comps[i].dcoffset, 1); /* SPdco_i */ - } else if (dcotype == 1) { - for (i = 0; i < volume->numcomps; i++){ - cio_write(cio, volume->comps[i].dcoffset, 1); /* SPdco_i */ - opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset); - } - } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Ldco */ - cio_seek(cio, lenp + len); - -} -static void j3d_read_dco(opj_j3d_t *j3d){ - int len, i; - int dcotype; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - - len = cio_read(cio, 2); /* Lsiz */ - dcotype = cio_read(cio, 1); //offset 8bit unsigned / 16bit signed integers - if (dcotype == 0) { - for (i = 0; i < volume->numcomps; i++) { - volume->comps[i].dcoffset = cio_read(cio, 1); - if (volume->comps[i].dcoffset > 128) - volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256; - } - } else if (dcotype == 1) { - for (i = 0; i < volume->numcomps; i++) { - volume->comps[i].dcoffset = cio_read(cio, 1); - if (volume->comps[i].dcoffset > 128) - volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256; - } - } - -} -static void j3d_write_atk(opj_j3d_t *j3d){ - int lenp, len, s, k; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_atk_t *atk = j3d->cp->tcps->tccps->atk; - - cio_write(cio, J3D_MS_ATK, 2); /* ATK */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11) - + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2); /* Satk */ - if (atk->wt_typ == J3D_ATK_IRR) - cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ); - cio_write(cio, atk->Natk, 1); - for (s = 0; s < atk->Natk; s++){ - if (atk->filt_cat == J3D_ATK_ARB) - cio_write(cio, atk->Oatk[s], 1); - if (atk->wt_typ == J3D_ATK_REV){ - cio_write(cio, atk->Eatk[s], 1); - cio_write(cio, atk->Batk[s], 1); - } - cio_write(cio, atk->LCatk[s], 1); - for (k = 0; k < atk->LCatk[s]; k++) - cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ); - } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Latk */ - cio_seek(cio, lenp + len); -} -static void j3d_read_atk(opj_j3d_t *j3d){ - int len, i, Satk, k; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - opj_atk_t *atk = cp->tcps->tccps->atk; - - len = cio_read(cio, 2); /* Latk */ - Satk = cio_read(cio, 2); - atk->index = Satk & 0x00ff; - atk->coeff_typ = Satk >> 8 & 0x0007; - atk->filt_cat = Satk >> 11 & 0x0001; - atk->wt_typ = Satk >> 12 & 0x0001; - atk->minit = Satk >> 13 & 0x0001; - atk->exten = Satk >> 14 & 0x0001; - if (atk->wt_typ == J3D_ATK_IRR) - atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0); - atk->Natk = cio_read(cio, 1); - for (i = 0; i < atk->Natk; i++) { - if (atk->filt_cat == J3D_ATK_ARB) - atk->Oatk[i] = cio_read(cio, 1); - if (atk->wt_typ == J3D_ATK_REV){ - atk->Eatk[i] = cio_read(cio, 1); - atk->Batk[i] = cio_read(cio, 1); - } - atk->LCatk[i] = cio_read(cio, 1); - for (k = 0; k < atk->LCatk[i]; k++) - atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0); - } -} -static void j3d_write_cbd(opj_j3d_t *j3d){ -} -static void j3d_read_cbd(opj_j3d_t *j3d){ -} -static void j3d_write_mct(opj_j3d_t *j3d){ -} -static void j3d_read_mct(opj_j3d_t *j3d){ -} -static void j3d_write_mcc(opj_j3d_t *j3d){ -} -static void j3d_read_mcc(opj_j3d_t *j3d){ -} -static void j3d_write_mco(opj_j3d_t *j3d){ -} -static void j3d_read_mco(opj_j3d_t *j3d){ -} -static void j3d_write_nlt(opj_j3d_t *j3d){ -} -static void j3d_read_nlt(opj_j3d_t *j3d){ -} -/* ----------------------------------------------------------------------- -15444-1 codestream syntax -------------------------------------------------------------------------*/ -static void j3d_write_soc(opj_j3d_t *j3d) { - opj_cio_t *cio = j3d->cio; - cio_write(cio, J3D_MS_SOC, 2); -} - -static void j3d_read_soc(opj_j3d_t *j3d) { - j3d->state = J3D_STATE_MHSIZ; -} - -static void j3d_write_siz(opj_j3d_t *j3d) { - int i; - int lenp, len; - int Rsiz; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - - cio_write(cio, J3D_MS_SIZ, 2); /* SIZ */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - //cio_write(cio, 0, 2); /* Rsiz (capabilities of 15444-1 only) */ - Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/ - cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/ - cio_write(cio, volume->x1, 4); /* Xsiz */ - cio_write(cio, volume->y1, 4); /* Ysiz */ - cio_write(cio, volume->x0, 4); /* X0siz */ - cio_write(cio, volume->y0, 4); /* Y0siz */ - cio_write(cio, cp->tdx, 4); /* XTsiz */ - cio_write(cio, cp->tdy, 4); /* YTsiz */ - cio_write(cio, cp->tx0, 4); /* XT0siz */ - cio_write(cio, cp->ty0, 4); /* YT0siz */ - cio_write(cio, volume->numcomps, 2); /* Csiz */ - for (i = 0; i < volume->numcomps; i++) { - cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1); /* Ssiz_i */ - cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */ - cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */ - } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lsiz */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_siz(opj_j3d_t *j3d) { - int len, i; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - opj_cp_t *cp = j3d->cp; - - len = cio_read(cio, 2); /* Lsiz */ - cp->rsiz = cio_read(cio, 2); /* Rsiz (capabilities) */ - volume->x1 = cio_read(cio, 4); /* Xsiz */ - volume->y1 = cio_read(cio, 4); /* Ysiz */ - volume->x0 = cio_read(cio, 4); /* X0siz */ - volume->y0 = cio_read(cio, 4); /* Y0siz */ - cp->tdx = cio_read(cio, 4); /* XTsiz */ - cp->tdy = cio_read(cio, 4); /* YTsiz */ - cp->tx0 = cio_read(cio, 4); /* XT0siz */ - cp->ty0 = cio_read(cio, 4); /* YT0siz */ - - volume->numcomps = cio_read(cio, 2); /* Csiz */ - volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t)); - for (i = 0; i < volume->numcomps; i++) { - int tmp, j; - tmp = cio_read(cio, 1); /* Ssiz_i */ - volume->comps[i].prec = (tmp & 0x7f) + 1; - volume->comps[i].sgnd = tmp >> 7; - volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */ - volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */ - for (j = 0; j < 3; j++) { - volume->comps[i].resno_decoded[j] = 0; /* number of resolution decoded */ - volume->comps[i].factor[j] = 0; /* reducing factor per component */ - } - } - - if (j3d->cinfo->codec_format == CODEC_J2K){ - volume->z1 = 1; - volume->z0 = 0; - volume->numslices = 1; - cp->tdz = 1; - cp->tz0 = 0; - for (i = 0; i < volume->numcomps; i++) - volume->comps[i].dz = 1; - - //Initialization of volume - cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx); - cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy); - cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz); - cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t)); - cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int)); - cp->tileno_size = 0; - - for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) { - cp->tcps[i].POC = 0; - cp->tcps[i].numpocs = 0; - cp->tcps[i].first = 1; - } - - /* Initialization for PPM marker (Packets header)*/ - cp->ppm = 0; - cp->ppm_data = NULL; - cp->ppm_data_first = NULL; - cp->ppm_previous = 0; - cp->ppm_store = 0; - - j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps); - for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) { - cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps); - } - j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *)); - j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int)); - j3d->state = J3D_STATE_MH; - } -} - - - -static void j3d_write_com(opj_j3d_t *j3d) { - unsigned int i; - int lenp, len; - - opj_cio_t *cio = j3d->cio; - - cio_write(cio, J3D_MS_COM, 2); - lenp = cio_tell(cio); - cio_skip(cio, 2); - //cio_write(cio, 0, 2); - cio_write(cio, j3d->cp->transform_format,1); - cio_write(cio, j3d->cp->encoding_format,1); - //opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format); - if (j3d->cp->comment != NULL) { - char *comment = j3d->cp->comment; - for (i = 0; i < strlen(comment); i++) { - cio_write(cio, comment[i], 1); - } - } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); - cio_seek(cio, lenp + len); -} - -static void j3d_read_com(opj_j3d_t *j3d) { - int len; - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); - - j3d->cp->transform_format = (OPJ_TRANSFORM) cio_read(cio, 1); - j3d->cp->encoding_format = (OPJ_ENTROPY_CODING) cio_read(cio, 1); - //opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format); - - cio_skip(cio, len - 4); //posible comments -} - -static void j3d_write_cox(opj_j3d_t *j3d, int compno) { - int i; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = &cp->tcps[j3d->curtileno]; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j3d->cio; - - cio_write(cio, tccp->numresolution[0] - 1, 1); /* SPcox (D) No of decomposition levels in x-axis*/ - if (j3d->cinfo->codec_format == CODEC_J3D) { - cio_write(cio, tccp->numresolution[1] - 1, 1); /* SPcox (E) No of decomposition levels in y-axis*/ - cio_write(cio, tccp->numresolution[2] - 1, 1); /* SPcox (F) No of decomposition levels in z-axis*/ - } - /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/ - cio_write(cio, tccp->cblk[0] - 2, 1); /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/ - cio_write(cio, tccp->cblk[1] - 2, 1); /* SPcox (H) Cblk height*/ - if (j3d->cinfo->codec_format == CODEC_J3D) { - cio_write(cio, tccp->cblk[2] - 2, 1); /* SPcox (I) Cblk depth*/ - } - cio_write(cio, tccp->cblksty, 1); /* SPcox (J) Cblk style*/ - cio_write(cio, tccp->dwtid[0], 1); /* SPcox (K) WT in x-axis 15444-2 Table A10*/ - if (j3d->cinfo->codec_format == CODEC_J3D) { - cio_write(cio, tccp->dwtid[1], 1); /* SPcox (L) WT in y-axis 15444-2 Table A10*/ - cio_write(cio, tccp->dwtid[2], 1); /* SPcox (M) WT in z-axis 15444-2 Table A10*/ - } - - if (tccp->csty & J3D_CCP_CSTY_PRT) { - for (i = 0; i < tccp->numresolution[0]; i++) { - if (i < tccp->numresolution[2]) - cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2); /* SPcox (N_i) Table A9*/ - else - if (j3d->cinfo->codec_format == CODEC_J3D) - cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2); /* SPcox (N_i) Table A9*/ - else - cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1); /* SPcox (N_i) Table A9*/ } - } -} - -static void j3d_read_cox(opj_j3d_t *j3d, int compno) { - int i; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j3d->cio; - - tccp->numresolution[0] = cio_read(cio, 1) + 1; /* SPcox (D) No of decomposition levels in x-axis*/ - if (j3d->cinfo->codec_format == CODEC_J3D) { - tccp->numresolution[1] = cio_read(cio, 1) + 1; /* SPcox (E) No of decomposition levels in y-axis*/ - tccp->numresolution[2] = cio_read(cio, 1) + 1; /* SPcox (F) No of decomposition levels in z-axis*/ - }else if (j3d->cinfo->codec_format == CODEC_J2K) { - tccp->numresolution[1] = tccp->numresolution[0]; - tccp->numresolution[2] = 1; - } - /* check the reduce value */ - cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]); - cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]); - cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]); - - tccp->cblk[0] = cio_read(cio, 1) + 2; /* SPcox (G) */ - tccp->cblk[1] = cio_read(cio, 1) + 2; /* SPcox (H) */ - if (j3d->cinfo->codec_format == CODEC_J3D) - tccp->cblk[2] = cio_read(cio, 1) + 2; /* SPcox (I) */ - else - tccp->cblk[2] = tccp->cblk[0]; - - tccp->cblksty = cio_read(cio, 1); /* SPcox (J) */ - tccp->dwtid[0] = cio_read(cio, 1); /* SPcox (K) */ - if (j3d->cinfo->codec_format == CODEC_J3D) { - tccp->dwtid[1] = cio_read(cio, 1); /* SPcox (L) */ - tccp->dwtid[2] = cio_read(cio, 1); /* SPcox (M) */ - }else{ - tccp->dwtid[1] = tccp->dwtid[0]; /* SPcox (L) */ - tccp->dwtid[2] = tccp->dwtid[0]; /* SPcox (M) */ - } - tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); //TODO: only valid for irreversible 9x7 WTs - if (tccp->csty & J3D_CP_CSTY_PRT) { - for (i = 0; i < tccp->numresolution[0]; i++) { - int tmp = cio_read(cio, 2); /* SPcox (N_i) */ - tccp->prctsiz[0][i] = tmp & 0xf; - tccp->prctsiz[1][i] = tmp >> 4; - tccp->prctsiz[2][i] = tmp >> 8; - } - } -} - -static void j3d_write_cod(opj_j3d_t *j3d) { - opj_cp_t *cp = NULL; - opj_tcp_t *tcp = NULL; - int lenp, len; - - opj_cio_t *cio = j3d->cio; - - cio_write(cio, J3D_MS_COD, 2); /* COD */ - - lenp = cio_tell(cio); - cio_skip(cio, 2); - - cp = j3d->cp; - tcp = &cp->tcps[j3d->curtileno]; - - /* Scod : Table A-4*/ - cio_write(cio, tcp->csty, 1); /* Scod : Coding style parameters */ - /* SGcod : Table A-5*/ - cio_write(cio, tcp->prg, 1); /* SGcod (A) : Progression order */ - cio_write(cio, tcp->numlayers, 2); /* SGcod (B) : No of layers */ - cio_write(cio, tcp->mct, 1); /* SGcod (C) : Multiple component transformation usage */ - /* SPcod : Table A-6*/ - j3d_write_cox(j3d, 0); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lcod */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_cod(opj_j3d_t *j3d) { - int len, i, pos; - - opj_cio_t *cio = j3d->cio; - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp; - opj_volume_t *volume = j3d->volume; - - /* Lcod */ - len = cio_read(cio, 2); - /* Scod : Table A-4*/ - tcp->csty = cio_read(cio, 1); - /* SGcod : Table A-5*/ - tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); - tcp->numlayers = cio_read(cio, 2); - tcp->mct = cio_read(cio, 1); - - pos = cio_tell(cio); - for (i = 0; i < volume->numcomps; i++) { - tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT; - cio_seek(cio, pos); - j3d_read_cox(j3d, i); - } -} - -static void j3d_write_coc(opj_j3d_t *j3d, int compno) { - int lenp, len; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = &cp->tcps[j3d->curtileno]; - opj_volume_t *volume = j3d->volume; - opj_cio_t *cio = j3d->cio; - - cio_write(cio, J3D_MS_COC, 2); /* COC */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */ - cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */ - - j3d_write_cox(j3d, compno); - - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lcoc */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_coc(opj_j3d_t *j3d) { - int len, compno; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp; - opj_volume_t *volume = j3d->volume; - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); /* Lcoc */ - compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */ - tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */ - j3d_read_cox(j3d, compno); -} - -static void j3d_write_qcx(opj_j3d_t *j3d, int compno) { - int bandno, numbands; - int expn, mant; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = &cp->tcps[j3d->curtileno]; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j3d->cio; - - cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx : Table A28 de 15444-1*/ - - if (j3d->cinfo->codec_format == CODEC_J2K) - numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2; - else if (j3d->cinfo->codec_format == CODEC_J3D) { - int diff = tccp->numresolution[0] - tccp->numresolution[2]; - numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */ - } - - for (bandno = 0; bandno < numbands; bandno++) { - expn = tccp->stepsizes[bandno].expn; - mant = tccp->stepsizes[bandno].mant; - - if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) { - cio_write(cio, expn << 3, 1); /* SPqcx_i */ - } else { - cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */ - } - } -} - -static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) { - int tmp; - int bandno, numbands; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j3d->cio; - - tmp = cio_read(cio, 1); /* Sqcx */ - tccp->qntsty = tmp & 0x1f; - tccp->numgbits = tmp >> 5; - - /*Numbands = 1 si SIQNT - len - 1 si NOQNT - (len - 1) / 2 si SEQNT */ - numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2); - - for (bandno = 0; bandno < numbands; bandno++) { - int expn, mant; - if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) { - expn = cio_read(cio, 1) >> 3; /* SPqcx_i */ - mant = 0; - } else { - tmp = cio_read(cio, 2); /* SPqcx_i */ - expn = tmp >> 11; - mant = tmp & 0x7ff; - } - tccp->stepsizes[bandno].expn = expn; - tccp->stepsizes[bandno].mant = mant; - } - - /* Add Antonin : if scalar_derived -> compute other stepsizes */ - if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) { - for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) { - int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7; - tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands); - tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant; - } - } - /* ddA */ -} - -static void j3d_write_qcd(opj_j3d_t *j3d) { - int lenp, len; - - opj_cio_t *cio = j3d->cio; - - cio_write(cio, J3D_MS_QCD, 2); /* QCD */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - j3d_write_qcx(j3d, 0); /* Sqcd*/ - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lqcd */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_qcd(opj_j3d_t *j3d) { - int len, i, pos; - - opj_cio_t *cio = j3d->cio; - opj_volume_t *volume = j3d->volume; - - len = cio_read(cio, 2); /* Lqcd */ - pos = cio_tell(cio); - for (i = 0; i < volume->numcomps; i++) { - cio_seek(cio, pos); - j3d_read_qcx(j3d, i, len - 2); - } -} - -static void j3d_write_qcc(opj_j3d_t *j3d, int compno) { - int lenp, len; - - opj_cio_t *cio = j3d->cio; - - cio_write(cio, J3D_MS_QCC, 2); /* QCC */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2); /* Cqcc */ - j3d_write_qcx(j3d, compno); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lqcc */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_qcc(opj_j3d_t *j3d) { - int len, compno; - int numcomp = j3d->volume->numcomps; - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); /* Lqcc */ - compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */ - j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2)); -} - -static void j3d_write_poc(opj_j3d_t *j3d) { - int len, numpchgs, i; - - int numcomps = j3d->volume->numcomps; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = &cp->tcps[j3d->curtileno]; - opj_tccp_t *tccp = &tcp->tccps[0]; - opj_cio_t *cio = j3d->cio; - - numpchgs = tcp->numpocs; - cio_write(cio, J3D_MS_POC, 2); /* POC */ - len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs; - cio_write(cio, len, 2); /* Lpoc */ - for (i = 0; i < numpchgs; i++) { - opj_poc_t *poc = &tcp->pocs[i]; - cio_write(cio, poc->resno0, 1); /* RSpoc_i */ - cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ - cio_write(cio, poc->layno1, 2); /* LYEpoc_i */ - poc->layno1 = int_min(poc->layno1, tcp->numlayers); - cio_write(cio, poc->resno1, 1); /* REpoc_i */ - poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]); - cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ - poc->compno1 = int_min(poc->compno1, numcomps); - cio_write(cio, poc->prg, 1); /* Ppoc_i */ - } -} - -static void j3d_read_poc(opj_j3d_t *j3d) { - int len, numpchgs, i, old_poc; - - int numcomps = j3d->volume->numcomps; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp; - opj_tccp_t *tccp = &tcp->tccps[0]; - opj_cio_t *cio = j3d->cio; - - old_poc = tcp->POC ? tcp->numpocs + 1 : 0; - tcp->POC = 1; - len = cio_read(cio, 2); /* Lpoc */ - numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2)); - - for (i = old_poc; i < numpchgs + old_poc; i++) { - opj_poc_t *poc; - poc = &tcp->pocs[i]; - poc->resno0 = cio_read(cio, 1); /* RSpoc_i */ - poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */ - poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */ - poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */ - poc->compno1 = int_min( - cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */ - poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */ - } - - tcp->numpocs = numpchgs + old_poc - 1; -} - -static void j3d_read_crg(opj_j3d_t *j3d) { - int len, i, Xcrg_i, Ycrg_i, Zcrg_i; - - opj_cio_t *cio = j3d->cio; - int numcomps = j3d->volume->numcomps; - - len = cio_read(cio, 2); /* Lcrg */ - for (i = 0; i < numcomps; i++) { - Xcrg_i = cio_read(cio, 2); /* Xcrg_i */ - Ycrg_i = cio_read(cio, 2); /* Ycrg_i */ - Zcrg_i = cio_read(cio, 2); /* Zcrg_i */ - } -} - -static void j3d_read_tlm(opj_j3d_t *j3d) { - int len, Ztlm, Stlm, ST, SP, tile_tlm, i; - long int Ttlm_i, Ptlm_i; - - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); /* Ltlm */ - Ztlm = cio_read(cio, 1); /* Ztlm */ - Stlm = cio_read(cio, 1); /* Stlm */ - ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); - SP = (Stlm >> 6) & 0x01; - tile_tlm = (len - 4) / ((SP + 1) * 2 + ST); - for (i = 0; i < tile_tlm; i++) { - Ttlm_i = cio_read(cio, ST); /* Ttlm_i */ - Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */ - } -} - -static void j3d_read_plm(opj_j3d_t *j3d) { - int len, i, Zplm, Nplm, add, packet_len = 0; - - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); /* Lplm */ - Zplm = cio_read(cio, 1); /* Zplm */ - len -= 3; - while (len > 0) { - Nplm = cio_read(cio, 4); /* Nplm */ - len -= 4; - for (i = Nplm; i > 0; i--) { - add = cio_read(cio, 1); - len--; - packet_len = (packet_len << 7) + add; /* Iplm_ij */ - if ((add & 0x80) == 0) { - /* New packet */ - packet_len = 0; - } - if (len <= 0) - break; - } - } -} - -static void j3d_read_plt(opj_j3d_t *j3d) { - int len, i, Zplt, packet_len = 0, add; - - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); /* Lplt */ - Zplt = cio_read(cio, 1); /* Zplt */ - for (i = len - 3; i > 0; i--) { - add = cio_read(cio, 1); - packet_len = (packet_len << 7) + add; /* Iplt_i */ - if ((add & 0x80) == 0) { - /* New packet */ - packet_len = 0; - } - } -} - -static void j3d_read_ppm(opj_j3d_t *j3d) { - int len, Z_ppm, i, j; - int N_ppm; - - opj_cp_t *cp = j3d->cp; - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); - cp->ppm = 1; - - Z_ppm = cio_read(cio, 1); /* Z_ppm */ - len -= 3; - while (len > 0) { - if (cp->ppm_previous == 0) { - N_ppm = cio_read(cio, 4); /* N_ppm */ - len -= 4; - } else { - N_ppm = cp->ppm_previous; - } - j = cp->ppm_store; - if (Z_ppm == 0) { /* First PPM marker */ - cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char)); - cp->ppm_data_first = cp->ppm_data; - cp->ppm_len = N_ppm; - } else { /* NON-first PPM marker */ - cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char)); - cp->ppm_data_first = cp->ppm_data; - cp->ppm_len = N_ppm + cp->ppm_store; - } - for (i = N_ppm; i > 0; i--) { /* Read packet header */ - cp->ppm_data[j] = cio_read(cio, 1); - j++; - len--; - if (len == 0) - break; /* Case of non-finished packet header in present marker but finished in next one */ - } - cp->ppm_previous = i - 1; - cp->ppm_store = j; - } -} - -static void j3d_read_ppt(opj_j3d_t *j3d) { - int len, Z_ppt, i, j = 0; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = cp->tcps + j3d->curtileno; - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); - Z_ppt = cio_read(cio, 1); - tcp->ppt = 1; - if (Z_ppt == 0) { /* First PPT marker */ - tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char)); - tcp->ppt_data_first = tcp->ppt_data; - tcp->ppt_store = 0; - tcp->ppt_len = len - 3; - } else { /* NON-first PPT marker */ - tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char)); - tcp->ppt_data_first = tcp->ppt_data; - tcp->ppt_len = len - 3 + tcp->ppt_store; - } - j = tcp->ppt_store; - for (i = len - 3; i > 0; i--) { - tcp->ppt_data[j] = cio_read(cio, 1); - j++; - } - tcp->ppt_store = j; -} - -static void j3d_write_sot(opj_j3d_t *j3d) { - int lenp, len; - - opj_cio_t *cio = j3d->cio; - - j3d->sot_start = cio_tell(cio); - cio_write(cio, J3D_MS_SOT, 2); /* SOT */ - lenp = cio_tell(cio); - cio_skip(cio, 2); /* Lsot (further) */ - cio_write(cio, j3d->curtileno, 2); /* Isot */ - cio_skip(cio, 4); /* Psot (further in j3d_write_sod) */ - cio_write(cio, 0, 1); /* TPsot */ - cio_write(cio, 1, 1); /* TNsot (no of tile-parts of this tile in this codestream)*/ - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lsot */ - cio_seek(cio, lenp + len); -} - -static void j3d_read_sot(opj_j3d_t *j3d) { - int len, tileno, totlen, partno, numparts, i; - opj_tcp_t *tcp = NULL; - char status = 0; - - opj_cp_t *cp = j3d->cp; - opj_cio_t *cio = j3d->cio; - - len = cio_read(cio, 2); - tileno = cio_read(cio, 2); - - if (cp->tileno_size == 0) { - cp->tileno[cp->tileno_size] = tileno; - cp->tileno_size++; - } else { - i = 0; - while (i < cp->tileno_size && status == 0) { - status = cp->tileno[i] == tileno ? 1 : 0; - i++; - } - if (status == 0) { - cp->tileno[cp->tileno_size] = tileno; - cp->tileno_size++; - } - } - - totlen = cio_read(cio, 4); - if (!totlen) - totlen = cio_numbytesleft(cio) + 8; - - partno = cio_read(cio, 1); - numparts = cio_read(cio, 1); - - j3d->curtileno = tileno; - j3d->eot = cio_getbp(cio) - 12 + totlen; - j3d->state = J3D_STATE_TPH; - tcp = &cp->tcps[j3d->curtileno]; - - if (tcp->first == 1) { - - /* Initialization PPT */ - opj_tccp_t *tmp = tcp->tccps; - memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t)); - tcp->ppt = 0; - tcp->ppt_data = NULL; - tcp->ppt_data_first = NULL; - tcp->tccps = tmp; - - for (i = 0; i < j3d->volume->numcomps; i++) { - tcp->tccps[i] = j3d->default_tcp->tccps[i]; - } - cp->tcps[j3d->curtileno].first = 0; - } -} - -static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) { - int l, layno; - int totlen; - opj_tcp_t *tcp = NULL; - opj_volume_info_t *volume_info = NULL; - - opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */ - opj_cp_t *cp = j3d->cp; - opj_cio_t *cio = j3d->cio; - - cio_write(cio, J3D_MS_SOD, 2); - if (j3d->curtileno == 0) { - j3d->sod_start = cio_tell(cio) + j3d->pos_correction; - } - - /* INDEX >> */ - volume_info = j3d->volume_info; - if (volume_info && volume_info->index_on) { - volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1; - } - /* << INDEX */ - - tcp = &cp->tcps[j3d->curtileno]; - for (layno = 0; layno < tcp->numlayers; layno++) { - tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0; - } - - if(volume_info) { - volume_info->num = 0; - } - - l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info); - - /* Writing Psot in SOT marker */ - totlen = cio_tell(cio) + l - j3d->sot_start; - cio_seek(cio, j3d->sot_start + 6); - cio_write(cio, totlen, 4); - cio_seek(cio, j3d->sot_start + totlen); -} - -static void j3d_read_sod(opj_j3d_t *j3d) { - int len, truncate = 0, i; - unsigned char *data = NULL, *data_ptr = NULL; - - opj_cio_t *cio = j3d->cio; - int curtileno = j3d->curtileno; - - len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1); - - if (len == cio_numbytesleft(cio) + 1) { - truncate = 1; /* Case of a truncate codestream */ - } - - data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char)); - - for (i = 0; i < j3d->tile_len[curtileno]; i++) { - data[i] = j3d->tile_data[curtileno][i]; - } - - data_ptr = data + j3d->tile_len[curtileno]; - for (i = 0; i < len; i++) { - data_ptr[i] = cio_read(cio, 1); - } - - j3d->tile_len[curtileno] += len; - opj_free(j3d->tile_data[curtileno]); - j3d->tile_data[curtileno] = data; - - if (!truncate) { - j3d->state = J3D_STATE_TPHSOT; - } else { - j3d->state = J3D_STATE_NEOC; /* RAJOUTE !! */ - } -} - -static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) { - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = &cp->tcps[tileno]; - opj_cio_t *cio = j3d->cio; - int numcomps = j3d->volume->numcomps; - - cio_write(cio, J3D_MS_RGN, 2); /* RGN */ - cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */ - cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */ - cio_write(cio, 0, 1); /* Srgn */ - cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */ -} - -static void j3d_read_rgn(opj_j3d_t *j3d) { - int len, compno, roisty; - - opj_cp_t *cp = j3d->cp; - opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp; - opj_cio_t *cio = j3d->cio; - int numcomps = j3d->volume->numcomps; - - len = cio_read(cio, 2); /* Lrgn */ - compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */ - roisty = cio_read(cio, 1); /* Srgn */ - tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */ -} - -static void j3d_write_eoc(opj_j3d_t *j3d) { - opj_cio_t *cio = j3d->cio; - /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */ - cio_write(cio, J3D_MS_EOC, 2); -} - -static void j3d_read_eoc(opj_j3d_t *j3d) { - int i, tileno; - -#ifndef NO_PACKETS_DECODING - opj_tcd_t *tcd = tcd_create(j3d->cinfo); - tcd_malloc_decode(tcd, j3d->volume, j3d->cp); - /*j3d_dump_volume(stdout, tcd->volume); - j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/ - for (i = 0; i < j3d->cp->tileno_size; i++) { - tileno = j3d->cp->tileno[i]; - //opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n"); - tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno); - opj_free(j3d->tile_data[tileno]); - j3d->tile_data[tileno] = NULL; - } - tcd_free_decode(tcd); - tcd_destroy(tcd); -#else - for (i = 0; i < j3d->cp->tileno_size; i++) { - tileno = j3d->cp->tileno[i]; - opj_free(j3d->tile_data[tileno]); - j3d->tile_data[tileno] = NULL; - } -#endif - - j3d->state = J3D_STATE_MT; -} - -static void j3d_read_unk(opj_j3d_t *j3d) { - opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n"); -} - -static opj_atk_t atk_info_wt[] = { - {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/ - {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/ - {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/ - {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/ - {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/ - {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/ - {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736}, - {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/ - {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}}, /* WT 5-3 IRR*/ - {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}} /* WT 13-7 REV*/ -}; - -typedef struct opj_dec_mstabent { - /** marker value */ - int id; - /** value of the state when the marker can appear */ - int states; - /** action linked to the marker */ - void (*handler) (opj_j3d_t *j3d); -} opj_dec_mstabent_t; - -opj_dec_mstabent_t j3d_dec_mstab[] = { - {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc}, - {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot}, - {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod}, - {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc}, - {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap}, - {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz}, - {J3D_MS_ZSI, J3D_STATE_MHSIZ, j3d_read_zsi}, - {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod}, - {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc}, - {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn}, - {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd}, - {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc}, - {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc}, - {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm}, - {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm}, - {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt}, - {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm}, - {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt}, - {J3D_MS_SOP, 0, 0}, - {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg}, - {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com}, - {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco}, - {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk}, - {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk} - /*, -->must define the j3d_read functions - {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd}, - {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct}, - {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc}, - {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco}, - {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt}, - {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms}, - {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs}, - {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads}, - {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd}, - {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/ -}; - -/** -Read the lookup table containing all the marker, status and action -@param id Marker value -*/ -static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) { - opj_dec_mstabent_t *e; - for (e = j3d_dec_mstab; e->id != 0; e++) { - if (e->id == id) { - break; - } - } - return e; -} - -/* ----------------------------------------------------------------------- */ -/* J3D / JPT decoder interface */ -/* ----------------------------------------------------------------------- */ - -opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) { - opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t)); - if(j3d) { - j3d->cinfo = cinfo; - j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t)); - if(!j3d->default_tcp) { - opj_free(j3d); - return NULL; - } - } - return j3d; -} - -void j3d_destroy_decompress(opj_j3d_t *j3d) { - int i = 0; - - if(j3d->tile_len != NULL) { - opj_free(j3d->tile_len); - } - if(j3d->tile_data != NULL) { - opj_free(j3d->tile_data); - } - if(j3d->default_tcp != NULL) { - opj_tcp_t *default_tcp = j3d->default_tcp; - if(default_tcp->ppt_data_first != NULL) { - opj_free(default_tcp->ppt_data_first); - } - if(j3d->default_tcp->tccps != NULL) { - opj_free(j3d->default_tcp->tccps); - } - opj_free(j3d->default_tcp); - } - if(j3d->cp != NULL) { - opj_cp_t *cp = j3d->cp; - if(cp->tcps != NULL) { - for(i = 0; i < cp->tw * cp->th * cp->tl; i++) { - if(cp->tcps[i].ppt_data_first != NULL) { - opj_free(cp->tcps[i].ppt_data_first); - } - if(cp->tcps[i].tccps != NULL) { - opj_free(cp->tcps[i].tccps); - } - } - opj_free(cp->tcps); - } - if(cp->ppm_data_first != NULL) { - opj_free(cp->ppm_data_first); - } - if(cp->tileno != NULL) { - opj_free(cp->tileno); - } - if(cp->comment != NULL) { - opj_free(cp->comment); - } - - opj_free(cp); - } - - opj_free(j3d); -} - -void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) { - if(j3d && parameters) { - /* create and initialize the coding parameters structure */ - opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t)); - cp->reduce[0] = parameters->cp_reduce[0]; - cp->reduce[1] = parameters->cp_reduce[1]; - cp->reduce[2] = parameters->cp_reduce[2]; - cp->layer = parameters->cp_layer; - cp->bigendian = parameters->bigendian; - - - cp->encoding_format = ENCOD_2EB; - cp->transform_format = TRF_2D_DWT; - - /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */ - j3d->cp = cp; - } -} - -opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) { - opj_volume_t *volume = NULL; - - opj_common_ptr cinfo = j3d->cinfo; - - j3d->cio = cio; - - /* create an empty volume */ - volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t)); - j3d->volume = volume; - - j3d->state = J3D_STATE_MHSOC; - - for (;;) { - opj_dec_mstabent_t *e; - int id = cio_read(cio, 2); - if (id >> 8 != 0xff) { - opj_volume_destroy(volume); - opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id); - return 0; - } - e = j3d_dec_mstab_lookup(id); - //opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states); - if (!(j3d->state & e->states)) { - opj_volume_destroy(volume); - opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id); - return 0; - } - if (e->handler) { - (*e->handler)(j3d); - } - //opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state); - if (j3d->state == J3D_STATE_MT) { - break; - } - if (j3d->state == J3D_STATE_NEOC) { - break; - } - } - if (j3d->state == J3D_STATE_NEOC) { - j3d_read_eoc(j3d); - } - - if (j3d->state != J3D_STATE_MT) { - opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n"); - } - - return volume; -} - -/* ----------------------------------------------------------------------- */ -/* J3D encoder interface */ -/* ----------------------------------------------------------------------- */ - -opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) { - opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t)); - if(j3d) { - j3d->cinfo = cinfo; - } - return j3d; -} - -void j3d_destroy_compress(opj_j3d_t *j3d) { - int tileno; - - if(!j3d) return; - - if(j3d->volume_info != NULL) { - opj_volume_info_t *volume_info = j3d->volume_info; - if (volume_info->index_on && j3d->cp) { - opj_cp_t *cp = j3d->cp; - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - opj_tile_info_t *tile_info = &volume_info->tile[tileno]; - opj_free(tile_info->thresh); - opj_free(tile_info->packet); - } - opj_free(volume_info->tile); - } - opj_free(volume_info); - } - if(j3d->cp != NULL) { - opj_cp_t *cp = j3d->cp; - - if(cp->comment) { - opj_free(cp->comment); - } - if(cp->matrice) { - opj_free(cp->matrice); - } - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - opj_free(cp->tcps[tileno].tccps); - } - opj_free(cp->tcps); - opj_free(cp); - } - - opj_free(j3d); -} - -void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) { - int i, j, tileno, numpocs_tile; - opj_cp_t *cp = NULL; - - if(!j3d || !parameters || ! volume) { - return; - } - - /* create and initialize the coding parameters structure */ - cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t)); - - /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */ - j3d->cp = cp; - - /* set default values for cp */ - cp->tw = 1; - cp->th = 1; - cp->tl = 1; - - /* copy user encoding parameters */ - cp->disto_alloc = parameters->cp_disto_alloc; - cp->fixed_alloc = parameters->cp_fixed_alloc; - cp->fixed_quality = parameters->cp_fixed_quality; - - /* transform and coding method */ - cp->transform_format = parameters->transform_format; - cp->encoding_format = parameters->encoding_format; - - /* mod fixed_quality */ - if(parameters->cp_matrice) { - size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int); - cp->matrice = (int *) opj_malloc(array_size); - memcpy(cp->matrice, parameters->cp_matrice, array_size); - } - - /* creation of an index file ? */ - cp->index_on = parameters->index_on; - if(cp->index_on) { - j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t)); - } - - /* tiles */ - cp->tdx = parameters->cp_tdx; - cp->tdy = parameters->cp_tdy; - cp->tdz = parameters->cp_tdz; - /* tile offset */ - cp->tx0 = parameters->cp_tx0; - cp->ty0 = parameters->cp_ty0; - cp->tz0 = parameters->cp_tz0; - /* comment string */ - if(parameters->cp_comment) { - cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1); - if(cp->comment) { - strcpy(cp->comment, parameters->cp_comment); - } - } - - /*calculate other encoding parameters*/ - if (parameters->tile_size_on) { - cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx); - cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy); - cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz); - } else { - cp->tdx = volume->x1 - cp->tx0; - cp->tdy = volume->y1 - cp->ty0; - cp->tdz = volume->z1 - cp->tz0; - } - - /* initialize the multiple tiles */ - /* ---------------------------- */ - cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t)); - - for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) { - opj_tcp_t *tcp = &cp->tcps[tileno]; - tcp->numlayers = parameters->tcp_numlayers; - for (j = 0; j < tcp->numlayers; j++) { - if (cp->fixed_quality) { /* add fixed_quality */ - tcp->distoratio[j] = parameters->tcp_distoratio[j]; - } else { - tcp->rates[j] = parameters->tcp_rates[j]; - } - } - tcp->csty = parameters->csty; - tcp->prg = parameters->prog_order; - tcp->mct = volume->numcomps == 3 ? 1 : 0; - - numpocs_tile = 0; - tcp->POC = 0; - if (parameters->numpocs) { - /* initialisation of POC */ - tcp->POC = 1; - for (i = 0; i < parameters->numpocs; i++) { - if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) { - opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; - tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; - tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0; - tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1; - tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1; - tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1; - tcp_poc->prg = parameters->POC[numpocs_tile].prg; - tcp_poc->tile = parameters->POC[numpocs_tile].tile; - numpocs_tile++; - } - } - } - tcp->numpocs = numpocs_tile; - - tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t)); - - for (i = 0; i < volume->numcomps; i++) { - opj_tccp_t *tccp = &tcp->tccps[i]; - tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT; /* 0 => standard precint || 1 => custom-defined precinct */ - tccp->numresolution[0] = parameters->numresolution[0]; - tccp->numresolution[1] = parameters->numresolution[1]; - tccp->numresolution[2] = parameters->numresolution[2]; - assert (parameters->cblock_init[0] <= T1_MAXCBLKW); - assert (parameters->cblock_init[0] >= T1_MINCBLKW); - assert (parameters->cblock_init[1] <= T1_MAXCBLKH); - assert (parameters->cblock_init[1] >= T1_MINCBLKH); - assert (parameters->cblock_init[2] <= T1_MAXCBLKD); - assert (parameters->cblock_init[2] >= T1_MINCBLKD); - tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]); - tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]); - tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]); - assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD); - tccp->cblksty = parameters->mode; //Codeblock style --> Table A.19 (default 0) - - /*ATK / transform */ - tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */ - for (j = 0; j < 3; j++) { - tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */ - } - - /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/ - tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT; - tccp->numgbits = 2; - if (i == parameters->roi_compno) { - tccp->roishift = parameters->roi_shift; - } else { - tccp->roishift = 0; - } - /* Custom defined precints */ - if (parameters->csty & J3D_CCP_CSTY_PRT) { - int k; - for (k = 0; k < 3; k++) { - int p = 0; - for (j = tccp->numresolution[k] - 1; j >= 0; j--) { - if (p < parameters->res_spec) {/* p < number of precinct size specifications */ - if (parameters->prct_init[k][p] < 1) { - tccp->prctsiz[k][j] = 1; - } else { - tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]); - } - } else { - int res_spec = parameters->res_spec; - int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1)); - if (size_prct < 1) { - tccp->prctsiz[k][j] = 1; - } else { - tccp->prctsiz[k][j] = int_floorlog2(size_prct); - } - } - } - p++; - } - } else { - int k; - for (k = 0; k < 3; k++) { - for (j = 0; j < tccp->numresolution[k]; j++) { - tccp->prctsiz[k][j] = 15; - } - } - } - //Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0) - dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec); - } - } -} - -/** -Create an index file -@param j3d -@param cio -@param volume_info -@param index Index filename -@return Returns 1 if successful, returns 0 otherwise -*/ -static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) { - - int tileno, compno, layno, resno, precno, pack_nb, x, y, z; - FILE *stream = NULL; - double total_disto = 0; - - volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */ - - stream = fopen(index, "w"); - if (!stream) { - opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index); - return 0; - } - - fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l); - fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format); - fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format); - fprintf(stream, "PROG\t%d\n", volume_info->prog); - fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z); - fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl); - fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp); - fprintf(stream, "LAYER\t%d\n", volume_info->layer); - fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]); - - fprintf(stream, "Precint sizes for each resolution:\n"); - for (resno = volume_info->decomposition[0]; resno >= 0; resno--) { - fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno, - (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno])); /* based on tile 0 */ - } - fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end); - fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size); - fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n"); - for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) { - fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n", - volume_info->tile[tileno].num_tile, - volume_info->tile[tileno].start_pos, - volume_info->tile[tileno].end_header, - volume_info->tile[tileno].end_pos, - volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix, - volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix); - } - - for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) { - int start_pos, end_pos; - double disto = 0; - pack_nb = 0; - if (volume_info->prog == LRCP) { /* LRCP */ - fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n"); - for (layno = 0; layno < volume_info->layer; layno++) { - for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) { - for (compno = 0; compno < volume_info->comp; compno++) { - int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno]; - for (precno = 0; precno < prec_max; precno++) { - start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos; - end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos; - disto = volume_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto); - total_disto += disto; - pack_nb++; - } - } - } - } - } /* LRCP */ - else if (volume_info->prog == RLCP) { /* RLCP */ - /* - fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto"); - */ - for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) { - for (layno = 0; layno < volume_info->layer; layno++) { - for (compno = 0; compno < volume_info->comp; compno++) { - int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno]; - for (precno = 0; precno < prec_max; precno++) { - start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos; - end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos; - disto = volume_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n", - pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto); - total_disto += disto; - pack_nb++; - } - } - } - } - } /* RLCP */ - else if (volume_info->prog == RPCL) { /* RPCL */ - /* - fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n"); - */ - for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) { - /* I suppose components have same XRsiz, YRsiz */ - //int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x; - //int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y; - int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x; - int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y; - int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z; - int x1 = x0 + volume_info->tile_x; - int y1 = y0 + volume_info->tile_y; - int z1 = z0 + volume_info->tile_z; - for(z = z0; z < z1; z++) { - for(y = y0; y < y1; y++) { - for(x = x0; x < x1; x++) { - for (compno = 0; compno < volume_info->comp; compno++) { - int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno]; - for (precno = 0; precno < prec_max; precno++) { - int pcnx = volume_info->tile[tileno].prctno[0][resno]; - int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno ); - int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno ); - int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno ); - int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; - int precno_y = (int) floor( (float)precno/(float)pcnx ); - if (precno_y*pcy == y ) { - if (precno_x*pcx == x ) { - for (layno = 0; layno < volume_info->layer; layno++) { - start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos; - end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos; - disto = volume_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n", - pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); - total_disto += disto; - pack_nb++; - } - } - } - } /* precno */ - } /* compno */ - } /* x = x0..x1 */ - } /* y = y0..y1 */ - } /* z = z0..z1 */ - } /* resno */ - } /* RPCL */ - else if (volume_info->prog == PCRL) { /* PCRL */ - /* I suppose components have same XRsiz, YRsiz */ - int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x; - int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y; - int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z; - int x1 = x0 + volume_info->tile_x; - int y1 = y0 + volume_info->tile_y; - int z1 = z0 + volume_info->tile_z; - /* - fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n"); - */ - for(z = z0; z < z1; z++) { - for(y = y0; y < y1; y++) { - for(x = x0; x < x1; x++) { - for (compno = 0; compno < volume_info->comp; compno++) { - for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) { - int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]; - for (precno = 0; precno < prec_max; precno++) { - int pcnx = volume_info->tile[tileno].prctno[0][resno]; - int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno ); - int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno ); - int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno ); - int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; - int precno_y = (int) floor( (float)precno/(float)pcnx ); - int precno_z = (int) floor( (float)precno/(float)pcnx ); - if (precno_z*pcz == z ) { - if (precno_y*pcy == y ) { - if (precno_x*pcx == x ) { - for (layno = 0; layno < volume_info->layer; layno++) { - start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos; - end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos; - disto = volume_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n", - pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); - total_disto += disto; - pack_nb++; - } - } - } - } - } /* precno */ - } /* resno */ - } /* compno */ - } /* x = x0..x1 */ - } /* y = y0..y1 */ - } - } /* PCRL */ - else { /* CPRL */ - /* - fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n"); - */ - for (compno = 0; compno < volume_info->comp; compno++) { - /* I suppose components have same XRsiz, YRsiz */ - int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x; - int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y; - int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z; - int x1 = x0 + volume_info->tile_x; - int y1 = y0 + volume_info->tile_y; - int z1 = z0 + volume_info->tile_z; - for(z = z0; z < z1; z++) { - for(y = y0; y < y1; y++) { - for(x = x0; x < x1; x++) { - for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) { - int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno]; - for (precno = 0; precno < prec_max; precno++) { - int pcnx = volume_info->tile[tileno].prctno[0][resno]; - int pcny = volume_info->tile[tileno].prctno[1][resno]; - int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno ); - int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno ); - int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno ); - int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx; - int precno_y = (int) floor( (float)precno/(float)pcnx ); - int precno_z = 0; /*???*/ - if (precno_z*pcz == z ) { - if (precno_y*pcy == y ) { - if (precno_x*pcx == x ) { - for (layno = 0; layno < volume_info->layer; layno++) { - start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos; - end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos; - disto = volume_info->tile[tileno].packet[pack_nb].disto; - fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n", - pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); - total_disto += disto; - pack_nb++; - } - } - } - } - } /* precno */ - } /* resno */ - } /* x = x0..x1 */ - } /* y = y0..y1 */ - } /* z = z0..z1 */ - } /* comno */ - } /* CPRL */ - } /* tileno */ - - fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max); /* SE max */ - fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto); /* SE totale */ - - - fclose(stream); - - return 1; -} - -bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) { - int tileno, compno; - opj_volume_info_t *volume_info = NULL; - opj_cp_t *cp = NULL; - opj_tcd_t *tcd = NULL; /* TCD component */ - - j3d->cio = cio; - j3d->volume = volume; - cp = j3d->cp; - - /*j3d_dump_volume(stdout, volume); - j3d_dump_cp(stdout, volume, cp);*/ - - /* INDEX >> */ - volume_info = j3d->volume_info; - if (volume_info && cp->index_on) { - volume_info->index_on = cp->index_on; - volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t)); - volume_info->volume_w = volume->x1 - volume->x0; - volume_info->volume_h = volume->y1 - volume->y0; - volume_info->volume_l = volume->z1 - volume->z0; - volume_info->prog = (&cp->tcps[0])->prg; - volume_info->tw = cp->tw; - volume_info->th = cp->th; - volume_info->tl = cp->tl; - volume_info->tile_x = cp->tdx; /* new version parser */ - volume_info->tile_y = cp->tdy; /* new version parser */ - volume_info->tile_z = cp->tdz; /* new version parser */ - volume_info->tile_Ox = cp->tx0; /* new version parser */ - volume_info->tile_Oy = cp->ty0; /* new version parser */ - volume_info->tile_Oz = cp->tz0; /* new version parser */ - volume_info->transform_format = cp->transform_format; - volume_info->encoding_format = cp->encoding_format; - volume_info->comp = volume->numcomps; - volume_info->layer = (&cp->tcps[0])->numlayers; - volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1; - volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1; - volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1; - volume_info->D_max = 0; /* ADD Marcela */ - } - /* << INDEX */ - - j3d_write_soc(j3d); - j3d_write_siz(j3d); - if (j3d->cinfo->codec_format == CODEC_J3D) { - j3d_write_cap(j3d); - j3d_write_zsi(j3d); - } - j3d_write_cod(j3d); - j3d_write_qcd(j3d); - for (compno = 0; compno < volume->numcomps; compno++) { - opj_tcp_t *tcp = &cp->tcps[0]; - if (tcp->tccps[compno].roishift) - j3d_write_rgn(j3d, compno, 0); - } - /*Optional 15444-2 markers*/ - if (j3d->cp->tcps->tccps[0].atk != NULL) - j3d_write_atk(j3d); - if (j3d->volume->comps[0].dcoffset != 0) - j3d_write_dco(j3d); - - if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB) - j3d_write_com(j3d); - - /* INDEX >> */ - if(volume_info && volume_info->index_on) { - volume_info->main_head_end = cio_tell(cio) - 1; - } - /* << INDEX */ - - /* create the tile encoder */ - tcd = tcd_create(j3d->cinfo); - - /* encode each tile */ - for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) { - opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl); - - j3d->curtileno = tileno; - - /* initialisation before tile encoding */ - if (tileno == 0) { - tcd_malloc_encode(tcd, volume, cp, j3d->curtileno); - } else { - tcd_init_encode(tcd, volume, cp, j3d->curtileno); - } - - /* INDEX >> */ - if(volume_info && volume_info->index_on) { - volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno; - volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction; - } - /* << INDEX */ - - j3d_write_sot(j3d); - - for (compno = 1; compno < volume->numcomps; compno++) { - j3d_write_coc(j3d, compno); - j3d_write_qcc(j3d, compno); - } - - if (cp->tcps[tileno].numpocs) { - j3d_write_poc(j3d); - } - j3d_write_sod(j3d, tcd); //--> tcd_encode_tile - - /* INDEX >> */ - if(volume_info && volume_info->index_on) { - volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1; - } - /* << INDEX */ - } - - /* destroy the tile encoder */ - tcd_free_encode(tcd); - tcd_destroy(tcd); - - j3d_write_eoc(j3d); - - /* Creation of the index file */ - if(volume_info && volume_info->index_on) { - if(!j3d_create_index(j3d, cio, volume_info, index)) { - opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index); - return false; - } - } - - return true; -} - diff --git a/jp3d/libjp3dvm/jp3d.h b/jp3d/libjp3dvm/jp3d.h deleted file mode 100755 index d9dcc8e3..00000000 --- a/jp3d/libjp3dvm/jp3d.h +++ /dev/null @@ -1,518 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __J3D_H -#define __J3D_H -/** -@file j3d.h -@brief The JPEG-2000 Codestream Reader/Writer (J3D) - -The functions in J3D.C have for goal to read/write the several parts of the codestream: markers and data. -*/ - -/** @defgroup J3D J3D - JPEG-2000 codestream reader/writer */ -/*@{*/ - -#define J3D_CP_CSTY_PRT 0x01 -#define J3D_CP_CSTY_SOP 0x02 -#define J3D_CP_CSTY_EPH 0x04 -#define J3D_CCP_CSTY_PRT 0x01 -/** Table A-8 */ -#define J3D_CCP_CBLKSTY_LAZY 0x01 /* Selective arithmetic coding bypass */ -#define J3D_CCP_CBLKSTY_RESET 0x02 /* Reset context probabilities on coding pass boundaries */ -#define J3D_CCP_CBLKSTY_TERMALL 0x04 /* Termination on each coding pass */ -#define J3D_CCP_CBLKSTY_VSC 0x08 /* Vertically causal context, add also hook for switching off and on 3D context models */ -#define J3D_CCP_CBLKSTY_PTERM 0x10 /* Predictable termination */ -#define J3D_CCP_CBLKSTY_SEGSYM 0x20 /* Segmentation symbols are used */ -#define J3D_CCP_CBLKSTY_3DCTXT 0x40 /* 3D context models (3D-EBCOT) vs 2D context models */ - -#define J3D_CCP_QNTSTY_NOQNT 0 /* Quantization style : no quantization */ -#define J3D_CCP_QNTSTY_SIQNT 1 /* Quantization style : scalar derived (values signalled only in LLL subband) */ -#define J3D_CCP_QNTSTY_SEQNT 2 /* Quantization style : scalar expounded (values signalled for each subband) */ - -/* ----------------------------------------------------------------------- */ - -#define J3D_MS_SOC 0xff4f /**< SOC marker value */ -#define J3D_MS_SOT 0xff90 /**< SOT marker value */ -#define J3D_MS_SOD 0xff93 /**< SOD marker value */ -#define J3D_MS_EOC 0xffd9 /**< EOC marker value */ -#define J3D_MS_CAP 0xff50 /**< CAP marker value */ -#define J3D_MS_SIZ 0xff51 /**< SIZ marker value */ -#define J3D_MS_ZSI 0xff54 /**< ZSI marker value */ -#define J3D_MS_COD 0xff52 /**< COD marker value */ -#define J3D_MS_COC 0xff53 /**< COC marker value */ -#define J3D_MS_RGN 0xff5e /**< RGN marker value */ -#define J3D_MS_QCD 0xff5c /**< QCD marker value */ -#define J3D_MS_QCC 0xff5d /**< QCC marker value */ -#define J3D_MS_POC 0xff5f /**< POC marker value */ -#define J3D_MS_TLM 0xff55 /**< TLM marker value */ -#define J3D_MS_PLM 0xff57 /**< PLM marker value */ -#define J3D_MS_PLT 0xff58 /**< PLT marker value */ -#define J3D_MS_PPM 0xff60 /**< PPM marker value */ -#define J3D_MS_PPT 0xff61 /**< PPT marker value */ -#define J3D_MS_SOP 0xff91 /**< SOP marker value */ -#define J3D_MS_EPH 0xff92 /**< EPH marker value */ -#define J3D_MS_CRG 0xff63 /**< CRG marker value */ -#define J3D_MS_COM 0xff64 /**< COM marker value */ -//15444-2 -#define J3D_MS_DCO 0xff70 /**< DCO marker value */ -#define J3D_MS_VMS 0xff71 /**< VMS marker value */ -#define J3D_MS_DFS 0xff72 /**< DFS marker value */ -#define J3D_MS_ADS 0xff73 /**< ADS marker value */ -#define J3D_MS_ATK 0xff79 /**< ATK marker value */ -#define J3D_MS_CBD 0xff78 /**< CBD marker value */ -#define J3D_MS_MCT 0xff74 /**< MCT marker value */ -#define J3D_MS_MCC 0xff75 /**< MCC marker value */ -#define J3D_MS_MCO 0xff77 /**< MCO marker value */ -#define J3D_MS_NLT 0xff76 /**< NLT marker value */ -#define J3D_MS_QPD 0xff5a /**< QPD marker value */ -#define J3D_MS_QPC 0xff5b /**< QPC marker value */ - -/* ----------------------------------------------------------------------- */ -/* Capability RSIZ parameter, extended */ -#define J3D_RSIZ_BASIC 0x0000 - -#define J3D_RSIZ_DCO 0x8001 /* Required */ -#define J3D_RSIZ_VSQNT 0x8002 -#define J3D_RSIZ_TCQNT 0x8004 -#define J3D_RSIZ_VMASK 0x8008 -#define J3D_RSIZ_SSOVL 0x8010 -#define J3D_RSIZ_ADECS 0x8020 -#define J3D_RSIZ_ATK 0x8040 /*Required*/ -#define J3D_RSIZ_SSYMK 0x8080 -#define J3D_RSIZ_MCT 0x8100 /*Not compatible with DCO*/ -#define J3D_RSIZ_NLT 0x8200 /*Required*/ -#define J3D_RSIZ_ASHAP 0x8400 -#define J3D_RSIZ_PRQNT 0x8800 - -#define J3D_CAP_10 0x00400000 -/* Arbitrary transformation kernel, 15444-2 */ -#define J3D_ATK_IRR 0 -#define J3D_ATK_REV 1 -#define J3D_ATK_ARB 0 -#define J3D_ATK_WS 1 -#define J3D_ATK_CON 0 -/* ----------------------------------------------------------------------- */ - -/** -Values that specify the status of the decoding process when decoding the main header. -These values may be combined with a | operator. -*/ -typedef enum J3D_STATUS { - /**< a SOC marker is expected */ - J3D_STATE_MHSOC = 0x0001, - /**< a SIZ marker is expected */ - J3D_STATE_MHSIZ = 0x0002, - /**< the decoding process is in the main header */ - J3D_STATE_MH = 0x0004, - /**< the decoding process is in a tile part header and expects a SOT marker */ - J3D_STATE_TPHSOT = 0x0008, - /**< the decoding process is in a tile part header */ - J3D_STATE_TPH = 0x0010, - /**< the EOC marker has just been read */ - J3D_STATE_MT = 0x0020, - /**< the decoding process must not expect a EOC marker because the codestream is truncated */ - J3D_STATE_NEOC = 0x0040 -} J3D_STATUS; - - - -/** -Arbitrary transformation kernel -*/ -typedef struct opj_atk { -/** index of wavelet kernel */ - int index; -/** Numerical type of scaling factor and lifting step parameters */ - int coeff_typ; -/** Wavelet filter category */ - int filt_cat; -/** Wavelet transformation type (REV/IRR) */ - int wt_typ; -/** Initial odd/even subsequence */ - int minit; -/** Boundary extension method (constant CON / whole-sample symmetric WS) */ - int exten; -/** Scaling factor. Only for wt_typ=IRR */ - double Katk; -/** Number of lifting steps */ - int Natk; -/** Offset for lifting step s. Only for filt_cat=ARB */ - int Oatk[256]; -/** Base 2 scaling exponent for lifting step s. Only for wt_typ=REV */ - int Eatk[256]; -/** Additive residue for lifting step s. Only for wt_typ=REV */ - int Batk[256]; -/** Number of lifting coefficients signaled for lifting step s */ - int LCatk[256]; -/** Lifting coefficient k for lifting step s */ - double Aatk[256][256]; -} opj_atk_t; - - -/** -Quantization stepsize -*/ -typedef struct opj_stepsize { -/** exponent */ - int expn; -/** mantissa */ - int mant; -} opj_stepsize_t; - -/** -Tile-component coding parameters -*/ -typedef struct opj_tccp { - /** coding style */ - int csty; - /** number of resolutions of x, y and z-axis */ - int numresolution[3]; - /** code-blocks width height & depth*/ - int cblk[3]; - /** code-block coding style */ - int cblksty; - /** 0: no ATK (only 9-7 or 5-3) 1: ATK defined WT*/ - int atk_wt[3]; - /** Arbitrary transformation kernel (15444-2)*/ - opj_atk_t *atk; - /** DWT identifier for x, y and z-axis (0:WT9-7 1:WT5-3 >1:WT-atk->index) */ - int dwtid[3]; - /** reversible/irreversible wavelet transfomation (0:irrev 1:reversible)*/ - int reversible; - /** quantisation style */ - int qntsty; - /** stepsizes used for quantization */ - opj_stepsize_t stepsizes[J3D_MAXBANDS]; - /** number of guard bits. Table A28 de 15444-1*/ - int numgbits; - /** Region Of Interest shift */ - int roishift; - /** precinct width heigth & depth*/ - int prctsiz[3][J3D_MAXRLVLS]; -} opj_tccp_t; - -/** -Tile coding parameters : coding/decoding parameters common to all tiles -(information like COD, COC in main header) -*/ -typedef struct opj_tcp { -/** 1 : first part-tile of a tile */ - int first; - /** coding style */ - int csty; - /** progression order */ - OPJ_PROG_ORDER prg; - /** number of layers */ - int numlayers; - /** multi-component transform identifier */ - int mct; - /** rates of layers */ - float rates[100]; - /** number of progression order changes */ - int numpocs; - /** indicates if a POC marker has been used O:NO, 1:YES */ - int POC; - /** progression order changes */ - opj_poc_t pocs[J3D_MAXRLVLS - 1]; - /** add fixed_quality */ - float distoratio[100]; - /** tile-component coding parameters */ - opj_tccp_t *tccps; -/** packet header store there for futur use in t2_decode_packet */ - unsigned char *ppt_data; - /** pointer remaining on the first byte of the first header if ppt is used */ - unsigned char *ppt_data_first; - /** If ppt == 1 --> there was a PPT marker for the present tile */ - int ppt; - /** used in case of multiple marker PPT (number of info already stored) */ - int ppt_store; - int ppt_len; -} opj_tcp_t; - -/** -Coding parameters -*/ -typedef struct opj_cp { -/** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */ - OPJ_TRANSFORM transform_format; - /** entropy coding format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/ - OPJ_ENTROPY_CODING encoding_format; - /** allocation by rate/distortion */ - int disto_alloc; - /** allocation by fixed layer */ - int fixed_alloc; - /** add fixed_quality */ - int fixed_quality; - /** Rsiz: capabilities */ - int rsiz; - /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */ - int reduce[3]; - /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */ - int layer; - /** 0 = no index || 1 = index */ - int index_on; - /** Big-Endian/Little-endian order */ - int bigendian; - /** XTOsiz */ - int tx0; - /** YTOsiz */ - int ty0; - /** ZTOsiz */ - int tz0; - /** XTsiz */ - int tdx; - /** YTsiz */ - int tdy; - /** ZTsiz */ - int tdz; - /** comment for coding */ - char *comment; - /** number of tiles in width, heigth and depth */ - int tw; - int th; - int tl; - /** ID number of the tiles present in the codestream */ - int *tileno; - /** size of the vector tileno */ - int tileno_size; - /** tile coding parameters */ - opj_tcp_t *tcps; - /** fixed layer */ - int *matrice; - - /** packet header store there for futur use in t2_decode_packet */ - unsigned char *ppm_data; - /** pointer remaining on the first byte of the first header if ppm is used */ - unsigned char *ppm_data_first; - /** if ppm == 1 --> there was a PPM marker for the present tile */ - int ppm; - /** use in case of multiple marker PPM (number of info already store) */ - int ppm_store; - /** use in case of multiple marker PPM (case on non-finished previous info) */ - int ppm_previous; - int ppm_len; -} opj_cp_t; - -/** -Information concerning a packet inside tile -*/ -typedef struct opj_packet_info { - /** start position */ - int start_pos; - /** end position */ - int end_pos; - /** distorsion introduced */ - double disto; -} opj_packet_info_t; - -/** -Index structure : information regarding tiles inside volume -*/ -typedef struct opj_tile_info { - /** value of thresh for each layer by tile cfr. Marcela */ - double *thresh; - /** number of tile */ - int num_tile; - /** start position */ - int start_pos; - /** end position of the header */ - int end_header; - /** end position */ - int end_pos; - /** precinct number for each resolution level (width, heigth and depth) */ - int prctno[3][J3D_MAXRLVLS]; - /** precinct size (in power of 2), in X for each resolution level */ - int prctsiz[3][J3D_MAXRLVLS]; - /** information concerning packets inside tile */ - opj_packet_info_t *packet; - - /** add fixed_quality */ - int nbpix; - /** add fixed_quality */ - double distotile; -} opj_tile_info_t; - -/** -Index structure -*/ -typedef struct opj_volume_info { - - /** transform format 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */ - OPJ_TRANSFORM transform_format; - /** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI*/ - OPJ_ENTROPY_CODING encoding_format; /** 0 = no index || 1 = index */ - int index_on; - /** 0 = wt 9-7 || 1 = wt 5-3 || >=2 wt atk defined */ - int dwtid[3]; - /** maximum distortion reduction on the whole volume (add for Marcela) */ - double D_max; - /** packet number */ - int num; - /** writing the packet in the index with t2_encode_packets */ - int index_write; - /** volume width, height and depth */ - int volume_w; - int volume_h; - int volume_l; - /** progression order */ - OPJ_PROG_ORDER prog; - /** tile size in x, y and z */ - int tile_x; - int tile_y; - int tile_z; - /** tile origin in x, y and z */ - int tile_Ox; - int tile_Oy; - int tile_Oz; - /** number of tiles in X, Y and Z */ - int tw; - int th; - int tl; - /** component numbers */ - int comp; - /** number of layer */ - int layer; - /** number of decomposition in X, Y and Z*/ - int decomposition[3]; - /** DC offset (15444-2) */ - int dcoffset; - /** main header position */ - int main_head_end; - /** codestream's size */ - int codestream_size; - /** information regarding tiles inside volume */ - opj_tile_info_t *tile; -} opj_volume_info_t; - -/** -JPEG-2000 codestream reader/writer -*/ -typedef struct opj_j3d { - /** codec context */ - opj_common_ptr cinfo; - /** locate in which part of the codestream the decoder is (main header, tile header, end) */ - int state; - /** number of the tile curently concern by coding/decoding */ - int curtileno; - /** locate the position of the end of the tile in the codestream, used to detect a truncated codestream (in j3d_read_sod) */ - unsigned char *eot; - /** locate the start position of the SOT marker of the current coded tile: */ - int sot_start; - /* after encoding the tile, a jump (in j3d_write_sod) is done to the SOT marker to store the value of its length. */ - int sod_start; - /** as the J3D-file is written in several parts during encoding, it enables to make the right correction in position return by cio_tell */ - int pos_correction; - /** array used to store the data of each tile */ - unsigned char **tile_data; - /** array used to store the length of each tile */ - int *tile_len; - - /** decompression only : store decoding parameters common to all tiles */ - opj_tcp_t *default_tcp; - /** pointer to the encoded / decoded volume */ - opj_volume_t *volume; - /** pointer to the coding parameters */ - opj_cp_t *cp; - /** helper used to write the index file */ - opj_volume_info_t *volume_info; - /** pointer to the byte i/o stream */ - opj_cio_t *cio; -} opj_j3d_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Creates a J3D decompression structure -@param cinfo Codec context info -@return Returns a handle to a J3D decompressor if successful, returns NULL otherwise -*/ -opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo); -/** -Destroy a J3D decompressor handle -@param j3d J3D decompressor handle to destroy -*/ -void j3d_destroy_decompress(opj_j3d_t *j3d); -/** -Setup the decoder decoding parameters using user parameters. -Decoding parameters are returned in j3d->cp. -@param j3d J3D decompressor handle -@param parameters decompression parameters -*/ -void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters); -/** -Decode an volume from a JPEG-2000 codestream -@param j3d J3D decompressor handle -@param cio Input buffer stream -@return Returns a decoded volume if successful, returns NULL otherwise -*/ -opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio); -/** -Decode an volume form a JPT-stream (JPEG 2000, JPIP) -@param j3d J3D decompressor handle -@param cio Input buffer stream -@return Returns a decoded volume if successful, returns NULL otherwise -*/ -opj_volume_t* j3d_decode_jpt_stream(opj_j3d_t *j3d, opj_cio_t *cio); -/** -Creates a J3D compression structure -@param cinfo Codec context info -@return Returns a handle to a J3D compressor if successful, returns NULL otherwise -*/ -opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo); -/** -Destroy a J3D compressor handle -@param j3d J3D compressor handle to destroy -*/ -void j3d_destroy_compress(opj_j3d_t *j3d); -/** -Setup the encoder parameters using the current volume and using user parameters. -Coding parameters are returned in j3d->cp. -@param j3d J3D compressor handle -@param parameters compression parameters -@param volume input filled volume -*/ -void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume); -/** -Encode an volume into a JPEG-2000 codestream -@param j3d J3D compressor handle -@param cio Output buffer stream -@param volume Volume to encode -@param index Name of the index file if required, NULL otherwise -@return Returns true if successful, returns false otherwise -*/ -bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __J3D_H */ diff --git a/jp3d/libjp3dvm/jp3d_lib.c b/jp3d/libjp3dvm/jp3d_lib.c deleted file mode 100755 index fe19ab8b..00000000 --- a/jp3d/libjp3dvm/jp3d_lib.c +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifdef WIN32 -#include -#else -#include -#include -#include -#endif /* WIN32 */ -#include "opj_includes.h" - -double opj_clock() { -#ifdef WIN32 - /* WIN32: use QueryPerformance (very accurate) */ - LARGE_INTEGER freq , t ; - /* freq is the clock speed of the CPU */ - QueryPerformanceFrequency(&freq) ; - /* cout << "freq = " << ((double) freq.QuadPart) << endl; */ - /* t is the high resolution performance counter (see MSDN) */ - QueryPerformanceCounter ( & t ) ; - return ( t.QuadPart /(double) freq.QuadPart ) ; -#else - /* Unix or Linux: use resource usage */ - struct rusage t; - double procTime; - /* (1) Get the rusage data structure at this moment (man getrusage) */ - getrusage(0,&t); - /* (2) What is the elapsed time ? - CPU time = User time + System time */ - /* (2a) Get the seconds */ - procTime = t.ru_utime.tv_sec + t.ru_stime.tv_sec; - /* (2b) More precisely! Get the microseconds part ! */ - return ( procTime + (t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ; -#endif -} - -void* opj_malloc( size_t size ) { - void *memblock = malloc(size); - if(memblock) { - memset(memblock, 0, size); - } - return memblock; -} - -void* opj_realloc( void *memblock, size_t size ) { - return realloc(memblock, size); -} - -void opj_free( void *memblock ) { - free(memblock); -} - - diff --git a/jp3d/libjp3dvm/jp3d_lib.h b/jp3d/libjp3dvm/jp3d_lib.h deleted file mode 100755 index 45b28309..00000000 --- a/jp3d/libjp3dvm/jp3d_lib.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __J3D_LIB_H -#define __J3D_LIB_H -/** -@file jp3d_lib.h -@brief Internal functions - -The functions in JP3D_LIB.C are internal utilities mainly used for memory management. -*/ - -/** @defgroup MISC MISC - Miscellaneous internal functions */ -/*@{*/ - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ - -/** -Difference in successive opj_clock() calls tells you the elapsed time -@return Returns time in seconds -*/ -double opj_clock(); - -/** -Allocate a memory block with elements initialized to 0 -@param size Bytes to allocate -@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available -*/ -void* opj_malloc( size_t size ); - -/** -Reallocate memory blocks. -@param memblock Pointer to previously allocated memory block -@param size New size in bytes -@return Returns a void pointer to the reallocated (and possibly moved) memory block -*/ -void* opj_realloc( void *memblock, size_t size ); - -/** -Deallocates or frees a memory block. -@param memblock Previously allocated memory block to be freed -*/ -void opj_free( void *memblock ); - -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __J3D_LIB_H */ - diff --git a/jp3d/libjp3dvm/mct.c b/jp3d/libjp3dvm/mct.c deleted file mode 100755 index be4b875e..00000000 --- a/jp3d/libjp3dvm/mct.c +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/* */ -/* This table contains the norms of the basis function of the reversible MCT. */ -/* */ -static const double mct_norms[3] = { 1.732, .8292, .8292 }; - -/* */ -/* This table contains the norms of the basis function of the irreversible MCT. */ -/* */ -static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 }; - -/* */ -/* Foward reversible MCT. */ -/* */ -void mct_encode(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int r, g, b, y, u, v; - r = c0[i]; - g = c1[i]; - b = c2[i]; - y = (r + (g << 1) + b) >> 2; - u = b - g; - v = r - g; - c0[i] = y; - c1[i] = u; - c2[i] = v; - } -} - -/* */ -/* Inverse reversible MCT. */ -/* */ -void mct_decode(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int y, u, v, r, g, b; - y = c0[i]; - u = c1[i]; - v = c2[i]; - g = y - ((u + v) >> 2); - r = v + g; - b = u + g; - c0[i] = r; - c1[i] = g; - c2[i] = b; - } -} - -/* */ -/* Get norm of basis function of reversible MCT. */ -/* */ -double mct_getnorm(int compno) { - return mct_norms[compno]; -} - -/* */ -/* Foward irreversible MCT. */ -/* */ -void mct_encode_real(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int r, g, b, y, u, v; - r = c0[i]; - g = c1[i]; - b = c2[i]; - y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934); - u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096); - v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666); - c0[i] = y; - c1[i] = u; - c2[i] = v; - } -} - -/* */ -/* Inverse irreversible MCT. */ -/* */ -void mct_decode_real(int *c0, int *c1, int *c2, int n) { - int i; - for (i = 0; i < n; i++) { - int y, u, v, r, g, b; - y = c0[i]; - u = c1[i]; - v = c2[i]; - r = y + fix_mul(v, 11485); - g = y - fix_mul(u, 2819) - fix_mul(v, 5850); - b = y + fix_mul(u, 14516); - c0[i] = r; - c1[i] = g; - c2[i] = b; - } -} - -/* */ -/* Get norm of basis function of irreversible MCT. */ -/* */ -double mct_getnorm_real(int compno) { - return mct_norms_real[compno]; -} diff --git a/jp3d/libjp3dvm/mct.h b/jp3d/libjp3dvm/mct.h deleted file mode 100755 index 81513f7c..00000000 --- a/jp3d/libjp3dvm/mct.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __MCT_H -#define __MCT_H -/** -@file mct.h -@brief Implementation of a multi-component transforms (MCT) - -The functions in MCT.C have for goal to realize reversible and irreversible multicomponent -transform. The functions in MCT.C are used by some function in TCD.C. -*/ - -/** @defgroup MCT MCT - Implementation of a multi-component transform */ -/*@{*/ - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Apply a reversible multi-component transform to an image -@param c0 Samples for red component -@param c1 Samples for green component -@param c2 Samples blue component -@param n Number of samples for each component -*/ -void mct_encode(int *c0, int *c1, int *c2, int n); -/** -Apply a reversible multi-component inverse transform to an image -@param c0 Samples for luminance component -@param c1 Samples for red chrominance component -@param c2 Samples for blue chrominance component -@param n Number of samples for each component -*/ -void mct_decode(int *c0, int *c1, int *c2, int n); -/** -Get norm of the basis function used for the reversible multi-component transform -@param compno Number of the component (0->Y, 1->U, 2->V) -@return -*/ -double mct_getnorm(int compno); - -/** -Apply an irreversible multi-component transform to an image -@param c0 Samples for red component -@param c1 Samples for green component -@param c2 Samples blue component -@param n Number of samples for each component -*/ -void mct_encode_real(int *c0, int *c1, int *c2, int n); -/** -Apply an irreversible multi-component inverse transform to an image -@param c0 Samples for luminance component -@param c1 Samples for red chrominance component -@param c2 Samples for blue chrominance component -@param n Number of samples for each component -*/ -void mct_decode_real(int *c0, int *c1, int *c2, int n); -/** -Get norm of the basis function used for the irreversible multi-component transform -@param compno Number of the component (0->Y, 1->U, 2->V) -@return -*/ -double mct_getnorm_real(int compno); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __MCT_H */ diff --git a/jp3d/libjp3dvm/mqc.c b/jp3d/libjp3dvm/mqc.c deleted file mode 100755 index 4117bf1f..00000000 --- a/jp3d/libjp3dvm/mqc.c +++ /dev/null @@ -1,548 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup MQC MQC - Implementation of an MQ-Coder */ -/*@{*/ - -/** @name Local static functions */ -/*@{*/ - -/** -Output a byte, doing bit-stuffing if necessary. -After a 0xff byte, the next byte must be smaller than 0x90. -@param mqc MQC handle -*/ -static void mqc_byteout(opj_mqc_t *mqc); -/** -Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000 -@param mqc MQC handle -*/ -static void mqc_renorme(opj_mqc_t *mqc); -/** -Encode the most probable symbol -@param mqc MQC handle -*/ -static void mqc_codemps(opj_mqc_t *mqc); -/** -Encode the most least symbol -@param mqc MQC handle -*/ -static void mqc_codelps(opj_mqc_t *mqc); -/** -Fill mqc->c with 1's for flushing -@param mqc MQC handle -*/ -static void mqc_setbits(opj_mqc_t *mqc); -/** -Exchange MPS with LPS -@param mqc MQC handle -@return -*/ -static int mqc_mpsexchange(opj_mqc_t *mqc); -/** -Exchange LPS with MPS -@param mqc MQC handle -@return -*/ -static int mqc_lpsexchange(opj_mqc_t *mqc); -/** -Input a byte -@param mqc MQC handle -*/ -static void mqc_bytein(opj_mqc_t *mqc); -/** -Renormalize mqc->a and mqc->c while decoding -@param mqc MQC handle -*/ -static void mqc_renormd(opj_mqc_t *mqc); - -/*@}*/ - -/*@}*/ - -/* */ -/* This array defines all the possible states for a context. */ -/* */ -static opj_mqc_state_t mqc_states[47 * 2] = { - {0x5601, 0, &mqc_states[2], &mqc_states[3]}, - {0x5601, 1, &mqc_states[3], &mqc_states[2]}, - {0x3401, 0, &mqc_states[4], &mqc_states[12]}, - {0x3401, 1, &mqc_states[5], &mqc_states[13]}, - {0x1801, 0, &mqc_states[6], &mqc_states[18]}, - {0x1801, 1, &mqc_states[7], &mqc_states[19]}, - {0x0ac1, 0, &mqc_states[8], &mqc_states[24]}, - {0x0ac1, 1, &mqc_states[9], &mqc_states[25]}, - {0x0521, 0, &mqc_states[10], &mqc_states[58]}, - {0x0521, 1, &mqc_states[11], &mqc_states[59]}, - {0x0221, 0, &mqc_states[76], &mqc_states[66]}, - {0x0221, 1, &mqc_states[77], &mqc_states[67]}, - {0x5601, 0, &mqc_states[14], &mqc_states[13]}, - {0x5601, 1, &mqc_states[15], &mqc_states[12]}, - {0x5401, 0, &mqc_states[16], &mqc_states[28]}, - {0x5401, 1, &mqc_states[17], &mqc_states[29]}, - {0x4801, 0, &mqc_states[18], &mqc_states[28]}, - {0x4801, 1, &mqc_states[19], &mqc_states[29]}, - {0x3801, 0, &mqc_states[20], &mqc_states[28]}, - {0x3801, 1, &mqc_states[21], &mqc_states[29]}, - {0x3001, 0, &mqc_states[22], &mqc_states[34]}, - {0x3001, 1, &mqc_states[23], &mqc_states[35]}, - {0x2401, 0, &mqc_states[24], &mqc_states[36]}, - {0x2401, 1, &mqc_states[25], &mqc_states[37]}, - {0x1c01, 0, &mqc_states[26], &mqc_states[40]}, - {0x1c01, 1, &mqc_states[27], &mqc_states[41]}, - {0x1601, 0, &mqc_states[58], &mqc_states[42]}, - {0x1601, 1, &mqc_states[59], &mqc_states[43]}, - {0x5601, 0, &mqc_states[30], &mqc_states[29]}, - {0x5601, 1, &mqc_states[31], &mqc_states[28]}, - {0x5401, 0, &mqc_states[32], &mqc_states[28]}, - {0x5401, 1, &mqc_states[33], &mqc_states[29]}, - {0x5101, 0, &mqc_states[34], &mqc_states[30]}, - {0x5101, 1, &mqc_states[35], &mqc_states[31]}, - {0x4801, 0, &mqc_states[36], &mqc_states[32]}, - {0x4801, 1, &mqc_states[37], &mqc_states[33]}, - {0x3801, 0, &mqc_states[38], &mqc_states[34]}, - {0x3801, 1, &mqc_states[39], &mqc_states[35]}, - {0x3401, 0, &mqc_states[40], &mqc_states[36]}, - {0x3401, 1, &mqc_states[41], &mqc_states[37]}, - {0x3001, 0, &mqc_states[42], &mqc_states[38]}, - {0x3001, 1, &mqc_states[43], &mqc_states[39]}, - {0x2801, 0, &mqc_states[44], &mqc_states[38]}, - {0x2801, 1, &mqc_states[45], &mqc_states[39]}, - {0x2401, 0, &mqc_states[46], &mqc_states[40]}, - {0x2401, 1, &mqc_states[47], &mqc_states[41]}, - {0x2201, 0, &mqc_states[48], &mqc_states[42]}, - {0x2201, 1, &mqc_states[49], &mqc_states[43]}, - {0x1c01, 0, &mqc_states[50], &mqc_states[44]}, - {0x1c01, 1, &mqc_states[51], &mqc_states[45]}, - {0x1801, 0, &mqc_states[52], &mqc_states[46]}, - {0x1801, 1, &mqc_states[53], &mqc_states[47]}, - {0x1601, 0, &mqc_states[54], &mqc_states[48]}, - {0x1601, 1, &mqc_states[55], &mqc_states[49]}, - {0x1401, 0, &mqc_states[56], &mqc_states[50]}, - {0x1401, 1, &mqc_states[57], &mqc_states[51]}, - {0x1201, 0, &mqc_states[58], &mqc_states[52]}, - {0x1201, 1, &mqc_states[59], &mqc_states[53]}, - {0x1101, 0, &mqc_states[60], &mqc_states[54]}, - {0x1101, 1, &mqc_states[61], &mqc_states[55]}, - {0x0ac1, 0, &mqc_states[62], &mqc_states[56]}, - {0x0ac1, 1, &mqc_states[63], &mqc_states[57]}, - {0x09c1, 0, &mqc_states[64], &mqc_states[58]}, - {0x09c1, 1, &mqc_states[65], &mqc_states[59]}, - {0x08a1, 0, &mqc_states[66], &mqc_states[60]}, - {0x08a1, 1, &mqc_states[67], &mqc_states[61]}, - {0x0521, 0, &mqc_states[68], &mqc_states[62]}, - {0x0521, 1, &mqc_states[69], &mqc_states[63]}, - {0x0441, 0, &mqc_states[70], &mqc_states[64]}, - {0x0441, 1, &mqc_states[71], &mqc_states[65]}, - {0x02a1, 0, &mqc_states[72], &mqc_states[66]}, - {0x02a1, 1, &mqc_states[73], &mqc_states[67]}, - {0x0221, 0, &mqc_states[74], &mqc_states[68]}, - {0x0221, 1, &mqc_states[75], &mqc_states[69]}, - {0x0141, 0, &mqc_states[76], &mqc_states[70]}, - {0x0141, 1, &mqc_states[77], &mqc_states[71]}, - {0x0111, 0, &mqc_states[78], &mqc_states[72]}, - {0x0111, 1, &mqc_states[79], &mqc_states[73]}, - {0x0085, 0, &mqc_states[80], &mqc_states[74]}, - {0x0085, 1, &mqc_states[81], &mqc_states[75]}, - {0x0049, 0, &mqc_states[82], &mqc_states[76]}, - {0x0049, 1, &mqc_states[83], &mqc_states[77]}, - {0x0025, 0, &mqc_states[84], &mqc_states[78]}, - {0x0025, 1, &mqc_states[85], &mqc_states[79]}, - {0x0015, 0, &mqc_states[86], &mqc_states[80]}, - {0x0015, 1, &mqc_states[87], &mqc_states[81]}, - {0x0009, 0, &mqc_states[88], &mqc_states[82]}, - {0x0009, 1, &mqc_states[89], &mqc_states[83]}, - {0x0005, 0, &mqc_states[90], &mqc_states[84]}, - {0x0005, 1, &mqc_states[91], &mqc_states[85]}, - {0x0001, 0, &mqc_states[90], &mqc_states[86]}, - {0x0001, 1, &mqc_states[91], &mqc_states[87]}, - {0x5601, 0, &mqc_states[92], &mqc_states[92]}, - {0x5601, 1, &mqc_states[93], &mqc_states[93]}, -}; - -/* -========================================================== - local functions -========================================================== -*/ - -static void mqc_byteout(opj_mqc_t *mqc) { - if (*mqc->bp == 0xff) { - mqc->bp++; - *mqc->bp = mqc->c >> 20; - mqc->c &= 0xfffff; - mqc->ct = 7; - } else { - if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */ - mqc->bp++; - *mqc->bp = mqc->c >> 19; - mqc->c &= 0x7ffff; - mqc->ct = 8; - } else { - (*mqc->bp)++; - if (*mqc->bp == 0xff) { - mqc->c &= 0x7ffffff; - mqc->bp++; - *mqc->bp = mqc->c >> 20; - mqc->c &= 0xfffff; - mqc->ct = 7; - } else { - mqc->bp++; - *mqc->bp = mqc->c >> 19; - mqc->c &= 0x7ffff; - mqc->ct = 8; - } - } - } -} - -static void mqc_renorme(opj_mqc_t *mqc) { - do { - mqc->a <<= 1; - mqc->c <<= 1; - mqc->ct--; - if (mqc->ct == 0) { - mqc_byteout(mqc); - } - } while ((mqc->a & 0x8000) == 0); -} - -static void mqc_codemps(opj_mqc_t *mqc) { - mqc->a -= (*mqc->curctx)->qeval; - if ((mqc->a & 0x8000) == 0) { - if (mqc->a < (*mqc->curctx)->qeval) { - mqc->a = (*mqc->curctx)->qeval; - } else { - mqc->c += (*mqc->curctx)->qeval; - } - *mqc->curctx = (*mqc->curctx)->nmps; - mqc_renorme(mqc); - } else { - mqc->c += (*mqc->curctx)->qeval; - } -} - -static void mqc_codelps(opj_mqc_t *mqc) { - mqc->a -= (*mqc->curctx)->qeval; - if (mqc->a < (*mqc->curctx)->qeval) { - mqc->c += (*mqc->curctx)->qeval; - } else { - mqc->a = (*mqc->curctx)->qeval; - } - *mqc->curctx = (*mqc->curctx)->nlps; - mqc_renorme(mqc); -} - -static void mqc_setbits(opj_mqc_t *mqc) { - unsigned int tempc = mqc->c + mqc->a; - mqc->c |= 0xffff; - if (mqc->c >= tempc) { - mqc->c -= 0x8000; - } -} - -static int mqc_mpsexchange(opj_mqc_t *mqc) { - int d; - if (mqc->a < (*mqc->curctx)->qeval) { - d = 1 - (*mqc->curctx)->mps; - *mqc->curctx = (*mqc->curctx)->nlps; - } else { - d = (*mqc->curctx)->mps; - *mqc->curctx = (*mqc->curctx)->nmps; - } - - return d; -} - -static int mqc_lpsexchange(opj_mqc_t *mqc) { - int d; - if (mqc->a < (*mqc->curctx)->qeval) { - mqc->a = (*mqc->curctx)->qeval; - d = (*mqc->curctx)->mps; - *mqc->curctx = (*mqc->curctx)->nmps; - } else { - mqc->a = (*mqc->curctx)->qeval; - d = 1 - (*mqc->curctx)->mps; - *mqc->curctx = (*mqc->curctx)->nlps; - } - - return d; -} - -static void mqc_bytein(opj_mqc_t *mqc) { - if (mqc->bp != mqc->end) { - unsigned int c; - if (mqc->bp + 1 != mqc->end) { - c = *(mqc->bp + 1); - } else { - c = 0xff; - } - if (*mqc->bp == 0xff) { - if (c > 0x8f) { - mqc->c += 0xff00; - mqc->ct = 8; - } else { - mqc->bp++; - mqc->c += c << 9; - mqc->ct = 7; - } - } else { - mqc->bp++; - mqc->c += c << 8; - mqc->ct = 8; - } - } else { - mqc->c += 0xff00; - mqc->ct = 8; - } -} - -static void mqc_renormd(opj_mqc_t *mqc) { - do { - if (mqc->ct == 0) { - mqc_bytein(mqc); - } - mqc->a <<= 1; - mqc->c <<= 1; - mqc->ct--; - } while (mqc->a < 0x8000); -} - -/* -========================================================== - MQ-Coder interface -========================================================== -*/ - -opj_mqc_t* mqc_create() { - opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t)); - return mqc; -} - -void mqc_destroy(opj_mqc_t *mqc) { - if(mqc) { - opj_free(mqc); - } -} - -int mqc_numbytes(opj_mqc_t *mqc) { - return mqc->bp - mqc->start; -} - -void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) { - mqc_setcurctx(mqc, 0); - mqc->a = 0x8000; - mqc->c = 0; - mqc->bp = bp - 1; - mqc->ct = 12; - if (*mqc->bp == 0xff) { - mqc->ct = 13; - } - mqc->start = bp; -} - -void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) { - mqc->curctx = &mqc->ctxs[ctxno]; -} - -void mqc_encode(opj_mqc_t *mqc, int d) { - if ((*mqc->curctx)->mps == d) { - mqc_codemps(mqc); - } else { - mqc_codelps(mqc); - } -} - -void mqc_flush(opj_mqc_t *mqc) { - mqc_setbits(mqc); - mqc->c <<= mqc->ct; - mqc_byteout(mqc); - mqc->c <<= mqc->ct; - mqc_byteout(mqc); - - if (*mqc->bp != 0xff) { - mqc->bp++; - } -} - -void mqc_bypass_init_enc(opj_mqc_t *mqc) { - mqc->c = 0; - mqc->ct = 8; - /*if (*mqc->bp == 0xff) { - mqc->ct = 7; - } */ -} - -void mqc_bypass_enc(opj_mqc_t *mqc, int d) { - mqc->ct--; - mqc->c = mqc->c + (d << mqc->ct); - if (mqc->ct == 0) { - mqc->bp++; - *mqc->bp = mqc->c; - mqc->ct = 8; - if (*mqc->bp == 0xff) { - mqc->ct = 7; - } - mqc->c = 0; - } -} - -int mqc_bypass_flush_enc(opj_mqc_t *mqc) { - unsigned char bit_padding; - - bit_padding = 0; - - if (mqc->ct != 0) { - while (mqc->ct > 0) { - mqc->ct--; - mqc->c += bit_padding << mqc->ct; - bit_padding = (bit_padding + 1) & 0x01; - } - mqc->bp++; - *mqc->bp = mqc->c; - mqc->ct = 8; - mqc->c = 0; - } - - return 1; -} - -void mqc_reset_enc(opj_mqc_t *mqc) { - mqc_resetstates(mqc); - mqc_setstate(mqc, 18, 0, 46); - mqc_setstate(mqc, 0, 0, 3); - mqc_setstate(mqc, 1, 0, 4); -} - -void mqc_reset_enc_3(opj_mqc_t *mqc) { - mqc_resetstates(mqc); - mqc_setstate(mqc, T1_3D_CTXNO_UNI, 0, 46); - mqc_setstate(mqc, T1_3D_CTXNO_AGG, 0, 3); - mqc_setstate(mqc, T1_3D_CTXNO_ZC, 0, 4); -} - -int mqc_restart_enc(opj_mqc_t *mqc) { - int correction = 1; - - /* */ - int n = 27 - 15 - mqc->ct; - mqc->c <<= mqc->ct; - while (n > 0) { - mqc_byteout(mqc); - n -= mqc->ct; - mqc->c <<= mqc->ct; - } - mqc_byteout(mqc); - - return correction; -} - -void mqc_restart_init_enc(opj_mqc_t *mqc) { - /* */ - mqc_setcurctx(mqc, 0); - mqc->a = 0x8000; - mqc->c = 0; - mqc->ct = 12; - mqc->bp--; - if (*mqc->bp == 0xff) { - mqc->ct = 13; - } -} - -void mqc_erterm_enc(opj_mqc_t *mqc) { - int k = 11 - mqc->ct + 1; - - while (k > 0) { - mqc->c <<= mqc->ct; - mqc->ct = 0; - mqc_byteout(mqc); - k -= mqc->ct; - } - - if (*mqc->bp != 0xff) { - mqc_byteout(mqc); - } -} - -void mqc_segmark_enc(opj_mqc_t *mqc) { - int i; - mqc_setcurctx(mqc, 18); - - for (i = 1; i < 5; i++) { - mqc_encode(mqc, i % 2); - } -} - -void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { - mqc_setcurctx(mqc, 0); - mqc->start = bp; - mqc->end = bp + len; - mqc->bp = bp; - if (len==0) mqc->c = 0xff << 16; - else mqc->c = *mqc->bp << 16; - mqc_bytein(mqc); - mqc->c <<= 7; - mqc->ct -= 7; - mqc->a = 0x8000; -} - -int mqc_decode(opj_mqc_t *mqc) { - int d; - mqc->a -= (*mqc->curctx)->qeval; - if ((mqc->c >> 16) < (*mqc->curctx)->qeval) { - d = mqc_lpsexchange(mqc); - mqc_renormd(mqc); - } else { - mqc->c -= (*mqc->curctx)->qeval << 16; - if ((mqc->a & 0x8000) == 0) { - d = mqc_mpsexchange(mqc); - mqc_renormd(mqc); - } else { - d = (*mqc->curctx)->mps; - } - } - - return d; -} - -void mqc_resetstates(opj_mqc_t *mqc) { - int i; - for (i = 0; i < MQC_NUMCTXS; i++) { - mqc->ctxs[i] = mqc_states; - } -} - -void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) { - mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)]; -} - - diff --git a/jp3d/libjp3dvm/mqc.h b/jp3d/libjp3dvm/mqc.h deleted file mode 100755 index 67cfc008..00000000 --- a/jp3d/libjp3dvm/mqc.h +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __MQC_H -#define __MQC_H -/** -@file mqc.h -@brief Implementation of an MQ-Coder (MQC) - -The functions in MQC.C have for goal to realize the MQ-coder operations. The functions -in MQC.C are used by some function in T1.C. -*/ - -/** @defgroup MQC MQC - Implementation of an MQ-Coder */ -/*@{*/ - -/** -This struct defines the state of a context. -*/ -typedef struct opj_mqc_state { - /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */ - unsigned int qeval; - /** the Most Probable Symbol (0 or 1) */ - int mps; - /** next state if the next encoded symbol is the MPS */ - struct opj_mqc_state *nmps; - /** next state if the next encoded symbol is the LPS */ - struct opj_mqc_state *nlps; -} opj_mqc_state_t; - -#define MQC_NUMCTXS 32 - -/** -MQ coder -*/ -typedef struct opj_mqc { - unsigned int c; - unsigned int a; - unsigned int ct; - unsigned char *bp; - unsigned char *start; - unsigned char *end; - opj_mqc_state_t *ctxs[MQC_NUMCTXS]; - opj_mqc_state_t **curctx; -} opj_mqc_t; - -/** @name Exported functions */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a new MQC handle -@return Returns a new MQC handle if successful, returns NULL otherwise -*/ -opj_mqc_t* mqc_create(); -/** -Destroy a previously created MQC handle -@param mqc MQC handle to destroy -*/ -void mqc_destroy(opj_mqc_t *mqc); -/** -Return the number of bytes written/read since initialisation -@param mqc MQC handle -@return Returns the number of bytes already encoded -*/ -int mqc_numbytes(opj_mqc_t *mqc); -/** -Reset the states of all the context of the coder/decoder -(each context is set to a state where 0 and 1 are more or less equiprobable) -@param mqc MQC handle -*/ -void mqc_resetstates(opj_mqc_t *mqc); -/** -Set the state of a particular context -@param mqc MQC handle -@param ctxno Number that identifies the context -@param msb The MSB of the new state of the context -@param prob Number that identifies the probability of the symbols for the new state of the context -*/ -void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob); -/** -Initialize the encoder -@param mqc MQC handle -@param bp Pointer to the start of the buffer where the bytes will be written -*/ -void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp); -/** -Set the current context used for coding/decoding -@param mqc MQC handle -@param ctxno Number that identifies the context -*/ -void mqc_setcurctx(opj_mqc_t *mqc, int ctxno); -/** -Encode a symbol using the MQ-coder -@param mqc MQC handle -@param d The symbol to be encoded (0 or 1) -*/ -void mqc_encode(opj_mqc_t *mqc, int d); -/** -Flush the encoder, so that all remaining data is written -@param mqc MQC handle -*/ -void mqc_flush(opj_mqc_t *mqc); -/** -BYPASS mode switch, initialization operation. -JPEG 2000 p 505. -

Not fully implemented and tested !!

-@param mqc MQC handle -*/ -void mqc_bypass_init_enc(opj_mqc_t *mqc); -/** -BYPASS mode switch, coding operation. -JPEG 2000 p 505. -

Not fully implemented and tested !!

-@param mqc MQC handle -@param d The symbol to be encoded (0 or 1) -*/ -void mqc_bypass_enc(opj_mqc_t *mqc, int d); -/** -BYPASS mode switch, flush operation -

Not fully implemented and tested !!

-@param mqc MQC handle -@return Returns 1 (always) -*/ -int mqc_bypass_flush_enc(opj_mqc_t *mqc); -/** -RESET mode switch -@param mqc MQC handle -*/ -void mqc_reset_enc(opj_mqc_t *mqc); -/** -RESET mode switch -@param mqc MQC handle -*/ -void mqc_reset_enc_3(opj_mqc_t *mqc); -/** -RESTART mode switch (TERMALL) -@param mqc MQC handle -@return Returns 1 (always) -*/ -int mqc_restart_enc(opj_mqc_t *mqc); -/** -RESTART mode switch (TERMALL) reinitialisation -@param mqc MQC handle -*/ -void mqc_restart_init_enc(opj_mqc_t *mqc); -/** -ERTERM mode switch (PTERM) -@param mqc MQC handle -*/ -void mqc_erterm_enc(opj_mqc_t *mqc); -/** -SEGMARK mode switch (SEGSYM) -@param mqc MQC handle -*/ -void mqc_segmark_enc(opj_mqc_t *mqc); -/** -Initialize the decoder -@param mqc MQC handle -@param bp Pointer to the start of the buffer from which the bytes will be read -@param len Length of the input buffer -*/ -void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len); -/** -Decode a symbol -@param mqc MQC handle -@return Returns the decoded symbol (0 or 1) -*/ -int mqc_decode(opj_mqc_t *mqc); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __MQC_H */ diff --git a/jp3d/libjp3dvm/openjpeg.c b/jp3d/libjp3dvm/openjpeg.c deleted file mode 100755 index 2a6fe7c6..00000000 --- a/jp3d/libjp3dvm/openjpeg.c +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifdef WIN32 -#include -#endif /* WIN32 */ - -#include "opj_includes.h" - -/* ---------------------------------------------------------------------- */ -#ifdef WIN32 -#ifndef OPJ_STATIC -BOOL APIENTRY -DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - switch (ul_reason_for_call) { - case DLL_PROCESS_ATTACH : - break; - case DLL_PROCESS_DETACH : - break; - case DLL_THREAD_ATTACH : - case DLL_THREAD_DETACH : - break; - } - - return TRUE; -} -#endif /* OPJ_STATIC */ -#endif /* WIN32 */ - -/* ---------------------------------------------------------------------- */ - -const char* OPJ_CALLCONV opj_version() { - return OPENJPEG_VERSION; -} -opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) { - opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t)); - if(!dinfo) return NULL; - dinfo->is_decompressor = true; - switch(format) { - case CODEC_J3D: - case CODEC_J2K: - /* get a J3D decoder handle */ - dinfo->j3d_handle = (void*)j3d_create_decompress((opj_common_ptr)dinfo); - if(!dinfo->j3d_handle) { - opj_free(dinfo); - return NULL; - } - break; - default: - opj_free(dinfo); - return NULL; - } - - dinfo->codec_format = format; - - return dinfo; -} - -void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) { - if(dinfo) { - /* destroy the codec */ - if(dinfo->codec_format != CODEC_UNKNOWN) { - j3d_destroy_decompress((opj_j3d_t*)dinfo->j3d_handle); - } - /* destroy the decompressor */ - opj_free(dinfo); - } -} - -void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) { - if(parameters) { - memset(parameters, 0, sizeof(opj_dparameters_t)); - /* default decoding parameters */ - parameters->cp_layer = 0; - parameters->cp_reduce[0] = 0; - parameters->cp_reduce[1] = 0; - parameters->cp_reduce[2] = 0; - parameters->bigendian = 0; - - parameters->decod_format = -1; - parameters->cod_format = -1; - } -} - -void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) { - if(dinfo && parameters) { - if (dinfo->codec_format != CODEC_UNKNOWN) { - j3d_setup_decoder((opj_j3d_t*)dinfo->j3d_handle, parameters); - } - } -} - -opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) { - if(dinfo && cio) { - if (dinfo->codec_format != CODEC_UNKNOWN) { - return j3d_decode((opj_j3d_t*)dinfo->j3d_handle, cio); - } - } - - return NULL; -} - -opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) { - opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t)); - if(!cinfo) return NULL; - cinfo->is_decompressor = false; - switch(format) { - case CODEC_J3D: - case CODEC_J2K: - /* get a J3D coder handle */ - cinfo->j3d_handle = (void*)j3d_create_compress((opj_common_ptr)cinfo); - if(!cinfo->j3d_handle) { - opj_free(cinfo); - return NULL; - } - break; - default: - opj_free(cinfo); - return NULL; - } - - cinfo->codec_format = format; - - return cinfo; -} - -void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) { - if(cinfo) { - /* destroy the codec */ - if (cinfo->codec_format != CODEC_UNKNOWN) { - j3d_destroy_compress((opj_j3d_t*)cinfo->j3d_handle); - } - /* destroy the decompressor */ - opj_free(cinfo); - } -} - -void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) { - if(parameters) { - memset(parameters, 0, sizeof(opj_cparameters_t)); - /* default coding parameters */ - parameters->numresolution[0] = 3; - parameters->numresolution[1] = 3; - parameters->numresolution[2] = 1; - parameters->cblock_init[0] = 64; - parameters->cblock_init[1] = 64; - parameters->cblock_init[2] = 64; - parameters->prog_order = LRCP; - parameters->roi_compno = -1; /* no ROI */ - parameters->atk_wt[0] = 1; /* 5-3 WT */ - parameters->atk_wt[1] = 1; /* 5-3 WT */ - parameters->atk_wt[2] = 1; /* 5-3 WT */ - parameters->irreversible = 0; - parameters->subsampling_dx = 1; - parameters->subsampling_dy = 1; - parameters->subsampling_dz = 1; - - parameters->decod_format = -1; - parameters->cod_format = -1; - parameters->encoding_format = ENCOD_2EB; - parameters->transform_format = TRF_2D_DWT; - } -} - -void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume) { - if(cinfo && parameters && volume) { - if (cinfo->codec_format != CODEC_UNKNOWN) { - j3d_setup_encoder((opj_j3d_t*)cinfo->j3d_handle, parameters, volume); - } - } -} - -bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index) { - if(cinfo && cio && volume) { - if (cinfo->codec_format != CODEC_UNKNOWN) { - return j3d_encode((opj_j3d_t*)cinfo->j3d_handle, cio, volume, index); - } - } - - return false; -} - - diff --git a/jp3d/libjp3dvm/openjpeg.h b/jp3d/libjp3dvm/openjpeg.h deleted file mode 100755 index 36c09aff..00000000 --- a/jp3d/libjp3dvm/openjpeg.h +++ /dev/null @@ -1,715 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef OPENJPEG_H -#define OPENJPEG_H - -#define OPENJPEG_VERSION "1.0.0" - -/* -========================================================== - Compiler directives -========================================================== -*/ - -#if defined(OPJ_STATIC) || !(defined(WIN32) || defined(__WIN32__)) -#define OPJ_API -#define OPJ_CALLCONV -#else -#define OPJ_CALLCONV __stdcall -/* -The following ifdef block is the standard way of creating macros which make exporting -from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS -symbol defined on the command line. this symbol should not be defined on any project -that uses this DLL. This way any other project whose source files include this file see -OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols -defined with this macro as being exported. -*/ -#ifdef OPJ_EXPORTS -#define OPJ_API __declspec(dllexport) -#else -#define OPJ_API __declspec(dllimport) -#endif /* OPJ_EXPORTS */ -#endif /* !OPJ_STATIC || !WIN32 */ - -#ifndef __cplusplus -#if defined(HAVE_STDBOOL_H) -/* -The C language implementation does correctly provide the standard header -file "stdbool.h". - */ -#include -#else -/* -The C language implementation does not provide the standard header file -"stdbool.h" as required by ISO/IEC 9899:1999. Try to compensate for this -braindamage below. -*/ -#if !defined(bool) -#define bool int -#endif -#if !defined(true) -#define true 1 -#endif -#if !defined(false) -#define false 0 -#endif -#endif -#endif /* __cplusplus */ - -/* -========================================================== - Useful constant definitions -========================================================== -*/ -#ifndef MAX_SLICES -#define MAX_SLICES 300 /**< Maximum allowed size for slices */ -#endif /* MAX_PATH */ - -#ifndef MAX_PATH -#define MAX_PATH 260 /**< Maximum allowed size for filenames */ -#endif /* MAX_PATH */ - -#define J3D_MAXRLVLS 32 /**< Number of maximum resolution level authorized */ -#define J3D_MAXBANDS (7*J3D_MAXRLVLS + 1) /**< Number of maximum sub-band linked to number of resolution level */ - -#define TINY 1.0E-20 -/* -========================================================== - enum definitions -========================================================== -*/ - -#define J2K_CFMT 0 -#define J3D_CFMT 1 -#define LSE_CFMT 2 - -#define BIN_DFMT 0 -#define PGX_DFMT 1 -#define IMG_DFMT 2 -/* ----------------------------------------------------------------------- */ - -/** Progression order */ -typedef enum PROG_ORDER { -/**< place-holder */ - PROG_UNKNOWN = -1, -/**< layer-resolution-component-precinct order */ - LRCP = 0, -/**< resolution-layer-component-precinct order */ - RLCP = 1, -/**< resolution-precinct-component-layer order */ - RPCL = 2, -/**< precinct-component-resolution-layer order */ - PCRL = 3, -/**< component-precinct-resolution-layer order */ - CPRL = 4 -} OPJ_PROG_ORDER; - -/** -Supported volume color spaces -*/ -typedef enum COLOR_SPACE { -/**< place-holder */ - CLRSPC_UNKNOWN = -1, -/**< sRGB */ - CLRSPC_SRGB = 1, -/**< grayscale */ - CLRSPC_GRAY = 2, -/**< YUV */ - CLRSPC_SYCC = 3 -} OPJ_COLOR_SPACE; - -/** -Supported codec -*/ -typedef enum CODEC_FORMAT { - /**< place-holder */ - CODEC_UNKNOWN = -1, -/**< JPEG-2000 codestream : read/write */ - CODEC_J2K = 0, -/**< JPEG-2000 Part 10 file format : read/write */ - CODEC_J3D = 1 -} OPJ_CODEC_FORMAT; - -/** -Supported entropy coding algorithms -*/ -typedef enum ENTROPY_CODING { -/**< place-holder */ - ENCOD_UNKNOWN = -1, -/**< 2D EBCOT encoding */ - ENCOD_2EB = 0, -/**< 3D EBCOT encoding */ - ENCOD_3EB = 1, -/**< Golomb-Rice coding with 2D context */ - ENCOD_2GR = 2, -/**< Golomb-Rice coding with 3D context */ - ENCOD_3GR = 3 -} OPJ_ENTROPY_CODING; - -/** -Supported transforms -*/ -typedef enum TRANSFORM { -/**< place-holder */ - TRF_UNKNOWN = -1, -/**< 2D DWT, no transform in axial dim */ - TRF_2D_DWT = 0, -/**< 3D DWT */ - TRF_3D_DWT = 1, -/**< 3D prediction*/ - TRF_3D_RLS = 2, - TRF_3D_LSE = 3 -} OPJ_TRANSFORM; -/* -========================================================== - event manager typedef definitions -========================================================== -*/ - -/** -Callback function prototype for events -@param msg Event message -@param client_data -*/ -typedef void (*opj_msg_callback) (const char *msg, void *client_data); - -/** -Message handler object -used for -
    -
  • Error messages -
  • Warning messages -
  • Debugging messages -
-*/ -typedef struct opj_event_mgr { - /** Error message callback if available, NULL otherwise */ - opj_msg_callback error_handler; - /** Warning message callback if available, NULL otherwise */ - opj_msg_callback warning_handler; - /** Debug message callback if available, NULL otherwise */ - opj_msg_callback info_handler; -} opj_event_mgr_t; - - -/* -========================================================== - codec typedef definitions -========================================================== -*/ - -/** -Progression order changes -*/ -typedef struct opj_poc { - int resno0, compno0; - int layno1, resno1, compno1; - OPJ_PROG_ORDER prg; - int tile; - char progorder[4]; -} opj_poc_t; - - -/** -Compression parameters -*/ -typedef struct opj_cparameters { -/** size of tile: tile_size_on = false (not in argument) or = true (in argument) */ - bool tile_size_on; -/** XTOsiz */ - int cp_tx0; -/** YTOsiz */ - int cp_ty0; -/** ZTOsiz */ - int cp_tz0; - -/** XTsiz */ - int cp_tdx; -/** YTsiz */ - int cp_tdy; -/** ZTsiz */ - int cp_tdz; - -/** allocation by rate/distortion */ - int cp_disto_alloc; -/** allocation by fixed layer */ - int cp_fixed_alloc; -/** add fixed_quality */ - int cp_fixed_quality; -/** fixed layer */ - int *cp_matrice; -/** number of layers */ - int tcp_numlayers; -/** rates for successive layers */ - float tcp_rates[100]; -/** psnr's for successive layers */ - float tcp_distoratio[100]; -/** comment for coding */ - char *cp_comment; -/** csty : coding style */ - int csty; -/** DC offset (DCO) */ - int dcoffset; -/** progression order (default LRCP) */ - OPJ_PROG_ORDER prog_order; -/** progression order changes */ - opj_poc_t POC[J3D_MAXRLVLS-1]; -/** number of progression order changes (POC), default to 0 */ - int numpocs; - -/** number of resolutions */ - int numresolution[3]; -/** initial code block width, height and depth, default to 64 */ - int cblock_init[3]; -/** mode switch (1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)) */ - int mode; - -/** 1 : use the irreversible DWT 9-7, 0 : use lossless compression (default) */ - int irreversible; -/** WT from ATK, default to 0 (false), no of atk used */ - int atk_wt[3]; -/** region of interest: affected component in [0..3], -1 means no ROI */ - int roi_compno; -/** region of interest: upshift value */ - int roi_shift; - -/* number of precinct size specifications */ - int res_spec; -/** initial precinct width */ - int prct_init[3][J3D_MAXRLVLS]; - -/** transform format 0: 0: 2DWT, 1: 2D1P, 2: 3DWT, 3: 3RLS */ - OPJ_TRANSFORM transform_format; -/** output file format 0: 2EB, 1: 3EB, 2: 2GR, 3: 3GR, 4: GRI */ - OPJ_ENTROPY_CODING encoding_format; - - /**@name command line encoder parameters (not used inside the library) */ - /*@{*/ - char infile[MAX_PATH]; /** input file name */ - char outfile[MAX_PATH]; /** output file name */ - char imgfile[MAX_PATH]; /** IMG file name for BIN volumes*/ - int index_on; /** creation of an index file, default to 0 (false) */ - char index[MAX_PATH]; /** index file name */ - - int volume_offset_x0; /** subvolume encoding: origin volume offset in x, y and z direction */ - int volume_offset_y0; - int volume_offset_z0; - - int subsampling_dx; /** subsampling value for dx */ - int subsampling_dy; - int subsampling_dz; - - int decod_format; /** input file format 0: BIN, 1: PGX */ - int cod_format; /** output file format 0: JP3D */ - /*@}*/ -} opj_cparameters_t; - -/** -Decompression parameters -*/ -typedef struct opj_dparameters { -/** Set the number of highest resolution levels to be discarded. if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, volume is decoded to the full resolution */ - int cp_reduce[3]; -/** Set the maximum number of quality layers to decode. if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */ - int cp_layer; - int bigendian; - - /**@name command line encoder parameters (not used inside the library) */ - /*@{*/ -/** input file name */ - char infile[MAX_PATH]; -/** output file name */ - char outfile[MAX_PATH]; -/** IMG file name for BIN volumes*/ - char imgfile[MAX_PATH]; -/** Original file name for PSNR measures*/ - char original[MAX_PATH]; -/** input file format 0: J2K, 1: JP3D */ - int decod_format; -/** input file format 0: BIN, 1: PGM */ - int cod_format; -/** original file format 0: BIN, 1: PGM */ - int orig_format; - /*@}*/ -} opj_dparameters_t; - -/** Common fields between JPEG-2000 compression and decompression master structs. */ -#define opj_common_fields \ - opj_event_mgr_t *event_mgr; /**< pointer to the event manager */\ - void * client_data; /**< Available for use by application */\ - bool is_decompressor; /**< So common code can tell which is which */\ - OPJ_CODEC_FORMAT codec_format; /**< selected codec */\ - OPJ_ENTROPY_CODING encoding_format; /**< selected entropy coding */\ - OPJ_TRANSFORM transform_format; /**< selected transform */\ - void *j3d_handle /**< pointer to the J3D codec */ - -/* Routines that are to be used by both halves of the library are declared - * to receive a pointer to this structure. There are no actual instances of - * opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t. - */ -typedef struct opj_common_struct { - opj_common_fields; /* Fields common to both master struct types */ - /* Additional fields follow in an actual opj_cinfo_t or - * opj_dinfo_t. All three structs must agree on these - * initial fields! (This would be a lot cleaner in C++.) - */ -} opj_common_struct_t; - -typedef opj_common_struct_t * opj_common_ptr; - -/** -Compression context info -*/ -typedef struct opj_cinfo { - /** Fields shared with opj_dinfo_t */ - opj_common_fields; - /* other specific fields go here */ -} opj_cinfo_t; - -/** -Decompression context info -*/ -typedef struct opj_dinfo { - /** Fields shared with opj_cinfo_t */ - opj_common_fields; - /* other specific fields go here */ -} opj_dinfo_t; - -/* -========================================================== - I/O stream typedef definitions -========================================================== -*/ - -/* - * Stream open flags. - */ -/** The stream was opened for reading. */ -#define OPJ_STREAM_READ 0x0001 -/** The stream was opened for writing. */ -#define OPJ_STREAM_WRITE 0x0002 - -/** -Byte input-output stream (CIO) -*/ -typedef struct opj_cio { -/** codec context */ - opj_common_ptr cinfo; -/** open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE */ - int openmode; -/** pointer to the start of the buffer */ - unsigned char *buffer; -/** buffer size in bytes */ - int length; -/** pointer to the start of the stream */ - unsigned char *start; -/** pointer to the end of the stream */ - unsigned char *end; -/** pointer to the current position */ - unsigned char *bp; -} opj_cio_t; - -/* -========================================================== - volume typedef definitions -========================================================== -*/ - -/** -Defines a single volume component -*/ -typedef struct opj_volume_comp { -/** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */ - int dx; -/** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */ - int dy; -/** ZRsiz: vertical separation of a sample of ith component with respect to the reference grid */ - int dz; -/** data width */ - int w; - /** data height */ - int h; - /** data length : no of slices */ - int l; - /** x component offset compared to the whole volume */ - int x0; - /** y component offset compared to the whole volume */ - int y0; - /** z component offset compared to the whole volume */ - int z0; - /** precision */ - int prec; - /** volume depth in bits */ - int bpp; - /** DC offset (15444-2) */ - int dcoffset; - /** signed (1) / unsigned (0) */ - int sgnd; - /** BE byte order (1) / LE byte order (0) */ - int bigendian; - /** number of decoded resolution */ - int resno_decoded[3]; - /** number of division by 2 of the out volume compared to the original size of volume */ - int factor[3]; - /** volume component data */ - int *data; -} opj_volume_comp_t; - -/** -Defines volume data and characteristics -*/ -typedef struct opj_volume { -/** XOsiz: horizontal offset from the origin of the reference grid to the left side of the volume area */ - int x0; -/** YOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */ - int y0; -/** ZOsiz: vertical offset from the origin of the reference grid to the top side of the volume area */ - int z0; -/** Xsiz: width of the reference grid */ - int x1; -/** Ysiz: height of the reference grid */ - int y1; -/** Zsiz: length of the reference grid */ - int z1; -/** number of components in the volume */ - int numcomps; -/** number of slices in the volume */ - int numslices; -/** color space: sRGB, Greyscale or YUV */ - OPJ_COLOR_SPACE color_space; -/** volume components */ - opj_volume_comp_t *comps; -} opj_volume_t; - -/** -Component parameters structure used by the opj_volume_create function -*/ -typedef struct opj_volume_comptparm { - /** XRsiz: horizontal separation of a sample of ith component with respect to the reference grid */ - int dx; - /** YRsiz: vertical separation of a sample of ith component with respect to the reference grid */ - int dy; - /** ZRsiz: axial separation of a sample of ith component with respect to the reference grid */ - int dz; - /** data width */ - int w; - /** data height */ - int h; - /** data length */ - int l; - /** x component offset compared to the whole volume */ - int x0; - /** y component offset compared to the whole volume */ - int y0; - /** z component offset compared to the whole volume */ - int z0; - /** precision */ - int prec; - /** volume depth in bits */ - int bpp; - /** signed (1) / unsigned (0) */ - int sgnd; - /** DC offset*/ - int dcoffset; - /** BE byte order (1) / LE byte order (0) */ - int bigendian; -} opj_volume_cmptparm_t; - -#ifdef __cplusplus -extern "C" { -#endif - - -/* -========================================================== - openjpeg version -========================================================== -*/ - -OPJ_API const char * OPJ_CALLCONV opj_version(); - -/* -========================================================== - volume functions definitions -========================================================== -*/ - -/** -Create an volume -@param numcmpts number of components -@param cmptparms components parameters -@param clrspc volume color space -@return returns a new volume structure if successful, returns NULL otherwise -*/ -OPJ_API opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc); - -/** -Deallocate any resources associated with an volume -@param volume volume to be destroyed -*/ -OPJ_API void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume); - -/* -========================================================== - stream functions definitions -========================================================== -*/ - -/** -Open and allocate a memory stream for read / write. -On reading, the user must provide a buffer containing encoded data. The buffer will be -wrapped by the returned CIO handle. -On writing, buffer parameters must be set to 0: a buffer will be allocated by the library -to contain encoded data. -@param cinfo Codec context info -@param buffer Reading: buffer address. Writing: NULL -@param length Reading: buffer length. Writing: 0 -@return Returns a CIO handle if successful, returns NULL otherwise -*/ -OPJ_API opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length); - -/** -Close and free a CIO handle -@param cio CIO handle to free -*/ -OPJ_API void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio); - -/** -Get position in byte stream -@param cio CIO handle -@return Returns the position in bytes -*/ -OPJ_API int OPJ_CALLCONV cio_tell(opj_cio_t *cio); -/** -Set position in byte stream -@param cio CIO handle -@param pos Position, in number of bytes, from the beginning of the stream -*/ -OPJ_API void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos); - -/* -========================================================== - event manager functions definitions -========================================================== -*/ - -OPJ_API opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context); - -/* -========================================================== - codec functions definitions -========================================================== -*/ -/** -Creates a J3D decompression structure -@param format Decoder to select -@return Returns a handle to a decompressor if successful, returns NULL otherwise -*/ -OPJ_API opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format); -/** -Destroy a decompressor handle -@param dinfo decompressor handle to destroy -*/ -OPJ_API void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo); -/** -Set decoding parameters to default values -@param parameters Decompression parameters -*/ -OPJ_API void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters); -/** -Setup the decoder decoding parameters using user parameters. -Decoding parameters are returned in j3d->cp. -@param dinfo decompressor handle -@param parameters decompression parameters -*/ -OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters); -/** -Decode an volume from a JPEG-2000 codestream -@param dinfo decompressor handle -@param cio Input buffer stream -@return Returns a decoded volume if successful, returns NULL otherwise -*/ -OPJ_API opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio); -/** -Creates a J3D/JP2 compression structure -@param format Coder to select -@return Returns a handle to a compressor if successful, returns NULL otherwise -*/ -OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format); -/** -Destroy a compressor handle -@param cinfo compressor handle to destroy -*/ -OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo); -/** -Set encoding parameters to default values, that means : -
    -
  • Lossless -
  • 1 tile -
  • Size of precinct : 2^15 x 2^15 (means 1 precinct) -
  • Size of code-block : 64 x 64 -
  • Number of resolutions: 6 -
  • No SOP marker in the codestream -
  • No EPH marker in the codestream -
  • No sub-sampling in x or y direction -
  • No mode switch activated -
  • Progression order: LRCP -
  • No index file -
  • No ROI upshifted -
  • No offset of the origin of the volume -
  • No offset of the origin of the tiles -
  • Reversible DWT 5-3 -
-@param parameters Compression parameters -*/ -OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters); -/** -Setup the encoder parameters using the current volume and using user parameters. -@param cinfo compressor handle -@param parameters compression parameters -@param volume input filled volume -*/ -OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume); -/** -Encode an volume into a JPEG-2000 codestream -@param cinfo compressor handle -@param cio Output buffer stream -@param volume Volume to encode -@param index Name of the index file if required, NULL otherwise -@return Returns true if successful, returns false otherwise -*/ -OPJ_API bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index); - -#ifdef __cplusplus -} -#endif - -#endif /* OPENJPEG_H */ diff --git a/jp3d/libjp3dvm/opj_includes.h b/jp3d/libjp3dvm/opj_includes.h deleted file mode 100755 index ed975733..00000000 --- a/jp3d/libjp3dvm/opj_includes.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef OPJ_INCLUDES_H -#define OPJ_INCLUDES_H - -/* - ========================================================== - Standard includes used by the library - ========================================================== -*/ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -/* - ========================================================== - OpenJPEG interface - ========================================================== - */ -#include "openjpeg.h" - -/* - ========================================================== - OpenJPEG modules - ========================================================== -*/ - -#include "dirent.h" -#include "jp3d_lib.h" -#include "event.h" -#include "cio.h" - -#include "volume.h" -#include "jp3d.h" - -#include "mqc.h" -#include "raw.h" -#include "bio.h" -#include "tgt.h" -#include "tcd.h" -#include "t1.h" -#include "t1_3d.h" -#include "dwt.h" -#include "pi.h" -#include "t2.h" -#include "mct.h" -#include "int.h" -#include "fix.h" - -//#include "pred.h" -//#include "golomb.h" - -#endif /* OPJ_INCLUDES_H */ diff --git a/jp3d/libjp3dvm/pi.c b/jp3d/libjp3dvm/pi.c deleted file mode 100755 index 5fd673fd..00000000 --- a/jp3d/libjp3dvm/pi.c +++ /dev/null @@ -1,630 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez, LPI-UVA, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup PI PI - Implementation of a packet iterator */ -/*@{*/ - -/** @name Funciones locales */ -/*@{*/ - -/** -Get next packet in layer-resolution-component-precinct order. -@param pi packet iterator to modify -@return returns false if pi pointed to the last packet or else returns true -*/ -static bool pi_next_lrcp(opj_pi_iterator_t * pi); -/** -Get next packet in resolution-layer-component-precinct order. -@param pi packet iterator to modify -@return returns false if pi pointed to the last packet or else returns true -*/ -static bool pi_next_rlcp(opj_pi_iterator_t * pi); -/** -Get next packet in resolution-precinct-component-layer order. -@param pi packet iterator to modify -@return returns false if pi pointed to the last packet or else returns true -*/ -static bool pi_next_rpcl(opj_pi_iterator_t * pi); -/** -Get next packet in precinct-component-resolution-layer order. -@param pi packet iterator to modify -@return returns false if pi pointed to the last packet or else returns true -*/ -static bool pi_next_pcrl(opj_pi_iterator_t * pi); -/** -Get next packet in component-precinct-resolution-layer order. -@param pi packet iterator to modify -@return returns false if pi pointed to the last packet or else returns true -*/ -static bool pi_next_cprl(opj_pi_iterator_t * pi); - -/*@}*/ - -/*@}*/ - -/* -========================================================== - local functions -========================================================== -*/ - -static bool pi_next_lrcp(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; - - if (!pi->first) { - comp = &pi->comps[pi->compno]; - res = &comp->resolutions[pi->resno]; - goto LABEL_SKIP; - } else { - pi->first = 0; - } - - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolution[0]) { - continue; - } - res = &comp->resolutions[pi->resno]; - //for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1]); pi->precno++) { - for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); pi->precno++) { - index = pi->layno * pi->step_l - + pi->resno * pi->step_r - + pi->compno * pi->step_c - + pi->precno * pi->step_p; - if (!pi->include[index]) { - pi->include[index] = 1; - return true; - } -LABEL_SKIP:; - - } - } - } - } - - return false; -} - -static bool pi_next_rlcp(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; - - if (!pi->first) { - comp = &pi->comps[pi->compno]; - res = &comp->resolutions[pi->resno]; - goto LABEL_SKIP; - } else { - pi->first = 0; - } - - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolution[0]) { - continue; - } - res = &comp->resolutions[pi->resno]; - //for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1]); pi->precno++) { - for (pi->precno = 0; pi->precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); pi->precno++) { - index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; - if (!pi->include[index]) { - pi->include[index] = 1; - return true; - } -LABEL_SKIP:; - } - } - } - } - - return false; -} - -static bool pi_next_rpcl(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; - - if (!pi->first) { - goto LABEL_SKIP; - } else { - int compno, resno; - pi->first = 0; - pi->dx = 0; - pi->dy = 0; - for (compno = 0; compno < pi->numcomps; compno++) { - comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolution[0]; resno++) { - int dx, dy,dz; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno)); - dz = comp->dz * (1 << (res->pdz + comp->numresolution[2] - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - pi->dz = !pi->dz ? dz : int_min(pi->dz, dz); - } - } - } - - for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) { - for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) { - for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - int levelnox, levelnoy, levelnoz; - int trx0, try0, trz0; - int trx1, try1, trz1; - int rpx, rpy, rpz; - int prci, prcj, prck; - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolution[0]) { - continue; - } - res = &comp->resolutions[pi->resno]; - levelnox = comp->numresolution[0] - 1 - pi->resno; - levelnoy = comp->numresolution[1] - 1 - pi->resno; - levelnoz = comp->numresolution[2] - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox); - try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy); - trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz); - trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox); - try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy); - trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz); - rpx = res->pdx + levelnox; - rpy = res->pdy + levelnoy; - rpz = res->pdz + levelnoz; - if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { - continue; - } - if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) { - continue; - } - if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) { - continue; - } - if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue; - - if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue; - - prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) - - int_floordivpow2(trx0, res->pdx); - prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) - - int_floordivpow2(try0, res->pdy); - prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) - - int_floordivpow2(trz0, res->pdz); - pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1]; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; - if (!pi->include[index]) { - pi->include[index] = 1; - return true; - } - LABEL_SKIP:; - } - } - } - } - } - } - - return false; -} - -static bool pi_next_pcrl(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; - - if (!pi->first) { - comp = &pi->comps[pi->compno]; - goto LABEL_SKIP; - } else { - int compno, resno; - pi->first = 0; - pi->dx = 0; - pi->dy = 0; - pi->dz = 0; - for (compno = 0; compno < pi->numcomps; compno++) { - comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolution[0]; resno++) { - int dx, dy, dz; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolution[1] - 1 - resno)); - dz = comp->dz * (1 << (res->pdy + comp->numresolution[2] - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - pi->dz = !pi->dz ? dz : int_min(pi->dz, dz); - } - } - } - -for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) { - for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - comp = &pi->comps[pi->compno]; - for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) { - int levelnox, levelnoy, levelnoz; - int trx0, try0, trz0; - int trx1, try1, trz1; - int rpx, rpy, rpz; - int prci, prcj, prck; - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolution[0]) { - continue; - } - res = &comp->resolutions[pi->resno]; - levelnox = comp->numresolution[0] - 1 - pi->resno; - levelnoy = comp->numresolution[1] - 1 - pi->resno; - levelnoz = comp->numresolution[2] - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox); - try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy); - trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz); - trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox); - try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy); - trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz); - rpx = res->pdx + levelnox; - rpy = res->pdy + levelnoy; - rpz = res->pdz + levelnoz; - if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { - continue; - } - if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) { - continue; - } - if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) { - continue; - } - if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue; - - if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue; - - prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) - - int_floordivpow2(trx0, res->pdx); - prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) - - int_floordivpow2(try0, res->pdy); - prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) - - int_floordivpow2(trz0, res->pdz); - pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1]; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; - if (!pi->include[index]) { - pi->include[index] = 1; - return true; - } -LABEL_SKIP:; - } - } - } - } - } -} - - return false; -} - -static bool pi_next_cprl(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; - - if (!pi->first) { - comp = &pi->comps[pi->compno]; - goto LABEL_SKIP; - } else { - pi->first = 0; - } - - for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - int resno; - comp = &pi->comps[pi->compno]; - pi->dx = 0; - pi->dy = 0; - for (resno = 0; resno < comp->numresolution[0]; resno++) { - int dx, dy; - res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolution[0] - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolution[0] - 1 - resno)); - pi->dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi->dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - for (pi->z = pi->tz0; pi->z < pi->tz1; pi->z += pi->dz - (pi->z % pi->dz)) { - for (pi->y = pi->ty0; pi->y < pi->ty1; pi->y += pi->dy - (pi->y % pi->dy)) { - for (pi->x = pi->tx0; pi->x < pi->tx1; pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolution[0]); pi->resno++) { - int levelnox, levelnoy, levelnoz; - int trx0, try0, trz0; - int trx1, try1, trz1; - int rpx, rpy, rpz; - int prci, prcj, prck; - comp = &pi->comps[pi->compno]; - if (pi->resno >= comp->numresolution[0]) { - continue; - } - res = &comp->resolutions[pi->resno]; - levelnox = comp->numresolution[0] - 1 - pi->resno; - levelnoy = comp->numresolution[1] - 1 - pi->resno; - levelnoz = comp->numresolution[2] - 1 - pi->resno; - trx0 = int_ceildiv(pi->tx0, comp->dx << levelnox); - try0 = int_ceildiv(pi->ty0, comp->dy << levelnoy); - trz0 = int_ceildiv(pi->tz0, comp->dz << levelnoz); - trx1 = int_ceildiv(pi->tx1, comp->dx << levelnox); - try1 = int_ceildiv(pi->ty1, comp->dy << levelnoy); - trz1 = int_ceildiv(pi->tz1, comp->dz << levelnoz); - rpx = res->pdx + levelnox; - rpy = res->pdy + levelnoy; - rpz = res->pdz + levelnoz; - if ((!(pi->x % (comp->dx << rpx) == 0) || (pi->x == pi->tx0 && (trx0 << levelnox) % (1 << rpx)))) { - continue; - } - if ((!(pi->y % (comp->dy << rpy) == 0) || (pi->y == pi->ty0 && (try0 << levelnoy) % (1 << rpx)))) { - continue; - } - if ((!(pi->z % (comp->dz << rpz) == 0) || (pi->z == pi->tz0 && (trz0 << levelnoz) % (1 << rpx)))) { - continue; - } - if ((res->prctno[0]==0)||(res->prctno[1]==0)||(res->prctno[2]==0)) continue; - - if ((trx0==trx1)||(try0==try1)||(trz0==trz1)) continue; - - prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelnox), res->pdx) - - int_floordivpow2(trx0, res->pdx); - prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelnoy), res->pdy) - - int_floordivpow2(try0, res->pdy); - prck = int_floordivpow2(int_ceildiv(pi->z, comp->dz << levelnoz), res->pdz) - - int_floordivpow2(trz0, res->pdz); - pi->precno = prci + prcj * res->prctno[0] + prck * res->prctno[0] * res->prctno[1]; - for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) { - index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; - if (!pi->include[index]) { - pi->include[index] = 1; - return true; - } - LABEL_SKIP:; - } - } - } - } - } - } - - return false; -} - -/* -========================================================== - Packet iterator interface -========================================================== -*/ - -opj_pi_iterator_t *pi_create(opj_volume_t *volume, opj_cp_t *cp, int tileno) { - int p, q, r; - int compno, resno, pino; - opj_pi_iterator_t *pi = NULL; - opj_tcp_t *tcp = NULL; - opj_tccp_t *tccp = NULL; - size_t array_size; - - tcp = &cp->tcps[tileno]; - - array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t); - pi = (opj_pi_iterator_t *) opj_malloc(array_size); - if(!pi) { - fprintf(stdout,"[ERROR] Malloc of opj_pi_iterator failed \n"); - return NULL; - } - - for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ - int maxres = 0; - int maxprec = 0; - p = tileno % cp->tw; - q = tileno / cp->tw; - r = tileno / (cp->tw * cp->th); - - pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, volume->x0); - pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, volume->y0); - pi[pino].tz0 = int_max(cp->tz0 + r * cp->tdz, volume->z0); - pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1); - pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1); - pi[pino].tz1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1); - pi[pino].numcomps = volume->numcomps; - - array_size = volume->numcomps * sizeof(opj_pi_comp_t); - pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size); - if(!pi[pino].comps) { - fprintf(stdout,"[ERROR] Malloc of opj_pi_comp failed \n"); - pi_destroy(pi, cp, tileno); - return NULL; - } - memset(pi[pino].comps, 0, array_size); - - for (compno = 0; compno < pi->numcomps; compno++) { - int tcx0, tcx1, tcy0, tcy1, tcz0, tcz1; - int i; - opj_pi_comp_t *comp = &pi[pino].comps[compno]; - tccp = &tcp->tccps[compno]; - - comp->dx = volume->comps[compno].dx; - comp->dy = volume->comps[compno].dy; - comp->dz = volume->comps[compno].dz; - for (i = 0; i < 3; i++) { - comp->numresolution[i] = tccp->numresolution[i]; - if (comp->numresolution[i] > maxres) { - maxres = comp->numresolution[i]; - } - } - array_size = comp->numresolution[0] * sizeof(opj_pi_resolution_t); - comp->resolutions = (opj_pi_resolution_t *) opj_malloc(array_size); - if(!comp->resolutions) { - fprintf(stdout,"[ERROR] Malloc of opj_pi_resolution failed \n"); - pi_destroy(pi, cp, tileno); - return NULL; - } - - tcx0 = int_ceildiv(pi->tx0, comp->dx); - tcy0 = int_ceildiv(pi->ty0, comp->dy); - tcz0 = int_ceildiv(pi->tz0, comp->dz); - tcx1 = int_ceildiv(pi->tx1, comp->dx); - tcy1 = int_ceildiv(pi->ty1, comp->dy); - tcz1 = int_ceildiv(pi->tz1, comp->dz); - - for (resno = 0; resno < comp->numresolution[0]; resno++) { - int levelnox, levelnoy, levelnoz, diff; - int rx0, ry0, rz0, rx1, ry1, rz1; - int px0, py0, pz0, px1, py1, pz1; - opj_pi_resolution_t *res = &comp->resolutions[resno]; - if (tccp->csty & J3D_CCP_CSTY_PRT) { - res->pdx = tccp->prctsiz[0][resno]; - res->pdy = tccp->prctsiz[1][resno]; - res->pdz = tccp->prctsiz[2][resno]; - } else { - res->pdx = 15; - res->pdy = 15; - res->pdz = 15; - } - levelnox = comp->numresolution[0] - 1 - resno; - levelnoy = comp->numresolution[1] - 1 - resno; - levelnoz = comp->numresolution[2] - 1 - resno; - if (levelnoz < 0) levelnoz = 0; - diff = comp->numresolution[0] - comp->numresolution[2]; - - rx0 = int_ceildivpow2(tcx0, levelnox); - ry0 = int_ceildivpow2(tcy0, levelnoy); - rz0 = int_ceildivpow2(tcz0, levelnoz); - rx1 = int_ceildivpow2(tcx1, levelnox); - ry1 = int_ceildivpow2(tcy1, levelnoy); - rz1 = int_ceildivpow2(tcz1, levelnoz); - px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; - py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; - pz0 = int_floordivpow2(rz0, res->pdz) << res->pdz; - px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; - py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; - pz1 = int_ceildivpow2(rz1, res->pdz) << res->pdz; - res->prctno[0] = (rx0==rx1)? 0 : ((px1 - px0) >> res->pdx); - res->prctno[1] = (ry0==ry1)? 0 : ((py1 - py0) >> res->pdy); - res->prctno[2] = (rz0==rz1)? 0 : ((pz1 - pz0) >> res->pdz); - - if (res->prctno[0]*res->prctno[1]*res->prctno[2] > maxprec) { - maxprec = res->prctno[0]*res->prctno[1]*res->prctno[2]; - } - } - } - - tccp = &tcp->tccps[0]; - pi[pino].step_p = 1; - pi[pino].step_c = maxprec * pi[pino].step_p; - pi[pino].step_r = volume->numcomps * pi[pino].step_c; - pi[pino].step_l = maxres * pi[pino].step_r; - - if (pino == 0) { - array_size = volume->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int); - pi[pino].include = (short int *) opj_malloc(array_size); - if(!pi[pino].include) { - fprintf(stdout,"[ERROR] Malloc of pi[pino].include failed \n"); - pi_destroy(pi, cp, tileno); - return NULL; - } - } - else { - pi[pino].include = pi[pino - 1].include; - } - - if (tcp->POC == 0) { - pi[pino].first = 1; - pi[pino].poc.resno0 = 0; - pi[pino].poc.compno0 = 0; - pi[pino].poc.layno1 = tcp->numlayers; - pi[pino].poc.resno1 = maxres; - pi[pino].poc.compno1 = volume->numcomps; - pi[pino].poc.prg = tcp->prg; - } else { - pi[pino].first = 1; - pi[pino].poc.resno0 = tcp->pocs[pino].resno0; - pi[pino].poc.compno0 = tcp->pocs[pino].compno0; - pi[pino].poc.layno1 = tcp->pocs[pino].layno1; - pi[pino].poc.resno1 = tcp->pocs[pino].resno1; - pi[pino].poc.compno1 = tcp->pocs[pino].compno1; - pi[pino].poc.prg = tcp->pocs[pino].prg; - } - } - - return pi; -} - -void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) { - int compno, pino; - opj_tcp_t *tcp = &cp->tcps[tileno]; - if(pi) { - for (pino = 0; pino < tcp->numpocs + 1; pino++) { - if(pi[pino].comps) { - for (compno = 0; compno < pi->numcomps; compno++) { - opj_pi_comp_t *comp = &pi[pino].comps[compno]; - if(comp->resolutions) { - opj_free(comp->resolutions); - } - } - opj_free(pi[pino].comps); - } - } - if(pi->include) { - opj_free(pi->include); - } - opj_free(pi); - } -} - -bool pi_next(opj_pi_iterator_t * pi) { - switch (pi->poc.prg) { - case LRCP: - return pi_next_lrcp(pi); - case RLCP: - return pi_next_rlcp(pi); - case RPCL: - return pi_next_rpcl(pi); - case PCRL: - return pi_next_pcrl(pi); - case CPRL: - return pi_next_cprl(pi); - } - - return false; -} - diff --git a/jp3d/libjp3dvm/pi.h b/jp3d/libjp3dvm/pi.h deleted file mode 100755 index 1d3340be..00000000 --- a/jp3d/libjp3dvm/pi.h +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __PI_H -#define __PI_H -/** -@file pi.h -@brief Implementation of a packet iterator (PI) - -The functions in PI.C have for goal to realize a packet iterator that permits to get the next -packet following the progression order and change of it. The functions in PI.C are used -by some function in T2.C. -*/ - -/** @defgroup PI PI - Implementation of a packet iterator */ -/*@{*/ - -/** -Packet iterator : resolution level information -*/ -typedef struct opj_pi_resolution { -/** Size of precints in horizontal axis */ - int pdx; -/** Size of precints in vertical axis */ - int pdy; -/** Size of precints in axial axis */ - int pdz; -/** Number of precints in each axis */ - int prctno[3]; -} opj_pi_resolution_t; - -/** -Packet iterator : component information -*/ -typedef struct opj_pi_comp { -/** Size in horizontal axis */ - int dx; -/** Size in vertical axis */ - int dy; -/** Size in axial axis */ - int dz; -/** Number of resolution levels */ - int numresolution[3]; -/** Packet iterator : resolution level information */ - opj_pi_resolution_t *resolutions; -} opj_pi_comp_t; - -/** -Packet iterator -*/ -typedef struct opj_pi_iterator { -/** precise if the packet has been already used (usefull for progression order change) */ - short int *include; -/** layer step used to localize the packet in the include vector */ - int step_l; -/** resolution step used to localize the packet in the include vector */ - int step_r; -/** component step used to localize the packet in the include vector */ - int step_c; -/** precinct step used to localize the packet in the include vector */ - int step_p; -/** component that identify the packet */ - int compno; -/** resolution that identify the packet */ - int resno; -/** precinct that identify the packet */ - int precno; -/** layer that identify the packet */ - int layno; -/** 0 if the first packet */ - int first; -/** progression order change information */ - opj_poc_t poc; -/** Packet iterator : component information */ -opj_pi_comp_t *comps; - - int numcomps; - int tx0, ty0, tz0; - int tx1, ty1, tz1; - int x, y, z; - int dx, dy, dz; -} opj_pi_iterator_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a packet iterator -@param volume Raw volume for which the packets will be listed -@param cp Coding parameters -@param tileno Number that identifies the tile for which to list the packets -@return Returns a packet iterator that points to the first packet of the tile -@see pi_destroy -*/ -opj_pi_iterator_t *pi_create(opj_volume_t * volume, opj_cp_t * cp, int tileno); - -/** -Destroy a packet iterator -@param pi Previously created packet iterator -@param cp Coding parameters -@param tileno Number that identifies the tile for which the packets were listed -@see pi_create -*/ -void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno); - -/** -Modify the packet iterator to point to the next packet -@param pi Packet iterator to modify -@return Returns false if pi pointed to the last packet or else returns true -*/ -bool pi_next(opj_pi_iterator_t * pi); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __PI_H */ diff --git a/jp3d/libjp3dvm/raw.c b/jp3d/libjp3dvm/raw.c deleted file mode 100755 index 59d7d35b..00000000 --- a/jp3d/libjp3dvm/raw.c +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/* -========================================================== - local functions -========================================================== -*/ - - -/* -========================================================== - RAW encoding interface -========================================================== -*/ - -opj_raw_t* raw_create() { - opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t)); - return raw; -} - -void raw_destroy(opj_raw_t *raw) { - if(raw) { - opj_free(raw); - } -} - -int raw_numbytes(opj_raw_t *raw) { - return raw->bp - raw->start; -} - -void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len) { - raw->start = bp; - raw->lenmax = len; - raw->len = 0; - raw->c = 0; - raw->ct = 0; -} - -int raw_decode(opj_raw_t *raw) { - int d; - if (raw->ct == 0) { - raw->ct = 8; - if (raw->len == raw->lenmax) { - raw->c = 0xff; - } else { - if (raw->c == 0xff) { - raw->ct = 7; - } - raw->c = *(raw->start + raw->len); - raw->len++; - } - } - raw->ct--; - d = (raw->c >> raw->ct) & 0x01; - - return d; -} - diff --git a/jp3d/libjp3dvm/raw.h b/jp3d/libjp3dvm/raw.h deleted file mode 100755 index e17070ab..00000000 --- a/jp3d/libjp3dvm/raw.h +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __RAW_H -#define __RAW_H -/** -@file raw.h -@brief Implementation of operations for raw encoding (RAW) - -The functions in RAW.C have for goal to realize the operation of raw encoding linked -with the corresponding mode switch. -*/ - -/** @defgroup RAW RAW - Implementation of operations for raw encoding */ -/*@{*/ - -/** -RAW encoding operations -*/ -typedef struct opj_raw { -/** Temporary buffer where bits are coded or decoded */ - unsigned char c; -/** Number of bits already read or free to write */ - unsigned int ct; -/** Maximum length to decode */ - unsigned int lenmax; -/** Length decoded */ - unsigned int len; -/** Pointer to the current position in the buffer */ - unsigned char *bp; -/** Pointer to the start of the buffer */ - unsigned char *start; -/** Pointer to the end of the buffer */ - unsigned char *end; -} opj_raw_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a new RAW handle -@return Returns a new RAW handle if successful, returns NULL otherwise -*/ -opj_raw_t* raw_create(); -/** -Destroy a previously created RAW handle -@param raw RAW handle to destroy -*/ -void raw_destroy(opj_raw_t *raw); -/** -Return the number of bytes written/read since initialisation -@param raw RAW handle to destroy -@return Returns the number of bytes already encoded -*/ -int raw_numbytes(opj_raw_t *raw); -/** -Initialize the decoder -@param raw RAW handle -@param bp Pointer to the start of the buffer from which the bytes will be read -@param len Length of the input buffer -*/ -void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len); -/** -Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN -@param raw RAW handle -@return Returns the decoded symbol (0 or 1) -*/ -int raw_decode(opj_raw_t *raw); -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __RAW_H */ diff --git a/jp3d/libjp3dvm/t1.c b/jp3d/libjp3dvm/t1.c deleted file mode 100755 index 43a78b6a..00000000 --- a/jp3d/libjp3dvm/t1.c +++ /dev/null @@ -1,1181 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup T1 T1 - Implementation of the tier-1 coding */ -/*@{*/ - -/** @name Local static functions */ -/*@{*/ - -static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient); -static int t1_getctxno_sc(opj_t1_t *t1, int f); -static int t1_getctxno_mag(opj_t1_t *t1, int f); -static int t1_getspb(opj_t1_t *t1, int f); -static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos); -static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos); -static void t1_updateflags(int *fp, int s); -/** -Encode significant pass -*/ -static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc); -/** -Decode significant pass -*/ -static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc); -/** -Encode significant pass -*/ -static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty); -/** -Decode significant pass -*/ -static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty); -/** -Encode refinement pass -*/ -static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc); -/** -Decode refinement pass -*/ -static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc); -/** -Encode refinement pass -*/ -static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty); -/** -Decode refinement pass -*/ -static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty); -/** -Encode clean-up pass -*/ -static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc); -/** -Decode clean-up pass -*/ -static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc); -/** -Encode clean-up pass -*/ -static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty); -/** -Decode clean-up pass -*/ -static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty); -/** -Encode 1 code-block -@param t1 T1 handle -@param cblk Code-block coding parameters -@param orient -@param compno Component number -@param level -@param dwtid -@param stepsize -@param cblksty Code-block style -@param numcomps -@param tile -*/ -static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile); -/** -Decode 1 code-block -@param t1 T1 handle -@param cblk Code-block coding parameters -@param orient -@param roishift Region of interest shifting value -@param cblksty Code-block style -*/ -static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty); - -static int t1_init_ctxno_zc(int f, int orient); -static int t1_init_ctxno_sc(int f); -static int t1_init_ctxno_mag(int f); -static int t1_init_spb(int f); -/** -Initialize the look-up tables of the Tier-1 coder/decoder -@param t1 T1 handle -*/ -static void t1_init_luts(opj_t1_t *t1); - -/*@}*/ - -/*@}*/ - -/* ----------------------------------------------------------------------- */ - -static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) { - return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)]; -} - -static int t1_getctxno_sc(opj_t1_t *t1, int f) { - return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; -} - -static int t1_getctxno_mag(opj_t1_t *t1, int f) { - return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)]; -} - -static int t1_getspb(opj_t1_t *t1, int f) { - return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; -} - -static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) { - if (bitpos > T1_NMSEDEC_FRACBITS) { - return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; - } - - return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; -} - -static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) { - if (bitpos > T1_NMSEDEC_FRACBITS) { - return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; - } - - return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; -} - -static void t1_updateflags(int *fp, int s) { - int *np = fp - (T1_MAXCBLKW + 2); - int *sp = fp + (T1_MAXCBLKW + 2); - np[-1] |= T1_SIG_SE; - np[1] |= T1_SIG_SW; - sp[-1] |= T1_SIG_NE; - sp[1] |= T1_SIG_NW; - *np |= T1_SIG_S; - *sp |= T1_SIG_N; - fp[-1] |= T1_SIG_E; - fp[1] |= T1_SIG_W; - if (s) { - *np |= T1_SGN_S; - *sp |= T1_SGN_N; - fp[-1] |= T1_SGN_E; - fp[1] |= T1_SGN_W; - } -} - -static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) { - int v, flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp); - if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) { - v = int_abs(*dp) & one ? 1 : 0; - if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ - mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */ - mqc_bypass_enc(mqc, v); - } else { - mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); - mqc_encode(mqc, v); - } - if (v) { - v = *dp < 0 ? 1 : 0; - *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS); - if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ - mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */ - mqc_bypass_enc(mqc, v); - } else { - mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); - mqc_encode(mqc, v ^ t1_getspb(t1, flag)); - } - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - *fp |= T1_VISIT; - } -} - -static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) { - int v, flag; - - opj_raw_t *raw = t1->raw; /* RAW component */ - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp); - if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) { - if (type == T1_TYPE_RAW) { - if (raw_decode(raw)) { - v = raw_decode(raw); /* ESSAI */ - *dp = v ? -oneplushalf : oneplushalf; - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - } else { - mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); - if (mqc_decode(mqc)) { - mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); - v = mqc_decode(mqc) ^ t1_getspb(t1, flag); - *dp = v ? -oneplushalf : oneplushalf; - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - } - *fp |= T1_VISIT; - } -} /* VSC and BYPASS by Antonin */ - -static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) { - int i, j, k, m, one, vsc; - *nmsedec = 0; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc); - } - } - } - } -} - -static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) { - int i, j, k, m, one, half, oneplushalf, vsc; - one = 1 << bpno; - half = one >> 1; - oneplushalf = one | half; - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc); - } - } - } - } -} /* VSC and BYPASS by Antonin */ - -static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) { - int v, flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp); - if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) { - *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS); - v = int_abs(*dp) & one ? 1 : 0; - if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ - mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */ - mqc_bypass_enc(mqc, v); - } else { - mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); - mqc_encode(mqc, v); - } - *fp |= T1_REFINE; - } -} - -static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) { - int v, t, flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - opj_raw_t *raw = t1->raw; /* RAW component */ - - flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp); - if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) { - if (type == T1_TYPE_RAW) { - mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */ - v = raw_decode(raw); - } else { - mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); - v = mqc_decode(mqc); - } - t = v ? poshalf : neghalf; - *dp += *dp < 0 ? -t : t; - *fp |= T1_REFINE; - } -} /* VSC and BYPASS by Antonin */ - -static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) { - int i, j, k, m, one, vsc; - *nmsedec = 0; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc); - } - } - } - } -} - -static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty) { - int i, j, k, m, one, poshalf, neghalf; - int vsc; - one = 1 << bpno; - poshalf = one >> 1; - neghalf = bpno > 0 ? -poshalf : -1; - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc); - } - } - } - } -} /* VSC and BYPASS by Antonin */ - -static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) { - int v, flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp); - if (partial) { - goto LABEL_PARTIAL; - } - if (!(*fp & (T1_SIG | T1_VISIT))) { - mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); - v = int_abs(*dp) & one ? 1 : 0; - mqc_encode(mqc, v); - if (v) { -LABEL_PARTIAL: - *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS); - mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); - v = *dp < 0 ? 1 : 0; - mqc_encode(mqc, v ^ t1_getspb(t1, flag)); - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - } - *fp &= ~T1_VISIT; -} - -static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) { - int v, flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp); - if (partial) { - goto LABEL_PARTIAL; - } - if (!(flag & (T1_SIG | T1_VISIT))) { - mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); - if (mqc_decode(mqc)) { -LABEL_PARTIAL: - mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); - v = mqc_decode(mqc) ^ t1_getspb(t1, flag); - *dp = v ? -oneplushalf : oneplushalf; - t1_updateflags(fp, v); - *fp |= T1_SIG; - } - } - *fp &= ~T1_VISIT; -} /* VSC and BYPASS by Antonin */ - -static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) { - int i, j, k, m, one, agg, runlen, vsc; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - *nmsedec = 0; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - if (k + 3 < h) { - if (cblksty & J3D_CCP_CBLKSTY_VSC) { - agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || (t1->flags[1 + m][1 + k + 3][1 + i] - & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); - } else { - agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)); - } - } else { - agg = 0; - } - if (agg) { - for (runlen = 0; runlen < 4; runlen++) { - if (int_abs(t1->data[m][k + runlen][i]) & one) - break; - } - mqc_setcurctx(mqc, T1_CTXNO_AGG); - mqc_encode(mqc, runlen != 4); - if (runlen == 4) { - continue; - } - mqc_setcurctx(mqc, T1_CTXNO_UNI); - mqc_encode(mqc, runlen >> 1); - mqc_encode(mqc, runlen & 1); - } else { - runlen = 0; - } - for (j = k + runlen; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_enc_clnpass_step(t1, &(t1->flags[1 + m][1 + j][1 + i]), &(t1->data[m][j][i]), orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc); - } - } - } - } -} - -static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) { - int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc; - int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - one = 1 << bpno; - half = one >> 1; - oneplushalf = one | half; - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - if (k + 3 < h) { - if (cblksty & J3D_CCP_CBLKSTY_VSC) { - agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || (t1->flags[1 + m][1 + k + 3][1 + i] - & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); - } else { - agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH) - || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)); - } - } else { - agg = 0; - } - if (agg) { - mqc_setcurctx(mqc, T1_CTXNO_AGG); - if (!mqc_decode(mqc)) { - continue; - } - mqc_setcurctx(mqc, T1_CTXNO_UNI); - runlen = mqc_decode(mqc); - runlen = (runlen << 1) | mqc_decode(mqc); - } else { - runlen = 0; - } - for (j = k + runlen; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc); - } - } - } - } - if (segsym) { - int v = 0; - mqc_setcurctx(mqc, T1_CTXNO_UNI); - v = mqc_decode(mqc); - v = (v << 1) | mqc_decode(mqc); - v = (v << 1) | mqc_decode(mqc); - v = (v << 1) | mqc_decode(mqc); - /* - if (v!=0xa) { - opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); - } - */ - } -} /* VSC and BYPASS by Antonin */ - - -static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) { - int i, j, k; - int w, h, l; - int passno; - int bpno, passtype; - int max; - int nmsedec = 0; - double cumwmsedec = 0; - char type = T1_TYPE_MQ; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - w = cblk->x1 - cblk->x0; - h = cblk->y1 - cblk->y0; - l = cblk->z1 - cblk->z0; - - max = 0; - for (k = 0; k < l; k++) { - for (j = 0; j < h; j++) { - for (i = 0; i < w; i++) { - max = int_max(max, int_abs(t1->data[k][j][i])); - } - } - } - for (k = 0; k <= l; k++) { - for (j = 0; j <= h; j++) { - for (i = 0; i <= w; i++) { - t1->flags[k][j][i] = 0; - } - } - } - - cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0; - - bpno = cblk->numbps - 1; - passtype = 2; - - mqc_reset_enc(mqc); - mqc_init_enc(mqc, cblk->data); - - for (passno = 0; bpno >= 0; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - int correction = 3; - double tmpwmsedec; - type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - //fprintf(stdout,"passno %d passtype %d w %d h %d l %d bpno %d orient %d type %d cblksty %d\n",passno,passtype,w,h,l,bpno,orient,type,cblksty); - - switch (passtype) { - case 0: - t1_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty); - break; - case 1: - t1_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty); - break; - case 2: - //fprintf(stdout,"w %d h %d l %d bpno %d orient %d \n",w,h,l,bpno,orient); - t1_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty); - /* code switch SEGMARK (i.e. SEGSYM) */ - if (cblksty & J3D_CCP_CBLKSTY_SEGSYM) - mqc_segmark_enc(mqc); - break; - } - - /* fixed_quality */ - tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid); - cumwmsedec += tmpwmsedec; - tile->distotile += tmpwmsedec; - - /* Code switch "RESTART" (i.e. TERMALL) */ - if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) { - if (type == T1_TYPE_RAW) { - mqc_flush(mqc); - correction = 1; - /* correction = mqc_bypass_flush_enc(); */ - } else { /* correction = mqc_restart_enc(); */ - mqc_flush(mqc); - correction = 1; - } - pass->term = 1; - } else { - if (((bpno < (cblk->numbps - 4) && (passtype > 0)) - || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) { - if (type == T1_TYPE_RAW) { - mqc_flush(mqc); - correction = 1; - /* correction = mqc_bypass_flush_enc(); */ - } else { /* correction = mqc_restart_enc(); */ - mqc_flush(mqc); - correction = 1; - } - pass->term = 1; - } else { - pass->term = 0; - } - } - - if (++passtype == 3) { - passtype = 0; - bpno--; - } - - if (pass->term && bpno > 0) { - type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - if (type == T1_TYPE_RAW) - mqc_bypass_init_enc(mqc); - else - mqc_restart_init_enc(mqc); - } - - pass->distortiondec = cumwmsedec; - pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */ - pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate); - - /* Code-switch "RESET" */ - if (cblksty & J3D_CCP_CBLKSTY_RESET) - mqc_reset_enc(mqc); - } - - /* Code switch "ERTERM" (i.e. PTERM) */ - if (cblksty & J3D_CCP_CBLKSTY_PTERM) - mqc_erterm_enc(mqc); - else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY)) - mqc_flush(mqc); - - cblk->totalpasses = passno; -} - -static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) { - int i, j, k, w, h, l; - int bpno, passtype; - int segno, passno; - char type = T1_TYPE_MQ; /* BYPASS mode */ - - opj_raw_t *raw = t1->raw; /* RAW component */ - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - w = cblk->x1 - cblk->x0; - h = cblk->y1 - cblk->y0; - l = cblk->z1 - cblk->z0; - - for (k = 0; k < l; k++) { - for (j = 0; j < h; j++) { - for (i = 0; i < w; i++) { - t1->data[k][j][i] = 0; - } - } - } - - for (k = 0; k <= l; k++) { - for (j = 0; j <= h; j++) { - for (i = 0; i <= w; i++) { - t1->flags[k][j][i] = 0; - } - } - } - - bpno = roishift + cblk->numbps - 1; - passtype = 2; - - mqc_reset_enc(mqc); - - for (segno = 0; segno < cblk->numsegs; segno++) { - opj_tcd_seg_t *seg = &cblk->segs[segno]; - - /* BYPASS mode */ - type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - if (type == T1_TYPE_RAW) { - raw_init_dec(raw, seg->data, seg->len); - } else { - mqc_init_dec(mqc, seg->data, seg->len); - } - - for (passno = 0; passno < seg->numpasses; passno++) { - switch (passtype) { - case 0: - t1_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty); - break; - case 1: - t1_dec_refpass(t1, w, h, l, bpno+1, type, cblksty); - break; - case 2: - t1_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty); - break; - } - - if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) { - mqc_reset_enc(mqc); - } - if (++passtype == 3) { - passtype = 0; - bpno--; - } - } - } -} - -static int t1_init_ctxno_zc(int f, int orient) { - int h, v, d, n, t, hv; - n = 0; - h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0); - v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0); - d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0); - - switch (orient) { - case 2: - t = h; - h = v; - v = t; - case 0: - case 1: - if (!h) { - if (!v) { - if (!d) - n = 0; - else if (d == 1) - n = 1; - else - n = 2; - } else if (v == 1) { - n = 3; - } else { - n = 4; - } - } else if (h == 1) { - if (!v) { - if (!d) - n = 5; - else - n = 6; - } else { - n = 7; - } - } else - n = 8; - break; - case 3: - hv = h + v; - if (!d) { - if (!hv) { - n = 0; - } else if (hv == 1) { - n = 1; - } else { - n = 2; - } - } else if (d == 1) { - if (!hv) { - n = 3; - } else if (hv == 1) { - n = 4; - } else { - n = 5; - } - } else if (d == 2) { - if (!hv) { - n = 6; - } else { - n = 7; - } - } else { - n = 8; - } - break; - } - - return (T1_CTXNO_ZC + n); -} - -static int t1_init_ctxno_sc(int f) { - int hc, vc, n; - n = 0; - - hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == - T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W), - 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == - (T1_SIG_E | T1_SGN_E)) + - ((f & (T1_SIG_W | T1_SGN_W)) == - (T1_SIG_W | T1_SGN_W)), 1); - - vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == - T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S), - 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == - (T1_SIG_N | T1_SGN_N)) + - ((f & (T1_SIG_S | T1_SGN_S)) == - (T1_SIG_S | T1_SGN_S)), 1); - - if (hc < 0) { - hc = -hc; - vc = -vc; - } - if (!hc) { - if (vc == -1) - n = 1; - else if (!vc) - n = 0; - else - n = 1; - } else if (hc == 1) { - if (vc == -1) - n = 2; - else if (!vc) - n = 3; - else - n = 4; - } - - return (T1_CTXNO_SC + n); -} - -static int t1_init_ctxno_mag(int f) { - int n; - if (!(f & T1_REFINE)) - n = (f & (T1_SIG_OTH)) ? 1 : 0; - else - n = 2; - - return (T1_CTXNO_MAG + n); -} - -static int t1_init_spb(int f) { - int hc, vc, n; - - hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == - T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W), - 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == - (T1_SIG_E | T1_SGN_E)) + - ((f & (T1_SIG_W | T1_SGN_W)) == - (T1_SIG_W | T1_SGN_W)), 1); - - vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == - T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S), - 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == - (T1_SIG_N | T1_SGN_N)) + - ((f & (T1_SIG_S | T1_SGN_S)) == - (T1_SIG_S | T1_SGN_S)), 1); - - if (!hc && !vc) - n = 0; - else - n = (!(hc > 0 || (!hc && vc > 0))); - - return n; -} - -static void t1_init_luts(opj_t1_t *t1) { - int i, j; - double u, v, t; - for (j = 0; j < 4; j++) { - for (i = 0; i < 256; ++i) { - t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j); - } - } - for (i = 0; i < 256; i++) { - t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4); - } - for (j = 0; j < 2; j++) { - for (i = 0; i < 2048; ++i) { - t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i); - } - } - for (i = 0; i < 256; ++i) { - t1->lut_spb[i] = t1_init_spb(i << 4); - } - /* FIXME FIXME FIXME */ - /* fprintf(stdout,"nmsedec luts:\n"); */ - for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) { - t = i / pow(2, T1_NMSEDEC_FRACBITS); - u = t; - v = t - 1.5; - t1->lut_nmsedec_sig[i] = - int_max(0, - (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - t1->lut_nmsedec_sig0[i] = - int_max(0, - (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - u = t - 1.0; - if (i & (1 << (T1_NMSEDEC_BITS - 1))) { - v = t - 1.5; - } else { - v = t - 0.5; - } - t1->lut_nmsedec_ref[i] = - int_max(0, - (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - t1->lut_nmsedec_ref0[i] = - int_max(0, - (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - } -} - -/* ----------------------------------------------------------------------- */ - -opj_t1_t* t1_create(opj_common_ptr cinfo) { - opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t)); - if(t1) { - t1->cinfo = cinfo; - /* create MQC and RAW handles */ - t1->mqc = mqc_create(); - t1->raw = raw_create(); - /* initialize the look-up tables of the Tier-1 coder/decoder */ - t1_init_luts(t1); - } - return t1; -} - -void t1_destroy(opj_t1_t *t1) { - if(t1) { - /* destroy MQC and RAW handles */ - mqc_destroy(t1->mqc); - raw_destroy(t1->raw); - //opj_free(t1->data); - //opj_free(t1->flags); - opj_free(t1); - } -} - -void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) { - int compno, resno, bandno, precno, cblkno; - int x, y, z, i, j, k, orient; - int n=0; - int level[3]; - FILE *fid = NULL; -// char filename[10]; - tile->distotile = 0; /* fixed_quality */ - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - /* Weighted first order entropy - sprintf(filename,"res%d.txt",resno); - if ((fid = fopen(filename,"w")) == 0){ - fprintf(stdout,"Error while opening %s\n", filename); - exit(1); - } - */ - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - - //fprintf(stdout,"Precno %d Cblkno %d \n",precno,cblkno); - if (band->bandno == 0) { - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 1) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 2) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 3) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 4) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 5) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 6) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 7) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } - - if (tcp->tccps[compno].reversible == 1) { - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - t1->data[k][j][i] = - tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS; -//fprintf(fid," %d",t1->data[k][j][i]); - } - } - } - } else if (tcp->tccps[compno].reversible == 0) { - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - t1->data[k][j][i] = fix_mul( - tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)], - 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS); - } - } - } - } - - orient = band->bandno; /* FIXME */ - if (orient == 2) { - orient = 1; - } else if (orient == 1) { - orient = 2; - } - for (i = 0; i < 3; i++) - level[i] = tilec->numresolution[i] - 1 - resno; - //fprintf(stdout,"t1_encode_cblk(t1, cblk, %d, %d, %d %d %d, %d, %f, %d, %d, tile);\n", orient, compno, level[0], level[1], level[2], tcp->tccps[compno].reversible, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps); - t1_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile); - - } /* cblkno */ - } /* precno */ -//fprintf(fid,"\n"); - } /* bandno */ -//fclose(fid); - } /* resno */ - } /* compno */ -} - -void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) { - int compno, resno, bandno, precno, cblkno; - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - int x, y, k, i, j, z, orient; - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - - orient = band->bandno; /* FIXME */ - if (orient == 2) { - orient = 1; - } else if (orient == 1) { - orient = 2; - } - - t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty); - - if (band->bandno == 0) { - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 1) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 2) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 3) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 4) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 5) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 6) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 7) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } - - if (tcp->tccps[compno].roishift) { - int thresh, val, mag; - thresh = 1 << tcp->tccps[compno].roishift; - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - val = t1->data[k][j][i]; - mag = int_abs(val); - if (mag >= thresh) { - mag >>= tcp->tccps[compno].roishift; - t1->data[k][j][i] = val < 0 ? -mag : mag; - } - } - } - } - } - - if (tcp->tccps[compno].reversible == 1) { - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - int tmp = t1->data[k][j][i]; - tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2; - } - } - } - } else { /* if (tcp->tccps[compno].reversible == 0) */ - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0); - if (t1->data[k][j][i] >> 1 == 0) { - tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0; - } else { - int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); - tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2); - } - } - } - } - } - } /* cblkno */ - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ -} - - -/** mod fixed_quality */ -double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]) { - double w1, w2, wmsedec; - - if (dwtid[0] == 1 || dwtid[1] == 1 || dwtid[2] == 1) { - w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1; - } else { - w1 = (numcomps > 1) ? mct_getnorm(compno) : 1; - } - w2 = dwt_getnorm(orient, level, dwtid); - - //fprintf(stdout,"nmsedec %d level %d %d %d orient %d bpno %d stepsize %f \n",nmsedec ,level[0],level[1],level[2],orient,bpno,stepsize); - wmsedec = w1 * w2 * stepsize * (1 << bpno); - wmsedec *= wmsedec * nmsedec / 8192.0; - - return wmsedec; -} -/** mod fixed_quality */ diff --git a/jp3d/libjp3dvm/t1.h b/jp3d/libjp3dvm/t1.h deleted file mode 100755 index ec7160c1..00000000 --- a/jp3d/libjp3dvm/t1.h +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __T1_H -#define __T1_H -/** -@file t1.h -@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1) - -The functions in T1.C have for goal to realize the tier-1 coding operation. The functions -in T1.C are used by some function in TCD.C. -*/ - -/** @defgroup T1 T1 - Implementation of the tier-1 coding */ -/*@{*/ - -/* ----------------------------------------------------------------------- */ -#define T1_NMSEDEC_BITS 7 - -#define T1_MAXCBLKW 256 /*< Maximum size of code-block (width) */ -#define T1_MAXCBLKH 256 /*< Maximum size of code-block (heigth) */ -#define T1_MAXCBLKD 256 /*< Maximum size of code-block (depth) */ -#define T1_MINCBLKW 4 /*< Minimum size of code-block (width) */ -#define T1_MINCBLKH 4 /*< Minimum size of code-block (heigth) */ -#define T1_MINCBLKD 4 /*< Minimum size of code-block (depth) */ -#define T1_MAXWHD 18 -#define T1_CBLKW 256 -#define T1_CBLKH 256 -#define T1_CBLKD 256 - -#define T1_SIG_NE 0x0001 /*< Context orientation : North-East direction */ -#define T1_SIG_SE 0x0002 /*< Context orientation : South-East direction */ -#define T1_SIG_SW 0x0004 /*< Context orientation : South-West direction */ -#define T1_SIG_NW 0x0008 /*< Context orientation : North-West direction */ -#define T1_SIG_N 0x0010 /*< Context orientation : North direction */ -#define T1_SIG_E 0x0020 /*< Context orientation : East direction */ -#define T1_SIG_S 0x0040 /*< Context orientation : South direction */ -#define T1_SIG_W 0x0080 /*< Context orientation : West direction */ -#define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW) -#define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W) - -#define T1_SGN_N 0x0100 -#define T1_SGN_E 0x0200 -#define T1_SGN_S 0x0400 -#define T1_SGN_W 0x0800 -#define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W) - -#define T1_SIG 0x1000 -#define T1_REFINE 0x2000 -#define T1_VISIT 0x4000 - -#define T1_NUMCTXS_AGG 1 -#define T1_NUMCTXS_ZC 9 -#define T1_NUMCTXS_MAG 3 -#define T1_NUMCTXS_SC 5 -#define T1_NUMCTXS_UNI 1 - -#define T1_CTXNO_AGG 0 -#define T1_CTXNO_ZC (T1_CTXNO_AGG+T1_NUMCTXS_AGG) -#define T1_CTXNO_MAG (T1_CTXNO_ZC+T1_NUMCTXS_ZC) -#define T1_CTXNO_SC (T1_CTXNO_MAG+T1_NUMCTXS_MAG) -#define T1_CTXNO_UNI (T1_CTXNO_SC+T1_NUMCTXS_SC) -#define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI) - -#define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1) - -#define T1_TYPE_MQ 0 /*< Normal coding using entropy coder */ -#define T1_TYPE_RAW 1 /*< No encoding the information is store under raw format in codestream (mode switch RAW)*/ - -/* ----------------------------------------------------------------------- */ - -/** -Tier-1 coding (coding of code-block coefficients) -*/ -typedef struct opj_t1 { - /** codec context */ - opj_common_ptr cinfo; - - /** MQC component */ - opj_mqc_t *mqc; - /** RAW component */ - opj_raw_t *raw; - /** LUTs for context-based coding */ - int lut_ctxno_zc[1024]; - int lut_ctxno_sc[256]; - int lut_ctxno_mag[4096]; - int lut_spb[256]; - /** LUTs for decoding normalised MSE */ - int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS]; - int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS]; - int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS]; - int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS]; - /** Codeblock data */ - int data[T1_CBLKD][T1_CBLKH][T1_CBLKW];//int ***data; - /** Context information for each voxel in codeblock */ - int flags[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2];//int ***flags; -} opj_t1_t; - -/** @name Exported functions */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a new T1 handle -and initialize the look-up tables of the Tier-1 coder/decoder -@return Returns a new T1 handle if successful, returns NULL otherwise -@see t1_init_luts -*/ -opj_t1_t* t1_create(opj_common_ptr cinfo); -/** -Destroy a previously created T1 handle -@param t1 T1 handle to destroy -*/ -void t1_destroy(opj_t1_t *t1); -/** -Encode the code-blocks of a tile -@param t1 T1 handle -@param tile The tile to encode -@param tcp Tile coding parameters -*/ -void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); -/** -Decode the code-blocks of a tile -@param t1 T1 handle -@param tile The tile to decode -@param tcp Tile coding parameters -*/ -void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); -/** -Get weigths of MSE decoding -@param nmsedec The normalized MSE reduction -@param compno -@param level -@param orient -@param bpno -@param stepsize -@param numcomps -@param dwtid -returns MSE associated to decoding pass -*/ -double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]); - -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __T1_H */ diff --git a/jp3d/libjp3dvm/t1_3d.c b/jp3d/libjp3dvm/t1_3d.c deleted file mode 100755 index efdf2efc..00000000 --- a/jp3d/libjp3dvm/t1_3d.c +++ /dev/null @@ -1,1230 +0,0 @@ -/* - * Copyrigth (c) 2006, Mónica Díez, LPI-UVA, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */ -/*@{*/ - -/** @name Local static functions */ -/*@{*/ - -static int t1_3d_getctxno_zc(unsigned int f, int orient); -static int t1_3d_getctxno_sc(unsigned int f); -static int t1_3d_getctxno_mag(unsigned int f, int fsvr); -static int t1_3d_getspb(unsigned int f); -static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos); -static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos); -static void t1_3d_updateflags(unsigned int *fp, int s); -/** -Encode significant pass -*/ -static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc); -/** -Decode significant pass -*/ -static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc); -/** -Encode significant pass -*/ -static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty); -/** -Decode significant pass -*/ -static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty); -/** -Encode refinement pass -*/ -static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc); -/** -Decode refinement pass -*/ -static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc); -/** -Encode refinement pass -*/ -static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty); -/** -Decode refinement pass -*/ -static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty); -/** -Encode clean-up pass -*/ -static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc); -/** -Decode clean-up pass -*/ -static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc); -/** -Encode clean-up pass -*/ -static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty); -/** -Decode clean-up pass -*/ -static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty); -/** -Encode 1 code-block -@param t1 T1 handle -@param cblk Code-block coding parameters -@param orient -@param compno Component number -@param level[3] -@param dwtid[3] -@param stepsize -@param cblksty Code-block style -@param numcomps -@param tile -*/ -static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile); -/** -Decode 1 code-block -@param t1 T1 handle -@param cblk Code-block coding parameters -@param orient -@param roishift Region of interest shifting value -@param cblksty Code-block style -*/ -static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty); -static int t1_3d_init_ctxno_zc(unsigned int f, int orient); -static int t1_3d_init_ctxno_sc(unsigned int f); -static int t1_3d_init_ctxno_mag(unsigned int f, int f2); -static int t1_3d_init_spb(unsigned int f); -/** -Initialize the look-up tables of the Tier-1 coder/decoder -@param t1 T1 handle -*/ -static void t1_3d_init_luts(opj_t1_3d_t *t1); - -/*@}*/ - -/*@}*/ - -/* ----------------------------------------------------------------------- */ - -static int t1_3d_getctxno_zc(unsigned int f, int orient) { - return t1_3d_init_ctxno_zc((f & T1_3D_SIG_OTH), orient); -} - -static int t1_3d_getctxno_sc(unsigned int f) { - return t1_3d_init_ctxno_sc((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)); - //return t1->lut_ctxno_sc[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4]; -} - -static int t1_3d_getctxno_mag(unsigned int f, int fsvr) { - return t1_3d_init_ctxno_mag((f & T1_3D_SIG_OTH), fsvr); -} - -static int t1_3d_getspb(unsigned int f) { - return t1_3d_init_spb((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)); - //return t1->lut_spb[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4]; -} - -static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos) { - if (bitpos > T1_NMSEDEC_FRACBITS) { - return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; - } - - return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; -} - -static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos) { - if (bitpos > T1_NMSEDEC_FRACBITS) { - return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; - } - - return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; -} - -static void t1_3d_updateflags(unsigned int *fp, int s) { - unsigned int *np = fp - (T1_MAXCBLKW + 2); - unsigned int *sp = fp + (T1_MAXCBLKW + 2); - - unsigned int *bwp = fp + ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2)); - unsigned int *bnp = bwp - (T1_MAXCBLKW + 2); - unsigned int *bsp = bwp + (T1_MAXCBLKW + 2); - - unsigned int *fwp = fp - ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2)); - unsigned int *fnp = fwp - (T1_MAXCBLKW + 2); - unsigned int *fsp = fwp + (T1_MAXCBLKW + 2); - - np[-1] |= T1_3D_SIG_SE; - np[1] |= T1_3D_SIG_SW; - sp[-1] |= T1_3D_SIG_NE; - sp[1] |= T1_3D_SIG_NW; - *np |= T1_3D_SIG_S; - *sp |= T1_3D_SIG_N; - fp[-1] |= T1_3D_SIG_E; - fp[1] |= T1_3D_SIG_W; - - *fwp |= T1_3D_SIG_FC; - *bwp |= T1_3D_SIG_BC; - - fnp[-1] |= T1_3D_SIG_FSE; - fnp[1] |= T1_3D_SIG_FSW; - fsp[-1] |= T1_3D_SIG_FNE; - fsp[1] |= T1_3D_SIG_FNW; - *fnp |= T1_3D_SIG_FS; - *fsp |= T1_3D_SIG_FN; - fwp[-1] |= T1_3D_SIG_FE; - fwp[1] |= T1_3D_SIG_FW; - - bnp[-1] |= T1_3D_SIG_BSE; - bnp[1] |= T1_3D_SIG_BSW; - bsp[-1] |= T1_3D_SIG_BNE; - bsp[1] |= T1_3D_SIG_BNW; - *bnp |= T1_3D_SIG_BS; - *bsp |= T1_3D_SIG_BN; - bwp[-1] |= T1_3D_SIG_BE; - bwp[1] |= T1_3D_SIG_BW; - - if (s) { - *np |= (T1_3D_SGN_S << 16); - *sp |= (T1_3D_SGN_N << 16); - fp[-1] |= (T1_3D_SGN_E << 16); - fp[1] |= (T1_3D_SGN_W << 16); - *fwp |= (T1_3D_SGN_F << 16); - *bwp |= (T1_3D_SGN_B << 16); - } -} - -static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) { - int v, flagsvr; - unsigned int flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp); - flagsvr = (*fsvr); - if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) { - v = int_abs(*dp) & one ? 1 : 0; - if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ - mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); /* ESSAI */ - mqc_bypass_enc(mqc, v); - } else { - mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); - mqc_encode(mqc, v); - } - if (v) { - v = *dp < 0 ? 1 : 0; - *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS); - if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ - mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); /* ESSAI */ - mqc_bypass_enc(mqc, v); - } else { - mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); - mqc_encode(mqc, v ^ t1_3d_getspb(flag)); - } - t1_3d_updateflags(fp, v); - *fsvr |= T1_3D_SIG; - } - *fsvr |= T1_3D_VISIT; - } -} - -static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc) { - int v, flagsvr; - unsigned int flag; - - opj_raw_t *raw = t1->raw; /* RAW component */ - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp); - flagsvr = (*fsvr); - if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) { - if (type == T1_TYPE_RAW) { - if (raw_decode(raw)) { - v = raw_decode(raw); /* ESSAI */ - *dp = v ? -oneplushalf : oneplushalf; - t1_3d_updateflags(fp, v); - *fsvr |= T1_3D_SIG; - } - } else { - mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); - if (mqc_decode(mqc)) { - mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); - v = mqc_decode(mqc) ^ t1_3d_getspb(flag); - *dp = v ? -oneplushalf : oneplushalf; - t1_3d_updateflags(fp, v); - *fsvr |= T1_3D_SIG; - } - } - *fsvr |= T1_3D_VISIT; - } -} /* VSC and BYPASS by Antonin */ - -static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) { - int i, j, k, m, one, vsc; - *nmsedec = 0; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_3d_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc); - } - } - } - } -} - -static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) { - int i, j, k, m, one, half, oneplushalf, vsc; - one = 1 << bpno; - half = one >> 1; - oneplushalf = one | half; - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_3d_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc); - } - } - } - } -} /* VSC and BYPASS by Antonin */ - -static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) { - int v, flagsvr; - unsigned int flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp); - flagsvr = (*fsvr); - if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) { - *nmsedec += t1_3d_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS); - v = int_abs(*dp) & one ? 1 : 0; - if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */ - mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); /* ESSAI */ - mqc_bypass_enc(mqc, v); - } else { - mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); - mqc_encode(mqc, v); - } - *fsvr |= T1_3D_REFINE; - } -} - -static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc) { - int v, t, flagsvr; - unsigned int flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - opj_raw_t *raw = t1->raw; /* RAW component */ - - flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp); - flagsvr = (*fsvr); - if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) { - if (type == T1_TYPE_RAW) { - mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); /* ESSAI */ - v = raw_decode(raw); - } else { - mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); - v = mqc_decode(mqc); - } - t = v ? poshalf : neghalf; - *dp += *dp < 0 ? -t : t; - *fsvr |= T1_3D_REFINE; - } -} /* VSC and BYPASS by Antonin */ - -static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) { - int i, j, k, m, one, vsc; - *nmsedec = 0; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (m = 0; m < l; m++){ - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_3d_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc); - } - } - } - } -} - -static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty) { - int i, j, k, m, one, poshalf, neghalf; - int vsc; - one = 1 << bpno; - poshalf = one >> 1; - neghalf = bpno > 0 ? -poshalf : -1; - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - for (j = k; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_3d_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc); - } - } - } - } -} /* VSC and BYPASS by Antonin */ - -static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) { - int v, flagsvr; - unsigned int flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp); - flagsvr = (*fsvr); - if (partial) { - goto LABEL_PARTIAL; - } - if (!(*fsvr & (T1_3D_SIG | T1_3D_VISIT))) { - mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); - v = int_abs(*dp) & one ? 1 : 0; - mqc_encode(mqc, v); - if (v) { -LABEL_PARTIAL: - *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS); - mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); - v = *dp < 0 ? 1 : 0; - mqc_encode(mqc, v ^ t1_3d_getspb(flag)); - t1_3d_updateflags(fp, v); - *fsvr |= T1_3D_SIG; - } - } - *fsvr &= ~T1_3D_VISIT; -} - -static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc) { - int v, flagsvr; - unsigned int flag; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp); - flagsvr = (*fsvr); - if (partial) { - goto LABEL_PARTIAL; - } - if (!(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) { - mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); - if (mqc_decode(mqc)) { -LABEL_PARTIAL: - mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); - v = mqc_decode(mqc) ^ t1_3d_getspb(flag); - *dp = v ? -oneplushalf : oneplushalf; - t1_3d_updateflags(fp, v); - *fsvr |= T1_3D_SIG; - } - } - *fsvr &= ~T1_3D_VISIT; -} /* VSC and BYPASS by Antonin */ - -static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) { - int i, j, k, m, one, agg, runlen, vsc; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - *nmsedec = 0; - one = 1 << (bpno + T1_NMSEDEC_FRACBITS); - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - if (k + 3 < h) { - if (cblksty & J3D_CCP_CBLKSTY_VSC) { - agg = !( ((t1->flagSVR[1 + m][1 + k][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH))) - ); - } else { - agg = !( - ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH)) - ); - } - } else { - agg = 0; - } - if (agg) { - for (runlen = 0; runlen < 4; runlen++) { - if (int_abs(t1->data[m][k + runlen][i]) & one) - break; - } - mqc_setcurctx(mqc, T1_CTXNO_AGG); - mqc_encode(mqc, runlen != 4); - if (runlen == 4) { - continue; - } - mqc_setcurctx(mqc, T1_CTXNO_UNI); - mqc_encode(mqc, runlen >> 1); - mqc_encode(mqc, runlen & 1); - } else { - runlen = 0; - } - for (j = k + runlen; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_3d_enc_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc); - } - } - } - } -} - -static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) { - int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc; - int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - one = 1 << bpno; - half = one >> 1; - oneplushalf = one | half; - for (m = 0; m < l; m++) { - for (k = 0; k < h; k += 4) { - for (i = 0; i < w; i++) { - if (k + 3 < h) { - if (cblksty & J3D_CCP_CBLKSTY_VSC) { - agg = !( - ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH))) - ); - } else { - agg = !( - ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH)) - || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH)) - ); - } - } else { - agg = 0; - } - if (agg) { - mqc_setcurctx(mqc, T1_CTXNO_AGG); - if (!mqc_decode(mqc)) { - continue; - } - mqc_setcurctx(mqc, T1_CTXNO_UNI); - runlen = mqc_decode(mqc); - runlen = (runlen << 1) | mqc_decode(mqc); - } else { - runlen = 0; - } - for (j = k + runlen; j < k + 4 && j < h; j++) { - vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0; - t1_3d_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc); - } - } - } - } - if (segsym) { - int v = 0; - mqc_setcurctx(mqc, T1_CTXNO_UNI); - v = mqc_decode(mqc); - v = (v << 1) | mqc_decode(mqc); - v = (v << 1) | mqc_decode(mqc); - v = (v << 1) | mqc_decode(mqc); - /* - if (v!=0xa) { - opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); - } - */ - } -} /* VSC and BYPASS by Antonin */ - - -static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) { - int i, j, k; - int w, h, l; - int passno; - int bpno, passtype; - int max; - int nmsedec = 0; - double cumwmsedec = 0; - char type = T1_TYPE_MQ; - - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - w = cblk->x1 - cblk->x0; - h = cblk->y1 - cblk->y0; - l = cblk->z1 - cblk->z0; - - max = 0; - for (k = 0; k < l; k++) { - for (j = 0; j < h; j++) { - for (i = 0; i < w; i++) { - max = int_max(max, int_abs(t1->data[k][j][i])); - } - } - } - for (k = 0; k <= l; k++) { - for (j = 0; j <= h; j++) { - for (i = 0; i <= w; i++) { - t1->flags[k][j][i] = 0; - t1->flagSVR[k][j][i] = 0; - } - } - } - - cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0; - - bpno = cblk->numbps - 1; - passtype = 2; - - mqc_reset_enc(mqc); - mqc_init_enc(mqc, cblk->data); - - for (passno = 0; bpno >= 0; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - int correction = 3; - double tmpwmsedec; - type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - - switch (passtype) { - case 0: - t1_3d_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty); - break; - case 1: - t1_3d_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty); - break; - case 2: - t1_3d_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty); - /* code switch SEGMARK (i.e. SEGSYM) */ - if (cblksty & J3D_CCP_CBLKSTY_SEGSYM) - mqc_segmark_enc(mqc); - break; - } - - /* fixed_quality */ - tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid); - cumwmsedec += tmpwmsedec; - tile->distotile += tmpwmsedec; - - /* Code switch "RESTART" (i.e. TERMALL) */ - if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) { - if (type == T1_TYPE_RAW) { - mqc_flush(mqc); - correction = 1; - /* correction = mqc_bypass_flush_enc(); */ - } else { /* correction = mqc_restart_enc(); */ - mqc_flush(mqc); - correction = 1; - } - pass->term = 1; - } else { - if (((bpno < (cblk->numbps - 4) && (passtype > 0)) - || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) { - if (type == T1_TYPE_RAW) { - mqc_flush(mqc); - correction = 1; - } else { - mqc_flush(mqc); - correction = 1; - } - pass->term = 1; - } else { - pass->term = 0; - } - } - - if (++passtype == 3) { - passtype = 0; - bpno--; - } - - if (pass->term && bpno > 0) { - type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - if (type == T1_TYPE_RAW) - mqc_bypass_init_enc(mqc); - else - mqc_restart_init_enc(mqc); - } - - pass->distortiondec = cumwmsedec; - pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */ - pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate); - - /* Code-switch "RESET" */ - if (cblksty & J3D_CCP_CBLKSTY_RESET) - mqc_reset_enc(mqc); - } - - /* Code switch "ERTERM" (i.e. PTERM) */ - if (cblksty & J3D_CCP_CBLKSTY_PTERM) - mqc_erterm_enc(mqc); - else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY)) - mqc_flush(mqc); - - cblk->totalpasses = passno; -} - -static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) { - int i, j, k; - int w, h, l; - int bpno, passtype; - int segno, passno; - char type = T1_TYPE_MQ; /* BYPASS mode */ - opj_raw_t *raw = t1->raw; /* RAW component */ - opj_mqc_t *mqc = t1->mqc; /* MQC component */ - - w = cblk->x1 - cblk->x0; - h = cblk->y1 - cblk->y0; - l = cblk->z1 - cblk->z0; - - for (k = 0; k < l; k++) { - for (j = 0; j < h; j++) { - for (i = 0; i < w; i++) { - t1->data[k][j][i] = 0; - } - } - } - - for (k = 0; k <= l; k++) { - for (j = 0; j <= h; j++) { - for (i = 0; i <= w; i++) { - t1->flags[k][j][i] = 0; - t1->flagSVR[k][j][i] = 0; - } - } - } - - - bpno = roishift + cblk->numbps - 1; - passtype = 2; - - mqc_reset_enc(mqc); - - for (segno = 0; segno < cblk->numsegs; segno++) { - opj_tcd_seg_t *seg = &cblk->segs[segno]; - - /* BYPASS mode */ - type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; - if (type == T1_TYPE_RAW) { - raw_init_dec(raw, seg->data, seg->len); - } else { - mqc_init_dec(mqc, seg->data, seg->len); - } - - for (passno = 0; passno < seg->numpasses; passno++) { - switch (passtype) { - case 0: - t1_3d_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty); - break; - case 1: - t1_3d_dec_refpass(t1, w, h, l, bpno+1, type, cblksty); - break; - case 2: - t1_3d_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty); - break; - } - - if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) { - mqc_reset_enc(mqc); - } - if (++passtype == 3) { - passtype = 0; - bpno--; - } - - } - } -} - -static int t1_3d_init_ctxno_zc(unsigned int f, int orient) { - unsigned int h, v, c; - unsigned int d2xy, d2xz, d2yz, d3; - int n; - unsigned int hvc, hc, d2, d2xy2yz, d2xy2xz; - n = 0; - h = ((f & T1_3D_SIG_W) != 0) + ((f & T1_3D_SIG_E) != 0); - v = ((f & T1_3D_SIG_N) != 0) + ((f & T1_3D_SIG_S) != 0); - c = ((f & T1_3D_SIG_FC) != 0) + ((f & T1_3D_SIG_BC) != 0); - d2xy = ((f & T1_3D_SIG_NW) != 0) + ((f & T1_3D_SIG_NE) != 0) + ((f & T1_3D_SIG_SE) != 0) + ((f & T1_3D_SIG_SW) != 0); - d2xz = ((f & T1_3D_SIG_FW) != 0) + ((f & T1_3D_SIG_BW) != 0) + ((f & T1_3D_SIG_FE) != 0) + ((f & T1_3D_SIG_BE) != 0); - d2yz = ((f & T1_3D_SIG_FN) != 0) + ((f & T1_3D_SIG_FS) != 0) + ((f & T1_3D_SIG_BN) != 0) + ((f & T1_3D_SIG_BS) != 0); - d3 = ((f & T1_3D_SIG_FNW) != 0) + ((f & T1_3D_SIG_FNE) != 0) + ((f & T1_3D_SIG_FSE) != 0) + ((f & T1_3D_SIG_FSW) != 0) - + ((f & T1_3D_SIG_BNW) != 0) + ((f & T1_3D_SIG_BNE) != 0) + ((f & T1_3D_SIG_BSE) != 0) + ((f & T1_3D_SIG_BSW) != 0); - - switch (orient) { - case 0: //LLL - case 7: //HHH - hvc = h + v + c; - d2 = d2xy + d2xz + d2yz; - if (!hvc) { - if (!d2) { - n = (!d3) ? 0 : 1; - } else if (d2 == 1) { - n = (!d3) ? 2 : 3; - } else { - n = (!d3) ? 4 : 5; - } - } else if (hvc == 1) { - if (!d2) { - n = (!d3) ? 6 : 7; - } else if (d2 == 1) { - n = (!d3) ? 8 : 9; - } else { - n = 10; - } - } else if (hvc == 2) { - if (!d2) { - n = (!d3) ? 11 : 12; - } else { - n = 13; - } - } else if (hvc == 3) { - n = 14; - } else { - n = 15; - } - break; - //LHL, HLL, LLH - case 1: - case 2: - case 4: - hc = h + c; - d2xy2yz = d2xy + d2yz; - if (!hc) { - if (!v) { - if (!d2xy) { - n = (!d2xy2yz) ? ((!d3) ? 0 : 1) : ((!d3) ? 2 : 3); - } else if (d2xy == 1) { - n = (!d2xy2yz) ? ((!d3) ? 4 : 5) : 6; - } else { //>=2 - n = 7; - } - } else { - n = (v == 1) ? 8 : 9; // =1 or =2 - } - } else if (hc == 1) { - n = (!v) ? ( (!d2xy) ? ( (!d2xy2yz) ? ( (!d3) ? 10 : 11) : (12) ) : (13) ) : (14); - } else { //if (hc >= 2) - n = 15; - } - break; - //HLH, HHL, LHH - case 3: - case 5: - case 6: - hc = h + c; - d2xy2xz = d2xy + d2xz; - if (!v) { - if (!d2xz) { - if (!hc && !d2xy2xz) { - n = (!d3) ? 0 : 1; - } else if (hc == 1) { - n = (!d2xy2xz) ? 2 : 3; - } else { //if >= 2 - n = 4; - } - } else if ( d2xz>=1 && !hc ) { - n = 5; - } else if ( hc>=1 ) { - n = (d2xz==1) ? 6 : 7; - } - } else if (v == 1) { - if (!d2xz) { - n = (!hc) ? 8 : 9; - } else if (d2xz == 1) { - n = (!hc) ? 10 : 11; - } else if (d2xz == 2) { - n = (!hc) ? 12 : 13; - } else { // if (d2xz >= 3) { - n = 14; - } - } else if (v == 2) { - n = 15; - } - break; - } - - return (T1_3D_CTXNO_ZC + n); -} - -static int t1_3d_init_ctxno_sc(unsigned int f) { - int hc, vc, cc; - int n = 0; - - hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) - + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) - - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) - + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1); - - vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) - + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) - - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) - + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1); - - cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) - + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) - - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) - + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1); - if (hc < 0) { - hc = -hc; - vc = -vc; - cc = -cc; - } - - if (!hc) { - if (!vc) - n = (!cc) ? 0 : 1; - else if (vc == -1) - n = (!cc) ? 1 : ( (cc>0) ? 2 : 4); - else if (vc == 1) - n = (!cc) ? 1 : ( (cc<0) ? 2 : 4); - } else if (hc == 1) { - if (!vc) - n = (!cc) ? 1 : ( (cc<0) ? 2 : 4); - else if (vc == 1) - n = (!cc) ? 4 : ( (cc>0) ? 5 : 3); - else if (vc == -1) - n = (!cc) ? 2 : 3; - } else if (hc == -1) { - if (!vc) - n = (!cc) ? 1 : ( (cc>0) ? 2 : 4); - else if (vc == 1) - n = (!cc) ? 2 : 3; - else if (vc == -1) - n = (!cc) ? 4 : ( (cc<0) ? 5 : 3); - } - - return (T1_3D_CTXNO_SC + n); -} - -static int t1_3d_init_ctxno_mag(unsigned int f, int f2) { - int n; - if (!(f2 & T1_3D_REFINE)) //First refinement for this coefficient (no previous refinement) - n = (f & (T1_3D_SIG_PRIM)) ? 1 : 0; - else - n = 2; - - return (T1_3D_CTXNO_MAG + n); -} - -static int t1_3d_init_spb(unsigned int f) { - int hc, vc, cc; - int n = 0; - - hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) - + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) - - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) - + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1); - - vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) - + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) - - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) - + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1); - - cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) - + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) - - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) - + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1); - - n = ((hc + vc + cc) < 0); - - return n; -} - -static void t1_3d_init_luts(opj_t1_3d_t *t1) { - int i; - double u, v, t; - /*for (j = 0; j < 4; j++) { - for (i = 0; i < 256; ++i) { - t1->lut_ctxno_zc[(j << 8) | i] = t1_3d_init_ctxno_zc(i, j); - } - } - for (i = 0; i < 4096; i++) { - t1->lut_ctxno_sc[i] = t1_3d_init_ctxno_sc(i << 4); - } - for (j = 0; j < 2; j++) { - for (i = 0; i < 2048; ++i) { - t1->lut_ctxno_mag[(j << 11) + i] = t1_3d_init_ctxno_mag((j ? T1_3D_REFINE : 0) | i); - } - } - for (i = 0; i < 4096; ++i) { - t1->lut_spb[i] = t1_3d_init_spb(i << 4); - }*/ - /* FIXME FIXME FIXME */ - for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) { - t = i / pow(2, T1_NMSEDEC_FRACBITS); - u = t; - v = t - 1.5; - t1->lut_nmsedec_sig[i] = - int_max(0, - (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - t1->lut_nmsedec_sig0[i] = - int_max(0, - (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - u = t - 1.0; - if (i & (1 << (T1_NMSEDEC_BITS - 1))) { - v = t - 1.5; - } else { - v = t - 0.5; - } - t1->lut_nmsedec_ref[i] = - int_max(0, - (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - t1->lut_nmsedec_ref0[i] = - int_max(0, - (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0)); - } -} - -/* ----------------------------------------------------------------------- */ - -opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo) { - opj_t1_3d_t *t1 = (opj_t1_3d_t*)opj_malloc(sizeof(opj_t1_3d_t)); - if(t1) { - t1->cinfo = cinfo; - /* create MQC and RAW handles */ - t1->mqc = mqc_create(); - t1->raw = raw_create(); - /* initialize the look-up tables of the Tier-1 coder/decoder */ - t1_3d_init_luts(t1); - } - return t1; -} - -void t1_3d_destroy(opj_t1_3d_t *t1) { - if(t1) { - /* destroy MQC and RAW handles */ - mqc_destroy(t1->mqc); - raw_destroy(t1->raw); - opj_free(t1); - } -} - -void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) { - int compno, resno, bandno, precno, cblkno; - int x, y, z, i, j, k, orient; - int level[3]; - tile->distotile = 0; /* fixed_quality */ - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - - if (band->bandno == 0) { - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 1) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 2) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 3) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 4) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 5) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 6) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 7) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } - - if (tcp->tccps[compno].reversible == 1) { - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - t1->data[k][j][i] = - tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS; - } - } - } - } else if (tcp->tccps[compno].reversible == 0) { - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - t1->data[k][j][i] = fix_mul( - tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)], - 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS); - } - } - } - } - orient = band->bandno; /* FIXME */ - for (i = 0; i < 3; i++) - level[i] = tilec->numresolution[i] - 1 - resno; - - t1_3d_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile); - - } /* cblkno */ - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ -} - -void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) { - int compno, resno, bandno, precno, cblkno; - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - int x, y, z, i, j, k, orient; - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - - orient = band->bandno; /* FIXME */ - - //fprintf(stdout,"[INFO] t1_3d_decode_cblk(t1, cblk, orient(%d), tcp->tccps[compno].roishift (%d), tcp->tccps[compno].cblksty (%d));\n",orient,tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty); - t1_3d_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty); - - if (band->bandno == 0) { - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 1) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 2) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 3) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = cblk->z0 - band->z0; - } else if (band->bandno == 4) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 5) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 6) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } else if (band->bandno == 7) { - opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; - x = pres->x1 - pres->x0 + cblk->x0 - band->x0; - y = pres->y1 - pres->y0 + cblk->y0 - band->y0; - z = pres->z1 - pres->z0 + cblk->z0 - band->z0; - } - - if (tcp->tccps[compno].roishift) { - int thresh, val, mag; - thresh = 1 << tcp->tccps[compno].roishift; - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - val = t1->data[k][j][i]; - mag = int_abs(val); - if (mag >= thresh) { - mag >>= tcp->tccps[compno].roishift; - t1->data[k][j][i] = val < 0 ? -mag : mag; - } - } - } - } - } - - if (tcp->tccps[compno].reversible == 1) { - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - int tmp = t1->data[k][j][i]; - tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2; - } - } - } - } else { /* if (tcp->tccps[compno].reversible == 0) */ - for (k = 0; k < cblk->z1 - cblk->z0; k++) { - for (j = 0; j < cblk->y1 - cblk->y0; j++) { - for (i = 0; i < cblk->x1 - cblk->x0; i++) { - double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0); - if (t1->data[k][j][i] >> 1 == 0) { - tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0; - } else { - int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); - tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2); - } - } - } - } - } - } /* cblkno */ - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ -} diff --git a/jp3d/libjp3dvm/t1_3d.h b/jp3d/libjp3dvm/t1_3d.h deleted file mode 100755 index dd73763d..00000000 --- a/jp3d/libjp3dvm/t1_3d.h +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyrigth (c) 2006, Mónica Díez, LPI-UVA, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __T1_3D_H -#define __T1_3D_H -/** -@file t1_3d.h -@brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1) - -The functions in T1_3D.C have for goal to realize the tier-1 coding operation of 3D-EBCOT. -The functions in T1_3D.C are used by some function in TCD.C. -*/ - -/** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */ -/*@{*/ - -/* ----------------------------------------------------------------------- */ - -/* Neighbourhood of 3D EBCOT (Significance context)*/ -#define T1_3D_SIG_NE 0x00000001 /*< Context orientation : North-East direction */ -#define T1_3D_SIG_SE 0x00000002 /*< Context orientation : South-East direction */ -#define T1_3D_SIG_SW 0x00000004 /*< Context orientation : South-West direction */ -#define T1_3D_SIG_NW 0x00000008 /* Context orientation : North-West direction */ -#define T1_3D_SIG_N 0x00000010 /*< Context orientation : North direction */ -#define T1_3D_SIG_E 0x00000020 /*< Context orientation : East direction */ -#define T1_3D_SIG_S 0x00000040 /*< Context orientation : South direction */ -#define T1_3D_SIG_W 0x00000080 /*< Context orientation : West direction */ -#define T1_3D_SIG_FC 0x00000100 /*< Context orientation : Forward Central direction */ -#define T1_3D_SIG_BC 0x00000200 /*< Context orientation : Backward Central direction */ -#define T1_3D_SIG_FNE 0x00000400 /*< Context orientation : Forward North-East direction */ -#define T1_3D_SIG_FSE 0x00000800 /*< Context orientation : Forward South-East direction */ -#define T1_3D_SIG_FSW 0x00001000 /*< Context orientation : Forward South-West direction */ -#define T1_3D_SIG_FNW 0x00002000 /*< Context orientation : Forward North-West direction */ -#define T1_3D_SIG_FN 0x00004000 /*< Context orientation : Forward North direction */ -#define T1_3D_SIG_FE 0x00008000 /*< Context orientation : Forward East direction */ -#define T1_3D_SIG_FS 0x00010000 /*< Context orientation : Forward South direction */ -#define T1_3D_SIG_FW 0x00020000 /*< Context orientation : Forward West direction */ -#define T1_3D_SIG_BNE 0x00040000 /*< Context orientation : Backward North-East direction */ -#define T1_3D_SIG_BSE 0x00080000 /*< Context orientation : Backward South-East direction */ -#define T1_3D_SIG_BSW 0x00100000 /*< Context orientation : Backward South-West direction */ -#define T1_3D_SIG_BNW 0x00200000 /*< Context orientation : Backward North-West direction */ -#define T1_3D_SIG_BN 0x00400000 /*< Context orientation : Backward North direction */ -#define T1_3D_SIG_BE 0x00800000 /*< Context orientation : Backward East direction */ -#define T1_3D_SIG_BS 0x01000000 /*< Context orientation : Backward South direction */ -#define T1_3D_SIG_BW 0x02000000 /*< Context orientation : Backward West direction */ -#define T1_3D_SIG_COTH (T1_3D_SIG_N|T1_3D_SIG_NE|T1_3D_SIG_E|T1_3D_SIG_SE|T1_3D_SIG_S|T1_3D_SIG_SW|T1_3D_SIG_W|T1_3D_SIG_NW) -#define T1_3D_SIG_BOTH (T1_3D_SIG_BN|T1_3D_SIG_BNE|T1_3D_SIG_BE|T1_3D_SIG_BSE|T1_3D_SIG_BS|T1_3D_SIG_BSW|T1_3D_SIG_BW|T1_3D_SIG_BNW|T1_3D_SIG_BC) -#define T1_3D_SIG_FOTH (T1_3D_SIG_FN|T1_3D_SIG_FNE|T1_3D_SIG_FE|T1_3D_SIG_FSE|T1_3D_SIG_FS|T1_3D_SIG_FSW|T1_3D_SIG_FW|T1_3D_SIG_FNW|T1_3D_SIG_FC) -#define T1_3D_SIG_OTH (T1_3D_SIG_FOTH|T1_3D_SIG_BOTH|T1_3D_SIG_COTH) -#define T1_3D_SIG_PRIM (T1_3D_SIG_N|T1_3D_SIG_E|T1_3D_SIG_S|T1_3D_SIG_W|T1_3D_SIG_FC|T1_3D_SIG_BC) - -#define T1_3D_SGN_N 0x0400 -#define T1_3D_SGN_E 0x0800 -#define T1_3D_SGN_S 0x1000 -#define T1_3D_SGN_W 0x2000 -#define T1_3D_SGN_F 0x4000 -#define T1_3D_SGN_B 0x8000 -#define T1_3D_SGN (T1_3D_SGN_N|T1_3D_SGN_E|T1_3D_SGN_S|T1_3D_SGN_W|T1_3D_SGN_F|T1_3D_SGN_B) - -#define T1_3D_SIG 0x0001 //Significance state -#define T1_3D_REFINE 0x0002 //Delayed significance -#define T1_3D_VISIT 0x0004 //First-pass membership - -#define T1_3D_NUMCTXS_AGG 1 -#define T1_3D_NUMCTXS_ZC 16 -#define T1_3D_NUMCTXS_MAG 3 -#define T1_3D_NUMCTXS_SC 6 -#define T1_3D_NUMCTXS_UNI 1 - -#define T1_3D_CTXNO_AGG 0 -#define T1_3D_CTXNO_ZC (T1_3D_CTXNO_AGG+T1_3D_NUMCTXS_AGG) //1 -#define T1_3D_CTXNO_MAG (T1_3D_CTXNO_ZC+T1_3D_NUMCTXS_ZC) //17 -#define T1_3D_CTXNO_SC (T1_3D_CTXNO_MAG+T1_3D_NUMCTXS_MAG) //20 -#define T1_3D_CTXNO_UNI (T1_3D_CTXNO_SC+T1_3D_NUMCTXS_SC) //26 -#define T1_3D_NUMCTXS (T1_3D_CTXNO_UNI+T1_3D_NUMCTXS_UNI) //27 - - -/* ----------------------------------------------------------------------- */ - -/** -Tier-1 coding (coding of code-block coefficients) -*/ -typedef struct opj_t1_3d { - /** Codec context */ - opj_common_ptr cinfo; - /** MQC component */ - opj_mqc_t *mqc; - /** RAW component */ - opj_raw_t *raw; - /** LUTs for decoding normalised MSE */ - int lut_nmsedec_sig[1 << T1_NMSEDEC_BITS]; - int lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS]; - int lut_nmsedec_ref[1 << T1_NMSEDEC_BITS]; - int lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS]; - /** Codeblock data */ - int data[T1_CBLKD][T1_CBLKH][T1_CBLKW]; - /** Context information for each voxel in codeblock */ - unsigned int flags[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2]; - /** Voxel information (significance/visited/refined) */ - int flagSVR[T1_CBLKD + 2][T1_CBLKH + 2][T1_CBLKH + 2]; -} opj_t1_3d_t; - -/** @name Exported functions */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a new T1_3D handle -and initialize the look-up tables of the Tier-1 coder/decoder -@return Returns a new T1 handle if successful, returns NULL otherwise -@see t1_init_luts -*/ -opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo); -/** -Destroy a previously created T1_3D handle -@param t1 T1_3D handle to destroy -*/ -void t1_3d_destroy(opj_t1_3d_t *t1); -/** -Encode the code-blocks of a tile -@param t1 T1_3D handle -@param tile The tile to encode -@param tcp Tile coding parameters -*/ -void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); -/** -Decode the code-blocks of a tile -@param t1 T1_3D handle -@param tile The tile to decode -@param tcp Tile coding parameters -*/ -void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); -/** -Get weigths of MSE decoding -@param nmsedec The normalized MSE reduction -@param compno -@param level -@param orient -@param bpno -@param reversible -@param stepsize -@param numcomps -@param dwtid -returns MSE associated to decoding pass -double t1_3d_getwmsedec(int nmsedec, int compno, int levelxy, int levelz, int orient, int bpno, int reversible, double stepsize, int numcomps, int dwtid); -*/ -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __T1_H */ diff --git a/jp3d/libjp3dvm/t2.c b/jp3d/libjp3dvm/t2.c deleted file mode 100755 index 20d8e7d2..00000000 --- a/jp3d/libjp3dvm/t2.c +++ /dev/null @@ -1,675 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/** @defgroup T2 T2 - Implementation of a tier-2 coding */ -/*@{*/ - -/** @name Local static functions */ -/*@{*/ - -static void t2_putcommacode(opj_bio_t *bio, int n); -static int t2_getcommacode(opj_bio_t *bio); -/** -Variable length code for signalling delta Zil (truncation point) -@param bio Bit Input/Output component -@param n delta Zil -*/ -static void t2_putnumpasses(opj_bio_t *bio, int n); -static int t2_getnumpasses(opj_bio_t *bio); -/** -Encode a packet of a tile to a destination buffer -@param tile Tile for which to write the packets -@param tcp Tile coding parameters -@param pi Packet identity -@param dest Destination buffer -@param len Length of the destination buffer -@param volume_info Structure to create an index file -@param tileno Number of the tile encoded -@param cp Coding parameters -@return Number of bytes encoded from the packet -*/ -static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_volume_info_t *volume_info, int tileno, opj_cp_t *cp); -/** -Initialize the segment decoder -@param seg Segment instance -@param cblksty Codeblock style -@param first Is first segment -*/ -static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first); -/** -Decode a packet of a tile from a source buffer -@param t2 T2 handle -@param src Source buffer -@param len Length of the source buffer -@param tile Tile for which to write the packets -@param tcp Tile coding parameters -@param pi Packet identity -@return Number of bytes decoded from the packet -*/ -int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi); - -/*@}*/ - -/*@}*/ - -/* ----------------------------------------------------------------------- */ - -/* #define RESTART 0x04 */ -static void t2_putcommacode(opj_bio_t *bio, int n) { - while (--n >= 0) { - bio_write(bio, 1, 1); - } - bio_write(bio, 0, 1); -} - -static int t2_getcommacode(opj_bio_t *bio) { - int n; - for (n = 0; bio_read(bio, 1); n++) { - ; - } - return n; -} - -static void t2_putnumpasses(opj_bio_t *bio, int n) { - if (n == 1) { - bio_write(bio, 0, 1); - } else if (n == 2) { - bio_write(bio, 2, 2); - } else if (n <= 5) { - bio_write(bio, 0xc | (n - 3), 4); - } else if (n <= 36) { - bio_write(bio, 0x1e0 | (n - 6), 9); - } else if (n <= 164) { - bio_write(bio, 0xff80 | (n - 37), 16); - } -} - -static int t2_getnumpasses(opj_bio_t *bio) { - int n; - if (!bio_read(bio, 1)) - return 1; - if (!bio_read(bio, 1)) - return 2; - if ((n = bio_read(bio, 2)) != 3) - return (3 + n); - if ((n = bio_read(bio, 5)) != 31) - return (6 + n); - return (37 + bio_read(bio, 7)); -} - -static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_volume_info_t * volume_info, int tileno, opj_cp_t *cp) { - int bandno, cblkno; - unsigned char *sop = 0, *eph = 0; - unsigned char *c = dest; - - int compno = pi->compno; /* component value */ - int resno = pi->resno; /* resolution level value */ - int precno = pi->precno; /* precinct value */ - int layno = pi->layno; /* quality layer value */ - - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - opj_bio_t *bio = NULL; /* BIO component */ - - /* */ - if ((tcp->csty & J3D_CP_CSTY_SOP)) { - sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char)); - sop[0] = 255; - sop[1] = 145; - sop[2] = 0; - sop[3] = 4; - sop[4] = (volume_info) ? (volume_info->num % 65536) / 256 : (0 % 65536) / 256 ; - sop[5] = (volume_info) ? (volume_info->num % 65536) % 256 : (0 % 65536) % 256 ; - memcpy(c, sop, 6); - opj_free(sop); - c += 6; - } - /* */ - - if (!layno) { - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - cblk->numpasses = 0; - tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); - } - } - } - - bio = bio_create(); - bio_init_enc(bio, c, len); - bio_write(bio, 1, 1); /* Empty header bit */ - - /* Writing Packet header */ - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - if (!cblk->numpasses && layer->numpasses) { - tgt_setvalue(prc->incltree, cblkno, layno); - } - } - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - int increment = 0; - int nump = 0; - int len = 0, passno; - /* cblk inclusion bits */ - if (!cblk->numpasses) { - tgt_encode(bio, prc->incltree, cblkno, layno + 1); - } else { - bio_write(bio, layer->numpasses != 0, 1); - } - /* if cblk not included, go to the next cblk */ - if (!layer->numpasses) { - continue; - } - /* if first instance of cblk --> zero bit-planes information */ - if (!cblk->numpasses) { - cblk->numlenbits = 3; - tgt_encode(bio, prc->imsbtree, cblkno, 999); - } - /* number of coding passes included */ - t2_putnumpasses(bio, layer->numpasses); - - /* computation of the increase of the length indicator and insertion in the header */ - for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - nump++; - len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); - len = 0; - nump = 0; - } - } - t2_putcommacode(bio, increment); - - /* computation of the new Length indicator */ - cblk->numlenbits += increment; - - /* insertion of the codeword segment length */ - for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - nump++; - len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { - bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); - len = 0; - nump = 0; - } - } - - } - } - - - if (bio_flush(bio)) { - return -999; /* modified to eliminate longjmp !! */ - } - - c += bio_numbytes(bio); - - bio_destroy(bio); - - /* */ - if (tcp->csty & J3D_CP_CSTY_EPH) { - eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char)); - eph[0] = 255; - eph[1] = 146; - memcpy(c, eph, 2); - opj_free(eph); - c += 2; - } - /* */ - - /* Writing the packet body */ - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - if (!layer->numpasses) { - continue; - } - if (c + layer->len > dest + len) { - return -999; - } - - memcpy(c, layer->data, layer->len); - cblk->numpasses += layer->numpasses; - c += layer->len; - /* ADD for index Cfr. Marcela --> delta disto by packet */ - if(volume_info && volume_info->index_write && volume_info->index_on) { - opj_tile_info_t *info_TL = &volume_info->tile[tileno]; - opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num]; - info_PK->disto += layer->disto; - if (volume_info->D_max < info_PK->disto) { - volume_info->D_max = info_PK->disto; - } - } - /* */ - } - } - - return (c - dest); -} - -static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) { - seg->numpasses = 0; - seg->len = 0; - if (cblksty & J3D_CCP_CBLKSTY_TERMALL) { - seg->maxpasses = 1; - } - else if (cblksty & J3D_CCP_CBLKSTY_LAZY) { - if (first) { - seg->maxpasses = 10; - } else { - seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1; - } - } else { - seg->maxpasses = 109; - } -} - -int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) { - int bandno, cblkno; - unsigned char *c = src; - - opj_cp_t *cp = t2->cp; - - int compno = pi->compno; /* component value */ - int resno = pi->resno; /* resolution level value */ - int precno = pi->precno; /* precinct value */ - int layno = pi->layno; /* quality layer value */ - - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - unsigned char *hd = NULL; - int present; - - opj_bio_t *bio = NULL; /* BIO component */ - - if (layno == 0) { - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue; - - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - cblk->numsegs = 0; - } - } - } - - /* SOP markers */ - - if (tcp->csty & J3D_CP_CSTY_SOP) { - if ((*c) != 0xff || (*(c + 1) != 0x91)) { - opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); - } else { - c += 6; - } - - /** TODO : check the Nsop value */ - } - - /* - When the marker PPT/PPM is used the packet header are store in PPT/PPM marker - This part deal with this caracteristic - step 1: Read packet header in the saved structure - step 2: Return to codestream for decoding - */ - - bio = bio_create(); - - if (cp->ppm == 1) { /* PPM */ - hd = cp->ppm_data; - bio_init_dec(bio, hd, cp->ppm_len); - } else if (tcp->ppt == 1) { /* PPT */ - hd = tcp->ppt_data; - bio_init_dec(bio, hd, tcp->ppt_len); - } else { /* Normal Case */ - hd = c; - bio_init_dec(bio, hd, src+len-hd); - } - - present = bio_read(bio, 1); - - if (!present) { - bio_inalign(bio); - hd += bio_numbytes(bio); - bio_destroy(bio); - - /* EPH markers */ - - if (tcp->csty & J3D_CP_CSTY_EPH) { - if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { - printf("Error : expected EPH marker\n"); - } else { - hd += 2; - } - } - - if (cp->ppm == 1) { /* PPM case */ - cp->ppm_len += cp->ppm_data-hd; - cp->ppm_data = hd; - return (c - src); - } - if (tcp->ppt == 1) { /* PPT case */ - tcp->ppt_len+=tcp->ppt_data-hd; - tcp->ppt_data = hd; - return (c - src); - } - - return (hd - src); - } - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue; - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - int included, increment, n; - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_seg_t *seg = NULL; - /* if cblk not yet included before --> inclusion tagtree */ - if (!cblk->numsegs) { - included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); - /* else one bit */ - } else { - included = bio_read(bio, 1); - } - /* if cblk not included */ - if (!included) { - cblk->numnewpasses = 0; - continue; - } - /* if cblk not yet included --> zero-bitplane tagtree */ - if (!cblk->numsegs) { - int i, numimsbs; - for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++); - numimsbs = i - 1; - cblk->numbps = band->numbps - numimsbs; - cblk->numlenbits = 3; - } - /* number of coding passes */ - cblk->numnewpasses = t2_getnumpasses(bio); - increment = t2_getcommacode(bio); - /* length indicator increment */ - cblk->numlenbits += increment; - if (!cblk->numsegs) { - seg = &cblk->segs[0]; - t2_init_seg(seg, tcp->tccps[compno].cblksty, 1); - } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); - } - } - n = cblk->numnewpasses; - - do { - seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n); - seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses)); - n -= seg->numnewpasses; - if (n > 0) { - t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); - } - } while (n > 0); - } - } - - if (bio_inalign(bio)) { - bio_destroy(bio); - return -999; - } - - hd += bio_numbytes(bio); - bio_destroy(bio); - - /* EPH markers */ - if (tcp->csty & J3D_CP_CSTY_EPH) { - if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { - opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); - } else { - hd += 2; - } - } - - if (cp->ppm==1) { - cp->ppm_len+=cp->ppm_data-hd; - cp->ppm_data = hd; - } else if (tcp->ppt == 1) { - tcp->ppt_len+=tcp->ppt_data-hd; - tcp->ppt_data = hd; - } else { - c=hd; - } - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue; - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_seg_t *seg = NULL; - if (!cblk->numnewpasses) - continue; - if (!cblk->numsegs) { - seg = &cblk->segs[0]; - cblk->numsegs++; - cblk->len = 0; - } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - seg++; - cblk->numsegs++; - } - } - - do { - if (c + seg->newlen > src + len) { - return -999; - } - - memcpy(cblk->data + cblk->len, c, seg->newlen); - if (seg->numpasses == 0) { - seg->data = cblk->data + cblk->len; - } - c += seg->newlen; - cblk->len += seg->newlen; - seg->len += seg->newlen; - seg->numpasses += seg->numnewpasses; - cblk->numnewpasses -= seg->numnewpasses; - if (cblk->numnewpasses > 0) { - seg++; - cblk->numsegs++; - } - } while (cblk->numnewpasses > 0); - } - } - - return (c - src); -} - -/* ----------------------------------------------------------------------- */ - -int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info) { - unsigned char *c = dest; - int e = 0; - opj_pi_iterator_t *pi = NULL; - int pino; - - opj_volume_t *volume = t2->volume; - opj_cp_t *cp = t2->cp; - - /* create a packet iterator */ - pi = pi_create(volume, cp, tileno); - if(!pi) { - fprintf(stdout,"[ERROR] Failed to create a pi structure\n"); - return -999; - } - - if(volume_info) { - volume_info->num = 0; - } - - for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { - while (pi_next(&pi[pino])) { - if (pi[pino].layno < maxlayers) { - e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, volume_info, tileno, cp); - //opj_event_msg(t2->cinfo, EVT_INFO, " t2_encode_packet: %d bytes coded\n",e); - if (e == -999) { - break; - } else { - c += e; - } - - /* INDEX >> */ - if(volume_info && volume_info->index_on) { - if(volume_info->index_write) { - opj_tile_info_t *info_TL = &volume_info->tile[tileno]; - opj_packet_info_t *info_PK = &info_TL->packet[volume_info->num]; - if (!volume_info->num) { - info_PK->start_pos = info_TL->end_header + 1; - } else { - info_PK->start_pos = info_TL->packet[volume_info->num - 1].end_pos + 1; - } - info_PK->end_pos = info_PK->start_pos + e - 1; - } - - volume_info->num++; - } - /* << INDEX */ - } - } - } - - /* don't forget to release pi */ - pi_destroy(pi, cp, tileno); - - if (e == -999) { - return e; - } - - return (c - dest); -} - -int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) { - unsigned char *c = src; - opj_pi_iterator_t *pi; - int pino, e = 0; - int n = 0,i; - - opj_volume_t *volume = t2->volume; - opj_cp_t *cp = t2->cp; - - /* create a packet iterator */ - pi = pi_create(volume, cp, tileno); - if(!pi) { - /* TODO: throw an error */ - return -999; - } - - for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { - while (pi_next(&pi[pino])) { - if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) { - e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]); - } else { - e = 0; - } - - /* progression in resolution */ - for (i = 0; i < 3; i++){ - volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i]; - } - n++; - - if (e == -999) { /* ADD */ - break; - } else { - opj_event_msg(t2->cinfo, EVT_INFO, " t2_decode_packet: %d bytes decoded\n",e); - c += e; - } - } - } - - /* don't forget to release pi */ - pi_destroy(pi, cp, tileno); - - if (e == -999) { - return e; - } - - return (c - src); -} - -/* ----------------------------------------------------------------------- */ - -opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp) { - /* create the tcd structure */ - opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t)); - if(!t2) return NULL; - t2->cinfo = cinfo; - t2->volume = volume; - t2->cp = cp; - - return t2; -} - -void t2_destroy(opj_t2_t *t2) { - if(t2) { - opj_free(t2); - } -} - diff --git a/jp3d/libjp3dvm/t2.h b/jp3d/libjp3dvm/t2.h deleted file mode 100755 index 191d0bc4..00000000 --- a/jp3d/libjp3dvm/t2.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __T2_H -#define __T2_H -/** -@file t2.h -@brief Implementation of a tier-2 coding (packetization of code-block data) (T2) - -*/ - -/** @defgroup T2 T2 - Implementation of a tier-2 coding */ -/*@{*/ - -/** -Tier-2 coding -*/ -typedef struct opj_t2 { -/** Codec context */ - opj_common_ptr cinfo; -/** Encoding: pointer to the src volume. Decoding: pointer to the dst volume. */ - opj_volume_t *volume; -/** Pointer to the volume coding parameters */ - opj_cp_t *cp; -} opj_t2_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ - -/** -Encode the packets of a tile to a destination buffer -@param t2 T2 handle -@param tileno number of the tile encoded -@param tile the tile for which to write the packets -@param maxlayers maximum number of layers -@param dest the destination buffer -@param len the length of the destination buffer -@param volume_info structure to create an index file -@return Number of bytes written from packets -*/ -int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_volume_info_t *volume_info); - -/** -Decode the packets of a tile from a source buffer -@param t2 T2 handle -@param src the source buffer -@param len length of the source buffer -@param tileno number that identifies the tile for which to decode the packets -@param tile tile for which to decode the packets -@return Number of bytes read from packets - */ -int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile); - -/** -Create a T2 handle -@param cinfo Codec context info -@param volume Source or destination volume -@param cp Volume coding parameters -@return Returns a new T2 handle if successful, returns NULL otherwise -*/ -opj_t2_t* t2_create(opj_common_ptr cinfo, opj_volume_t *volume, opj_cp_t *cp); -/** -Destroy a T2 handle -@param t2 T2 handle to destroy -*/ -void t2_destroy(opj_t2_t *t2); - -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __T2_H */ diff --git a/jp3d/libjp3dvm/tcd.c b/jp3d/libjp3dvm/tcd.c deleted file mode 100755 index b0184d58..00000000 --- a/jp3d/libjp3dvm/tcd.c +++ /dev/null @@ -1,1738 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez, LPI-UVA, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t * vol) { - int tileno, compno, resno, bandno, precno, cblkno; - - fprintf(fd, "volume {\n"); - fprintf(fd, " tw=%d, th=%d, tl=%d, x0=%d x1=%d y0=%d y1=%d z0=%d z1=%d\n", - vol->tw, vol->th, vol->tl, tcd->volume->x0, tcd->volume->x1, tcd->volume->y0, tcd->volume->y1, tcd->volume->z0, tcd->volume->z1); - - for (tileno = 0; tileno < vol->th * vol->tw * vol->tl; tileno++) { - opj_tcd_tile_t *tile = &tcd->tcd_volume->tiles[tileno]; - fprintf(fd, " tile {\n"); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numcomps=%d\n", - tile->x0, tile->y0, tile->z0, tile->x1, tile->y1, tile->z1, tile->numcomps); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - fprintf(fd, " tilecomp %d {\n",compno); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n", - tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]); - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - fprintf(fd, " res %d{\n",resno); - fprintf(fd," x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, pw=%d, ph=%d, pl=%d, numbands=%d\n", - res->x0, res->y0, res->z0, res->x1, res->y1, res->z1, res->prctno[0], res->prctno[1], res->prctno[2], res->numbands); - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - fprintf(fd, " band %d{\n", bandno); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, stepsize=%f, numbps=%d\n", - band->x0, band->y0, band->z0, band->x1, band->y1, band->z1, band->stepsize, band->numbps); - for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) { - opj_tcd_precinct_t *prec = &band->precincts[precno]; - fprintf(fd, " prec %d{\n",precno); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, cw=%d, ch=%d, cl=%d,\n", - prec->x0, prec->y0, prec->z0, prec->x1, prec->y1, prec->z1, prec->cblkno[0], prec->cblkno[1], prec->cblkno[2]); - for (cblkno = 0; cblkno < (prec->cblkno[0] * prec->cblkno[1] * prec->cblkno[2]); cblkno++) { - opj_tcd_cblk_t *cblk = &prec->cblks[cblkno]; - fprintf(fd, " cblk %d{\n",cblkno); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", cblk->x0, cblk->y0, cblk->z0, cblk->x1, cblk->y1, cblk->z1); - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); -} - -void tilec_dump(FILE *fd, opj_tcd_tilecomp_t *tilec) { - - int i=0,k; - int datalen; - int *a; - - fprintf(fd, " tilecomp{\n"); - fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n", - tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]); - fprintf(fd, " data {\n"); - datalen = (tilec->z1 - tilec->z0) * (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0); - a = tilec->data; - for (k = 0; k < datalen; k++) { - if (!(k % tilec->x1)){ - fprintf(fd, "\n"); - } - if (!(k % (tilec->y1 * tilec->x1))){ - fprintf(fd, "Slice %d\n",i++); - } - fprintf(fd," %d",a[k]); - - - } - fprintf(fd, " }\n"); - /*i=0; - fprintf(fd, "Slice %d\n"); - if (tilec->prediction->prederr) { - fprintf(fd, " prederror {\n"); - a = tilec->prediction->prederr; - for (k = 0; k < datalen; k++) { - fprintf(fd," %d",*(a++)); - if (!(k % (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0))){ - fprintf(fd, "\n");fprintf(fd, "Slice %d\n",i++); - } - if (!(k % (tilec->x1 - tilec->x0))){ - fprintf(fd, "\n"); - } - } - } - fprintf(fd, " }\n");*/ - fprintf(fd, "}\n"); -} - -/* ----------------------------------------------------------------------- */ - -/** -Create a new TCD handle -*/ -opj_tcd_t* tcd_create(opj_common_ptr cinfo) { - /* create the tcd structure */ - opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t)); - if(!tcd) return NULL; - tcd->cinfo = cinfo; - tcd->tcd_volume = (opj_tcd_volume_t*)opj_malloc(sizeof(opj_tcd_volume_t)); - if(!tcd->tcd_volume) { - opj_free(tcd); - return NULL; - } - - return tcd; -} - -/** -Destroy a previously created TCD handle -*/ -void tcd_destroy(opj_tcd_t *tcd) { - if(tcd) { - opj_free(tcd->tcd_volume); - opj_free(tcd); - } -} - -/* ----------------------------------------------------------------------- */ -void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) { - int compno, resno, bandno, precno, cblkno, i, j;//, k; - - opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */ - opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */ - opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */ - opj_tcd_band_t *band = NULL; /* pointer to tcd->band */ - opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */ - opj_tcd_cblk_t *cblk = NULL; /* pointer to tcd->cblk */ - opj_tcp_t *tcp = &cp->tcps[curtileno]; - int p,q,r; - - tcd->volume = volume; - tcd->cp = cp; - tcd->tcd_volume->tw = cp->tw; - tcd->tcd_volume->th = cp->th; - tcd->tcd_volume->tl = cp->tl; - tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t)); - tcd->tile = tcd->tcd_volume->tiles; - tile = tcd->tile; - - - /* p61 ISO/IEC IS15444-1 : 2002 */ - /* curtileno --> raster scanned index of tiles */ - /* p,q,r --> matricial index of tiles */ - p = curtileno % cp->tw; - q = curtileno / cp->tw; - r = curtileno / (cp->tw * cp->th); /* extension to 3-D */ - - /* 4 borders of the tile rescale on the volume if necessary (B.3)*/ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0); - tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1); - tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1); - tile->numcomps = volume->numcomps; - - /* Modification of the RATE >> */ - for (j = 0; j < tcp->numlayers; j++) { - if (tcp->rates[j] <= 1) { - tcp->rates[j] = 0; - } else { - float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec); - float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz); - den = tcp->rates[j] * den; - tcp->rates[j] = (num + den - 1) / den; - } - /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv( - tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec, - (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/ - if (tcp->rates[j]) { - if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { - tcp->rates[j] = tcp->rates[j - 1] + 20; - } else if (!j && tcp->rates[j] < 30){ - tcp->rates[j] = 30; - } - } - } - /* << Modification of the RATE */ - - tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t)); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - int res_max; - int prevnumbands = 0; - - /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */ - tcd->tilec = &tile->comps[compno]; - tilec = tcd->tilec; - - /* border of each tile component (global) (B.3) */ - tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy); - tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz); - tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy); - tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz); - - tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int)); - - res_max = 0; - for (i = 0;i < 3; i++){ - tilec->numresolution[i] = tccp->numresolution[i]; - //Greater of 3 resolutions contains all information - res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max; - } - - - tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t)); - for (resno = 0; resno < res_max; resno++) { - - int pdx, pdy, pdz; - int tlprcxstart, tlprcystart, tlprczstart; - int brprcxend, brprcyend, brprczend; - int tlcbgxstart, tlcbgystart, tlcbgzstart; - int brcbgxend, brcbgyend, brcbgzend; - int cbgwidthexpn, cbgheightexpn, cbglengthexpn; - int cblkwidthexpn, cblkheightexpn, cblklengthexpn; - - int diff = tccp->numresolution[0] - tccp->numresolution[2]; - int levelnox = tilec->numresolution[0] - 1 - resno; - int levelnoy = tilec->numresolution[1] - 1 - resno; - int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff)); - if (levelnoz < 0) levelnoz = 0; - - /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */ - tcd->res = &tilec->resolutions[resno]; - res = tcd->res; - - /* border for each resolution level (global) (B.14)*/ - res->x0 = int_ceildivpow2(tilec->x0, levelnox); - res->y0 = int_ceildivpow2(tilec->y0, levelnoy); - res->z0 = int_ceildivpow2(tilec->z0, levelnoz); - res->x1 = int_ceildivpow2(tilec->x1, levelnox); - res->y1 = int_ceildivpow2(tilec->y1, levelnoy); - res->z1 = int_ceildivpow2(tilec->z1, levelnoz); - //if (res->z1 < 0)fprintf(stdout,"Res: %d %d/%d --> %d\n",resno,tilec->z1, levelnoz, int_ceildivpow2(tilec->z1, levelnoz)); - - res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */ - - /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */ - if (tccp->csty & J3D_CCP_CSTY_PRT) { - pdx = tccp->prctsiz[0][resno]; - pdy = tccp->prctsiz[1][resno]; - pdz = tccp->prctsiz[2][resno]; - } else { - pdx = 15; - pdy = 15; - pdz = 15; - } - - /* p. 66, B.16, ISO/IEC IS15444-1 : 2002 */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - tlprczstart = int_floordivpow2(res->z0, pdz) << pdz; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - brprczend = int_ceildivpow2(res->z1, pdz) << pdz; - - res->prctno[0] = (brprcxend - tlprcxstart) >> pdx; - res->prctno[1] = (brprcyend - tlprcystart) >> pdy; - res->prctno[2] = (brprczend - tlprczstart) >> pdz; - if (res->prctno[2] == 0) res->prctno[2] = 1; - - /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002 */ - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - tlcbgzstart = tlprczstart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - brcbgzend = brprczend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - cbglengthexpn = pdz; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - tlcbgzstart = int_ceildivpow2(tlprczstart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - brcbgzend = int_ceildivpow2(brprczend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - cbglengthexpn = pdz - 1; - } - - cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); //6 - cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); //6 - cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); //6 - - res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t)); - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b, z0b, i; - int gain, numbps; - opj_stepsize_t *ss = NULL; - - tcd->band = &res->bands[bandno]; - band = tcd->band; - - band->bandno = (resno == 0) ? 0 : bandno + 1; - /* Bandno: 0 - LLL 2 - LHL - 1 - HLL 3 - HHL - 4 - LLH 6 - LHH - 5 - HLH 7 - HHH */ - x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; - z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; - - /* p. 65, B.15, ISO/IEC IS15444-1 : 2002 */ - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelnox); - band->y0 = int_ceildivpow2(tilec->y0, levelnoy); - band->z0 = int_ceildivpow2(tilec->z0, levelnoz); - band->x1 = int_ceildivpow2(tilec->x1, levelnox); - band->y1 = int_ceildivpow2(tilec->y1, levelnoy); - band->z1 = int_ceildivpow2(tilec->z1, levelnoz); - } else { - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1); - band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1); - band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1); - } - - ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)]; - if (bandno == (res->numbands - 1)) - prevnumbands += (resno == 0) ? 0 : res->numbands; - gain = dwt_getgain(band->bandno,tccp->reversible); - numbps = volume->comps[compno].prec + gain; - band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = (opj_tcd_precinct_t *) opj_malloc((res->prctno[0] * res->prctno[1] * res->prctno[2]) * sizeof(opj_tcd_precinct_t)); - - for (i = 0; i < (res->prctno[0] * res->prctno[1] * res->prctno[2]); i++) { - band->precincts[i].imsbtree = NULL; - band->precincts[i].incltree = NULL; - } - - for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) { - int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend; - int cbgxstart, cbgystart, cbgzstart, cbgxend, cbgyend, cbgzend; - - cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn); - cbgystart = tlcbgystart + ((precno % (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn); - cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn); - cbgxend = cbgxstart + (1 << cbgwidthexpn); - cbgyend = cbgystart + (1 << cbgheightexpn); - cbgzend = cbgzstart + (1 << cbglengthexpn); - - tcd->prc = &band->precincts[precno]; - prc = tcd->prc; - - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->z0 = int_max(cbgzstart, band->z0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - prc->z1 = int_min(cbgzend, band->z1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn; - prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn; - prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn; - prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2]; - - prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t)); - prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]); - prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]); - //tgt_tree_dump(stdout,prc->incltree); - for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn); - int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - int cblkzend = cblkzstart + (1 << cblklengthexpn); - int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1)); - - tcd->cblk = &prc->cblks[cblkno]; - cblk = tcd->cblk; - - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->z0 = int_max(cblkzstart, prc->z0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - cblk->z1 = int_min(cblkzend, prc->z1); - } - } - } - } - } - //tcd_dump(stdout, tcd, tcd->tcd_volume); - -} -void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) { - int compno, resno, bandno, precno, cblkno; - int j, p, q, r; - - opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */ - opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */ - opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */ - opj_tcd_band_t *band = NULL; /* pointer to tcd->band */ - opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */ - opj_tcd_cblk_t *cblk = NULL; /* pointer to tcd->cblk */ - opj_tcp_t *tcp = &cp->tcps[curtileno]; - - tcd->tile = tcd->tcd_volume->tiles; - tile = tcd->tile; - - /* p61 ISO/IEC IS15444-1 : 2002 */ - /* curtileno --> raster scanned index of tiles */ - /* p,q,r --> matricial index of tiles */ - p = curtileno % cp->tw; - q = curtileno / cp->tw; - r = curtileno / (cp->tw * cp->th); /* extension to 3-D */ - - /* 4 borders of the tile rescale on the volume if necessary (B.3)*/ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0); - tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1); - tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1); - tile->numcomps = volume->numcomps; - - /* Modification of the RATE >> */ - for (j = 0; j < tcp->numlayers; j++) { - if (tcp->rates[j] <= 1) { - tcp->rates[j] = 0; - } else { - float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec); - float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz); - den = tcp->rates[j] * den; - tcp->rates[j] = (num + den - 1) / den; - } - /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv( - tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec, - (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/ - if (tcp->rates[j]) { - if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { - tcp->rates[j] = tcp->rates[j - 1] + 20; - } else if (!j && tcp->rates[j] < 30){ - tcp->rates[j] = 30; - } - } - } - /* << Modification of the RATE */ - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - int res_max, i; - int prevnumbands = 0; - - /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */ - tcd->tilec = &tile->comps[compno]; - tilec = tcd->tilec; - - /* border of each tile component (global) (B.3) */ - tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy); - tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz); - tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy); - tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz); - - tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int)); - - res_max = 0; - for (i = 0;i < 3; i++){ - tilec->numresolution[i] = tccp->numresolution[i]; - //Greater of 3 resolutions contains all information - res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max; - } - - tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t)); - for (resno = 0; resno < res_max; resno++) { - int pdx, pdy, pdz; - int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend; - int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend; - int cbgwidthexpn, cbgheightexpn, cbglengthexpn; - int cblkwidthexpn, cblkheightexpn, cblklengthexpn; - - int levelnox = tilec->numresolution[0] - 1 - resno; - int levelnoy = tilec->numresolution[1] - 1 - resno; - int diff = tccp->numresolution[0] - tccp->numresolution[2]; - int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff)); - if (levelnoz < 0) levelnoz = 0; - - tcd->res = &tilec->resolutions[resno]; - res = tcd->res; - - /* border for each resolution level (global) (B.14)*/ - res->x0 = int_ceildivpow2(tilec->x0, levelnox); - res->y0 = int_ceildivpow2(tilec->y0, levelnoy); - res->z0 = int_ceildivpow2(tilec->z0, levelnoz); - res->x1 = int_ceildivpow2(tilec->x1, levelnox); - res->y1 = int_ceildivpow2(tilec->y1, levelnoy); - res->z1 = int_ceildivpow2(tilec->z1, levelnoz); - - // res->numbands = resno == 0 ? 1 : 3; /* --> 2D */ - res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */ - - /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */ - if (tccp->csty & J3D_CCP_CSTY_PRT) { - pdx = tccp->prctsiz[0][resno]; - pdy = tccp->prctsiz[1][resno]; - pdz = tccp->prctsiz[2][resno]; - } else { - pdx = 15; - pdy = 15; - pdz = 15; - } - /* p. 66, B.16, ISO/IEC IS15444-1 : 2002 */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - tlprczstart = int_floordivpow2(res->z0, pdz) << pdz; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - brprczend = int_ceildivpow2(res->z1, pdz) << pdz; - - res->prctno[0] = (brprcxend - tlprcxstart) >> pdx; - res->prctno[1] = (brprcyend - tlprcystart) >> pdy; - res->prctno[2] = (brprczend - tlprczstart) >> pdz; - if (res->prctno[2] == 0) res->prctno[2] = 1; - - /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002 */ - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - tlcbgzstart = tlprczstart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - brcbgzend = brprczend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - cbglengthexpn = pdz; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - tlcbgzstart = int_ceildivpow2(tlprczstart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - brcbgzend = int_ceildivpow2(brprczend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - cbglengthexpn = pdz - 1; - } - - cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); - cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); - - res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t)); - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b, z0b; - int gain, numbps; - opj_stepsize_t *ss = NULL; - - tcd->band = &res->bands[bandno]; - band = tcd->band; - - band->bandno = resno == 0 ? 0 : bandno + 1; - /* Bandno: 0 - LLL 2 - LHL - 1 - HLL 3 - HHL - 4 - LLH 6 - LHH - 5 - HLH 7 - HHH */ - x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; - z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; - - /* p. 65, B.15, ISO/IEC IS15444-1 : 2002 */ - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelnox); - band->y0 = int_ceildivpow2(tilec->y0, levelnoy); - band->z0 = int_ceildivpow2(tilec->z0, levelnoz); - band->x1 = int_ceildivpow2(tilec->x1, levelnox); - band->y1 = int_ceildivpow2(tilec->y1, levelnoy); - band->z1 = int_ceildivpow2(tilec->z1, levelnoz); - } else { - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1); - band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1); - band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1); - } - - ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)]; - if (bandno == (res->numbands - 1)) - prevnumbands += (resno == 0) ? 0 : res->numbands; - gain = dwt_getgain(band->bandno,tccp->reversible); - numbps = volume->comps[compno].prec + gain; - - band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend; - - int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn); - int cbgystart = tlcbgystart + ((precno / (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn); - int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - int cbgzend = cbgzstart + (1 << cbglengthexpn); - - /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */ - tcd->prc = &band->precincts[precno]; - prc = tcd->prc; - - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->z0 = int_max(cbgzstart, band->z0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - prc->z1 = int_min(cbgzend, band->z1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn; - prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn; - prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn; - prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2]; - - opj_free(prc->cblks); - prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t)); - prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]); - prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]); - - for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn); - int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - int cblkzend = cblkzstart + (1 << cblklengthexpn); - int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1)); - - tcd->cblk = &prc->cblks[cblkno]; - cblk = tcd->cblk; - - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->z0 = int_max(cblkzstart, prc->z0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - cblk->z1 = int_min(cblkzend, prc->z1); - } - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ - //tcd_dump(stdout, tcd, tcd->tcd_volume); -} - - -void tcd_free_encode(opj_tcd_t *tcd) { - int tileno, compno, resno, bandno, precno; - - opj_tcd_tile_t *tile = NULL; /* pointer to tcd->tile */ -// opj_tcd_slice_t *slice = NULL; /* pointer to tcd->slice */ - opj_tcd_tilecomp_t *tilec = NULL; /* pointer to tcd->tilec */ - opj_tcd_resolution_t *res = NULL; /* pointer to tcd->res */ - opj_tcd_band_t *band = NULL; /* pointer to tcd->band */ - opj_tcd_precinct_t *prc = NULL; /* pointer to tcd->prc */ - - for (tileno = 0; tileno < 1; tileno++) { - tcd->tile = tcd->tcd_volume->tiles; - tile = tcd->tile; - - for (compno = 0; compno < tile->numcomps; compno++) { - tcd->tilec = &tile->comps[compno]; - tilec = tcd->tilec; - - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - tcd->res = &tilec->resolutions[resno]; - res = tcd->res; - - for (bandno = 0; bandno < res->numbands; bandno++) { - tcd->band = &res->bands[bandno]; - band = tcd->band; - - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - tcd->prc = &band->precincts[precno]; - prc = tcd->prc; - - if (prc->incltree != NULL) { - tgt_destroy(prc->incltree); - prc->incltree = NULL; - } - if (prc->imsbtree != NULL) { - tgt_destroy(prc->imsbtree); - prc->imsbtree = NULL; - } - opj_free(prc->cblks); - prc->cblks = NULL; - } /* for (precno */ - opj_free(band->precincts); - band->precincts = NULL; - } /* for (bandno */ - } /* for (resno */ - opj_free(tilec->resolutions); - tilec->resolutions = NULL; - } /* for (compno */ - opj_free(tile->comps); - tile->comps = NULL; - } /* for (tileno */ - opj_free(tcd->tcd_volume->tiles); - tcd->tcd_volume->tiles = NULL; -} - -/* ----------------------------------------------------------------------- */ -void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp) { - int tileno, compno, resno, bandno, precno, cblkno, res_max, - i, j, p, q, r; - unsigned int x0 = 0, y0 = 0, z0 = 0, - x1 = 0, y1 = 0, z1 = 0, - w, h, l; - - tcd->volume = volume; - tcd->cp = cp; - tcd->tcd_volume->tw = cp->tw; - tcd->tcd_volume->th = cp->th; - tcd->tcd_volume->tl = cp->tl; - tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcd_tile_t)); - - for (i = 0; i < cp->tileno_size; i++) { - opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]); - opj_tcd_tile_t *tile = &(tcd->tcd_volume->tiles[cp->tileno[i]]); - - /* p61 ISO/IEC IS15444-1 : 2002 */ - /* curtileno --> raster scanned index of tiles */ - /* p,q,r --> matricial index of tiles */ - tileno = cp->tileno[i]; - p = tileno % cp->tw; - q = tileno / cp->tw; - r = tileno / (cp->tw * cp->th); /* extension to 3-D */ - - /* 4 borders of the tile rescale on the volume if necessary (B.3)*/ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0); - tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1); - tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1); - tile->numcomps = volume->numcomps; - - tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t)); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - int prevnumbands = 0; - - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy); - tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz); - tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy); - tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz); - - tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int)); - - res_max = 0; - for (i = 0;i < 3; i++){ - tilec->numresolution[i] = tccp->numresolution[i]; - //Greater of 3 resolutions contains all information - res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max; - } - - tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t)); - - for (resno = 0; resno < res_max; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - int pdx, pdy, pdz; - int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend; - int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend; - int cbgwidthexpn, cbgheightexpn, cbglengthexpn; - int cblkwidthexpn, cblkheightexpn, cblklengthexpn; - int levelnox = tilec->numresolution[0] - 1 - resno; - int levelnoy = tilec->numresolution[1] - 1 - resno; - int diff = tccp->numresolution[0] - tccp->numresolution[2]; - int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff)); - if (levelnoz < 0) levelnoz = 0; - - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelnox); - res->y0 = int_ceildivpow2(tilec->y0, levelnoy); - res->z0 = int_ceildivpow2(tilec->z0, levelnoz); - res->x1 = int_ceildivpow2(tilec->x1, levelnox); - res->y1 = int_ceildivpow2(tilec->y1, levelnoy); - res->z1 = int_ceildivpow2(tilec->z1, levelnoz); - res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */ - - /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */ - if (tccp->csty & J3D_CCP_CSTY_PRT) { - pdx = tccp->prctsiz[0][resno]; - pdy = tccp->prctsiz[1][resno]; - pdz = tccp->prctsiz[2][resno]; - } else { - pdx = 15; - pdy = 15; - pdz = 15; - } - - /* p. 66, B.16, ISO/IEC IS15444-1 : 2002 */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - tlprczstart = int_floordivpow2(res->z0, pdz) << pdz; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - brprczend = int_ceildivpow2(res->z1, pdz) << pdz; - - res->prctno[0] = (brprcxend - tlprcxstart) >> pdx; - res->prctno[1] = (brprcyend - tlprcystart) >> pdy; - res->prctno[2] = (brprczend - tlprczstart) >> pdz; - - /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002 */ - if (resno == 0) { - tlcbgxstart = tlprcxstart;//0 - tlcbgystart = tlprcystart; - tlcbgzstart = tlprczstart; - brcbgxend = brprcxend;//1 - brcbgyend = brprcyend; - brcbgzend = brprczend; - cbgwidthexpn = pdx; //15 - cbgheightexpn = pdy; - cbglengthexpn = pdz; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - tlcbgzstart = int_ceildivpow2(tlprczstart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - brcbgzend = int_ceildivpow2(brprczend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - cbglengthexpn = pdz - 1; - } - - cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); //6 - cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); //6 - cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); //6 - - res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t)); - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b, z0b; - int gain, numbps; - opj_stepsize_t *ss = NULL; - - opj_tcd_band_t *band = &res->bands[bandno]; - band->bandno = resno == 0 ? 0 : bandno + 1; - /* Bandno: 0 - LLL 2 - LHL - 1 - HLL 3 - HHL - 4 - LLH 6 - LHH - 5 - HLH 7 - HHH */ - x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; - z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0; - - /* p. 65, B.15, ISO/IEC IS15444-1 : 2002 */ - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelnox); - band->y0 = int_ceildivpow2(tilec->y0, levelnoy); - band->z0 = int_ceildivpow2(tilec->z0, levelnoz); - band->x1 = int_ceildivpow2(tilec->x1, levelnox); - band->y1 = int_ceildivpow2(tilec->y1, levelnoy); - band->z1 = int_ceildivpow2(tilec->z1, levelnoz); - } else { - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1); - band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1); - band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1); - } - - ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)]; - if (bandno == (res->numbands - 1)) - prevnumbands += (resno == 0) ? 0 : res->numbands; - gain = dwt_getgain(band->bandno,tccp->reversible); - numbps = volume->comps[compno].prec + gain; - - band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->prctno[0] * res->prctno[1] * res->prctno[2] * sizeof(opj_tcd_precinct_t)); - - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend; - - int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn); - int cbgystart = tlcbgystart + (precno / res->prctno[0]) * (1 << cbgheightexpn); - int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - int cbgzend = cbgzstart + (1 << cbglengthexpn); - - opj_tcd_precinct_t *prc = &band->precincts[precno]; - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->z0 = int_max(cbgzstart, band->z0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - prc->z1 = int_min(cbgzend, band->z1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn; - prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn; - prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn; - prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2]; - - prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t)); - prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]); - prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]); - - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn); - int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - int cblkzend = cblkzstart + (1 << cblklengthexpn); - int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1)); - /* code-block size (global) */ - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->z0 = int_max(cblkzstart, prc->z0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - cblk->z1 = int_min(cblkzend, prc->z1); - } - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ - } /* i = 0..cp->tileno_size */ - - //tcd_dump(stdout, tcd, tcd->tcd_volume); - - /* - Allocate place to store the decoded data = final volume - Place limited by the tile really present in the codestream - */ - - for (i = 0; i < volume->numcomps; i++) { - for (j = 0; j < cp->tileno_size; j++) { - tileno = cp->tileno[j]; - x0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x0 : int_min(x0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x0); - y0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y0 : int_min(y0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y0); - z0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z0 : int_min(z0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z0); - x1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x1 : int_max(x1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x1); - y1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y1 : int_max(y1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y1); - z1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z1 : int_max(z1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z1); - } - - w = x1 - x0; - h = y1 - y0; - l = z1 - z0; - - volume->comps[i].data = (int *) opj_malloc(w * h * l * sizeof(int)); - volume->comps[i].w = w; - volume->comps[i].h = h; - volume->comps[i].l = l; - volume->comps[i].x0 = x0; - volume->comps[i].y0 = y0; - volume->comps[i].z0 = z0; - volume->comps[i].bigendian = cp->bigendian; - } -} - -void tcd_free_decode(opj_tcd_t *tcd) { - int tileno,compno,resno,bandno,precno; - - opj_tcd_volume_t *tcd_volume = tcd->tcd_volume; - - for (tileno = 0; tileno < tcd_volume->tw * tcd_volume->th * tcd_volume->tl; tileno++) { - opj_tcd_tile_t *tile = &tcd_volume->tiles[tileno]; - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->prctno[1] * res->prctno[0] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prec = &band->precincts[precno]; - if (prec->cblks != NULL) opj_free(prec->cblks); - if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree); - if (prec->incltree != NULL) tgt_destroy(prec->incltree); - /*for (treeno = 0; treeno < prec->numtrees; treeno++){ - if (prec->imsbtree[treeno] != NULL) tgt_destroy(prec->imsbtree[treeno]); - if (prec->incltree[treeno] != NULL) tgt_destroy(prec->incltree[treeno]); - }*/ - } - if (band->precincts != NULL) opj_free(band->precincts); - } - } - if (tilec->resolutions != NULL) opj_free(tilec->resolutions); - } - if (tile->comps != NULL) opj_free(tile->comps); - } - - if (tcd_volume->tiles != NULL) opj_free(tcd_volume->tiles); -} - - - -/* ----------------------------------------------------------------------- */ -void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) { - int compno, resno, bandno, precno, cblkno; - int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolution[0]][3]; */ - int matrice[10][10][3]; - int i, j, k; - - opj_cp_t *cp = tcd->cp; - opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; - opj_tcp_t *tcd_tcp = tcd->tcp; - - /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolution[0]*3*sizeof(int)); */ - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; - for (i = 0; i < tcd_tcp->numlayers; i++) { - for (j = 0; j < tilec->numresolution[0]; j++) { - for (k = 0; k < 3; k++) { - matrice[i][j][k] = - (int) (cp->matrice[i * tilec->numresolution[0] * 3 + j * 3 + k] - * (float) (tcd->volume->comps[compno].prec / 16.0)); - } - } - } - - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - int n; - int imsb = tcd->volume->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ - /* Correction of the matrix of coefficient to include the IMSB information */ - if (layno == 0) { - value = matrice[layno][resno][bandno]; - if (imsb >= value) { - value = 0; - } else { - value -= imsb; - } - } else { - value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno]; - if (imsb >= matrice[layno - 1][resno][bandno]) { - value -= (imsb - matrice[layno - 1][resno][bandno]); - if (value < 0) { - value = 0; - } - } - } - - if (layno == 0) { - cblk->numpassesinlayers = 0; - } - - n = cblk->numpassesinlayers; - if (cblk->numpassesinlayers == 0) { - if (value != 0) { - n = 3 * value - 2 + cblk->numpassesinlayers; - } else { - n = cblk->numpassesinlayers; - } - } else { - n = 3 * value + cblk->numpassesinlayers; - } - - layer->numpasses = n - cblk->numpassesinlayers; - - if (!layer->numpasses) - continue; - - if (cblk->numpassesinlayers == 0) { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - } else { - layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; - } - if (final) - cblk->numpassesinlayers = n; - } - } - } - } - } -} - -void tcd_rateallocate_fixed(opj_tcd_t *tcd) { - int layno; - for (layno = 0; layno < tcd->tcp->numlayers; layno++) { - tcd_makelayer_fixed(tcd, layno, 1); - } -} - -void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) { - int compno, resno, bandno, precno, cblkno, passno; - - opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; - - tcd_tile->distolayer[layno] = 0; /* fixed_quality */ - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_layer_t *layer = &cblk->layers[layno]; - - int n; - if (layno == 0) { - cblk->numpassesinlayers = 0; - } - n = cblk->numpassesinlayers; - for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) { - int dr; - double dd; - opj_tcd_pass_t *pass = &cblk->passes[passno]; - if (n == 0) { - dr = pass->rate; - dd = pass->distortiondec; - } else { - dr = pass->rate - cblk->passes[n - 1].rate; - dd = pass->distortiondec - cblk->passes[n - 1].distortiondec; - } - if (!dr) { - if (dd) - n = passno + 1; - continue; - } - if (dd / dr >= thresh){ - n = passno + 1; - } - } - layer->numpasses = n - cblk->numpassesinlayers; - - if (!layer->numpasses) { - layer->disto = 0; - continue; - } - if (cblk->numpassesinlayers == 0) { - layer->len = cblk->passes[n - 1].rate; - layer->data = cblk->data; - layer->disto = cblk->passes[n - 1].distortiondec; - } else { - layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate; - layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec; - } - - tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */ - - if (final) - cblk->numpassesinlayers = n; - - // fprintf(stdout,"MakeLayer : %d %f %d %d \n",layer->len, layer->disto, layer->numpasses, n); - } - } - } - } - } -} - -bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_volume_info_t * volume_info) { - int compno, resno, bandno, precno, cblkno, passno, layno; - double min, max; - double cumdisto[100]; /* fixed_quality */ - const double K = 1; /* 1.1; // fixed_quality */ - double maxSE = 0; - - opj_cp_t *cp = tcd->cp; - opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; - opj_tcp_t *tcd_tcp = tcd->tcp; - - min = DBL_MAX; - max = 0; - - tcd_tile->nbpix = 0; /* fixed_quality */ - - for (compno = 0; compno < tcd_tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; - tilec->nbpix = 0; - for (resno = 0; resno < tilec->numresolution[0]; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - for (passno = 0; passno < cblk->totalpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - int dr; - double dd, rdslope; - if (passno == 0) { - dr = pass->rate; - dd = pass->distortiondec; - } else { - dr = pass->rate - cblk->passes[passno - 1].rate; - dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec; - } - if (dr == 0) { - continue; - } - rdslope = dd / dr; - if (rdslope < min) { - min = rdslope; - } - if (rdslope > max) { - max = rdslope; - } - - } /* passno */ - - /* fixed_quality */ - tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0)); - tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0)); - } /* cbklno */ - } /* precno */ - } /* bandno */ - } /* resno */ - - maxSE += (((double)(1 << tcd->volume->comps[compno].prec) - 1.0) - * ((double)(1 << tcd->volume->comps[compno].prec) -1.0)) - * ((double)(tilec->nbpix)); - } /* compno */ - - /* add antonin index */ - if(volume_info && volume_info->index_on) { - opj_tile_info_t *info_TL = &volume_info->tile[tcd->tcd_tileno]; - info_TL->nbpix = tcd_tile->nbpix; - info_TL->distotile = tcd_tile->distotile; - info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double)); - } - /* dda */ - - for (layno = 0; layno < tcd_tcp->numlayers; layno++) { - double lo = min; - double hi = max; - int success = 0; - int maxlen = tcd_tcp->rates[layno] ? int_min(((int) tcd_tcp->rates[layno]), len) : len; - double goodthresh; - double distotarget; /* fixed_quality */ - int i = 0; - - /* fixed_quality */ - distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10)); - - if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) { - opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->volume, cp); - int oldl = 0, oldoldl = 0; - for (i = 0; i < 128; i++) { - double thresh = (lo + hi) / 2; - int l = 0; - double distoachieved = 0; /* fixed_quality -q */ - - tcd_makelayer(tcd, layno, thresh, 0); - - if (cp->fixed_quality) { /* fixed_quality -q */ - distoachieved = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; - if (distoachieved < distotarget) { - hi = thresh; - continue; - } - lo = thresh; - } else { /* disto_alloc -r, fixed_alloc -f */ - l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, volume_info); - //fprintf(stdout, "layno %d i %d len=%d max=%d \n",layno,i,l,maxlen); - if (l == -999) { - lo = thresh; - continue; - } else if (l == oldl && oldl == oldoldl && tcd_tile->distolayer[layno] > 0.0 && i>32) - break; - hi = thresh; - oldoldl = oldl; - oldl = l; - } - success = 1; - goodthresh = thresh; - } - t2_destroy(t2); - } else { - success = 1; - goodthresh = min; - } - if (!success) { - return false; - } - - if(volume_info && volume_info->index_on) { /* Threshold for Marcela Index */ - volume_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh; - } - tcd_makelayer(tcd, layno, goodthresh, 1); - - /* fixed_quality */ - cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; - } - - return true; -} - -/* ----------------------------------------------------------------------- */ -int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_volume_info_t * volume_info) { - int compno; - int l, i, npck = 0; - double encoding_time; - - opj_tcd_tile_t *tile = NULL; - opj_tcp_t *tcd_tcp = NULL; - opj_cp_t *cp = NULL; - - opj_tcp_t *tcp = &tcd->cp->tcps[0]; - opj_tccp_t *tccp = &tcp->tccps[0]; - opj_volume_t *volume = tcd->volume; - opj_t2_t *t2 = NULL; /* T2 component */ - - tcd->tcd_tileno = tileno; /* current encoded/decoded tile */ - - tcd->tcd_tile = tcd->tcd_volume->tiles; /* tile information */ - tile = tcd->tcd_tile; - - tcd->tcp = &tcd->cp->tcps[tileno]; /* coding/decoding params of tileno */ - tcd_tcp = tcd->tcp; - - cp = tcd->cp; /* coding parameters */ - - /* INDEX >> */ - if(volume_info && volume_info->index_on) { - opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */ - for (i = 0; i < tilec_idx->numresolution[0]; i++) { - opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; - - volume_info->tile[tileno].prctno[0][i] = res_idx->prctno[0]; - volume_info->tile[tileno].prctno[1][i] = res_idx->prctno[1]; - volume_info->tile[tileno].prctno[2][i] = res_idx->prctno[2]; - - npck += res_idx->prctno[0] * res_idx->prctno[1] * res_idx->prctno[2]; - - volume_info->tile[tileno].prctsiz[0][i] = tccp->prctsiz[0][i]; - volume_info->tile[tileno].prctsiz[1][i] = tccp->prctsiz[1][i]; - volume_info->tile[tileno].prctsiz[2][i] = tccp->prctsiz[2][i]; - } - volume_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(volume_info->comp * volume_info->layer * npck * sizeof(opj_packet_info_t)); - } - /* << INDEX */ - - /*---------------TILE-------------------*/ - encoding_time = opj_clock(); /* time needed to encode a tile */ - - for (compno = 0; compno < tile->numcomps; compno++) { - int x, y, z; - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - int adjust; - int offset_x = int_ceildiv(volume->x0, volume->comps[compno].dx); //ceil(x0 / subsampling_dx) - int offset_y = int_ceildiv(volume->y0, volume->comps[compno].dy); - int offset_z = int_ceildiv(volume->z0, volume->comps[compno].dz); - - int tw = tilec->x1 - tilec->x0; - int w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx); - int th = tilec->y1 - tilec->y0; - int h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy); - int tl = tilec->z1 - tilec->z0; - int l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz); - - - - /* extract tile data from volume.comps[0].data to tile.comps[0].data */ - //fprintf(stdout,"[INFO] Extract tile data\n"); - if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) { - adjust = 0; - } else { - adjust = volume->comps[compno].sgnd ? 0 : 1 << (volume->comps[compno].prec - 1); //sign=='+' --> 2^(prec-1) - if (volume->comps[compno].dcoffset != 0){ - adjust += volume->comps[compno].dcoffset; - fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",volume->comps[compno].dcoffset,adjust); - } - } - - if (tcd_tcp->tccps[compno].reversible == 1) { //IF perfect reconstruction (DWT.5-3) - for (z = tilec->z0; z < tilec->z1; z++) { - for (y = tilec->y0; y < tilec->y1; y++) { - /* start of the src tile scanline */ - int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h]; - /* start of the dst tile scanline */ - int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th]; - for (x = tilec->x0; x < tilec->x1; x++) { - *tile_data++ = *data++ - adjust; - } - } - } - } else if (tcd_tcp->tccps[compno].reversible == 0) { //IF not (DWT.9-7) - for (z = tilec->z0; z < tilec->z1; z++) { - for (y = tilec->y0; y < tilec->y1; y++) { - /* start of the src tile scanline */ - int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h]; - /* start of the dst tile scanline */ - int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th]; - for (x = tilec->x0; x < tilec->x1; x++) { - *tile_data++ = (*data++ - adjust) << 13; - } - } - } - } - - } - - /*----------------MCT-------------------*/ - if (tcd_tcp->mct) { - int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0); - fprintf(stdout,"[INFO] Tcd_encode_tile: mct\n"); - if (tcd_tcp->tccps[0].reversible == 0) { - mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples); - } else { - mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples); - } - } - /*----------------TRANSFORM---------------------------------*/ - fprintf(stdout,"[INFO] Tcd_encode_tile: Transform\n"); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - dwt_encode(tilec, tcd_tcp->tccps[compno].dwtid); - } - - /*-------------------ENTROPY CODING-----------------------------*/ - fprintf(stdout,"[INFO] Tcd_encode_tile: Entropy coding\n"); - if ((cp->encoding_format == ENCOD_2EB)||(cp->encoding_format == ENCOD_3EB)) - { - if (cp->encoding_format == ENCOD_2EB) { - opj_t1_t *t1 = NULL; - t1 = t1_create(tcd->cinfo); - t1_encode_cblks(t1, tile, tcd_tcp); - t1_destroy(t1); - } else if (cp->encoding_format == ENCOD_3EB) { - opj_t1_3d_t *t1 = NULL; - t1 = t1_3d_create(tcd->cinfo); - t1_3d_encode_cblks(t1, tile, tcd_tcp); - t1_3d_destroy(t1); - } - /*-----------RATE-ALLOCATE------------------*/ - /* INDEX */ - if(volume_info) { - volume_info->index_write = 0; - } - if (cp->disto_alloc || cp->fixed_quality) { - fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate\n"); - tcd_rateallocate(tcd, dest, len, volume_info); /* Normal Rate/distortion allocation */ - } else {/* fixed_alloc */ - fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate fixed\n"); - tcd_rateallocate_fixed(tcd); /* Fixed layer allocation */ - } - - /*--------------TIER2------------------*/ - /* INDEX */ - if(volume_info) { - volume_info->index_write = 1; - } - fprintf(stdout,"[INFO] Tcd_encode_tile: Tier - 2\n"); - t2 = t2_create(tcd->cinfo, volume, cp); - l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, volume_info); - t2_destroy(t2); - } else if ((cp->encoding_format == ENCOD_2GR)||(cp->encoding_format == ENCOD_3GR)) { - /*if(volume_info) { - volume_info->index_write = 1; - } - gr = golomb_create(tcd->cinfo, volume, cp); - l = golomb_encode(gr, tileno, tile, dest, len, volume_info); - golomb_destroy(gr);*/ - } - - - /*---------------CLEAN-------------------*/ - fprintf(stdout,"[INFO] Tcd_encode_tile: %d bytes coded\n",l); - encoding_time = opj_clock() - encoding_time; - opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time); - - /* cleaning memory */ - for (compno = 0; compno < tile->numcomps; compno++) { - tcd->tilec = &tile->comps[compno]; - opj_free(tcd->tilec->data); - } - - if (l == -999){ - fprintf(stdout,"[ERROR] Unable to perform T2 tier. Return -999.\n"); - return 0; - } - - return l; -} - - -bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) { - int l, i; - int compno, eof = 0; - double tile_time, t1_time, dwt_time; - - opj_tcd_tile_t *tile = NULL; - opj_t2_t *t2 = NULL; /* T2 component */ - - tcd->tcd_tileno = tileno; - tcd->tcd_tile = &(tcd->tcd_volume->tiles[tileno]); - tcd->tcp = &(tcd->cp->tcps[tileno]); - tile = tcd->tcd_tile; - - tile_time = opj_clock(); /* time needed to decode a tile */ - opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d / %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th * tcd->cp->tl); - - if ((tcd->cp->encoding_format == ENCOD_2EB) || (tcd->cp->encoding_format == ENCOD_3EB)) { - /*--------------TIER2------------------*/ - t2 = t2_create(tcd->cinfo, tcd->volume, tcd->cp); - l = t2_decode_packets(t2, src, len, tileno, tile); - t2_destroy(t2); - opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: %d bytes decoded\n",l); - - if (l == -999) { - eof = 1; - opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n"); - } - - /*------------------TIER1-----------------*/ - opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding %d \n",tcd->cp->encoding_format); - t1_time = opj_clock(); /* time needed to decode a tile */ - if (tcd->cp->encoding_format == ENCOD_2EB) { - opj_t1_t *t1 = NULL; /* T1 component */ - t1 = t1_create(tcd->cinfo); - t1_decode_cblks(t1, tile, tcd->tcp); - t1_destroy(t1); - }else if (tcd->cp->encoding_format == ENCOD_3EB) { - opj_t1_3d_t *t1 = NULL; /* T1 component */ - t1 = t1_3d_create(tcd->cinfo); - t1_3d_decode_cblks(t1, tile, tcd->tcp); - t1_3d_destroy(t1); - } - - t1_time = opj_clock() - t1_time; - #ifdef VERBOSE - opj_event_msg(tcd->cinfo, EVT_INFO, "- tier-1 took %f s\n", t1_time); - #endif - } else if ((tcd->cp->encoding_format == ENCOD_2GR)||(tcd->cp->encoding_format == ENCOD_3GR)) { - opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding -- Does nothing :-D\n"); - /* - gr = golomb_create(tcd->cinfo, tcd->volume, tcd->cp); - l = golomb_decode(gr, tileno, tile, src, len); - golomb_destroy(gr); - if (l == -999) { - eof = 1; - opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n"); - } - */ - } - - /*----------------DWT---------------------*/ - fprintf(stdout,"[INFO] Tcd_decode_tile: Inverse DWT\n"); - dwt_time = opj_clock(); /* time needed to decode a tile */ - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - int stops[3], dwtid[3]; - - for (i = 0; i < 3; i++) { - if (tcd->cp->reduce[i] != 0) - tcd->volume->comps[compno].resno_decoded[i] = tile->comps[compno].numresolution[i] - tcd->cp->reduce[i] - 1; - stops[i] = tilec->numresolution[i] - 1 - tcd->volume->comps[compno].resno_decoded[i]; - if (stops[i] < 0) stops[i]=0; - dwtid[i] = tcd->cp->tcps->tccps[compno].dwtid[i]; - } - - dwt_decode(tilec, stops, dwtid); - - for (i = 0; i < 3; i++) { - if (tile->comps[compno].numresolution[i] > 0) { - tcd->volume->comps[compno].factor[i] = tile->comps[compno].numresolution[i] - (tcd->volume->comps[compno].resno_decoded[i] + 1); - if ( (tcd->volume->comps[compno].factor[i]) < 0 ) - tcd->volume->comps[compno].factor[i] = 0; - } - } - } - dwt_time = opj_clock() - dwt_time; - #ifdef VERBOSE - opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time); - #endif - - /*----------------MCT-------------------*/ - - if (tcd->tcp->mct) { - if (tcd->tcp->tccps[0].reversible == 1) { - mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, - (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0)); - } else { - mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, - (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)* (tile->comps[0].z1 - tile->comps[0].z0)); - } - } - - /*---------------TILE-------------------*/ - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - opj_tcd_resolution_t *res = &tilec->resolutions[tcd->volume->comps[compno].resno_decoded[0]]; - int adjust; - int minval = tcd->volume->comps[compno].sgnd ? -(1 << (tcd->volume->comps[compno].prec - 1)) : 0; - int maxval = tcd->volume->comps[compno].sgnd ? (1 << (tcd->volume->comps[compno].prec - 1)) - 1 : (1 << tcd->volume->comps[compno].prec) - 1; - - int tw = tilec->x1 - tilec->x0; - int w = tcd->volume->comps[compno].w; - int th = tilec->y1 - tilec->y0; - int h = tcd->volume->comps[compno].h; - - int i, j, k; - int offset_x = int_ceildivpow2(tcd->volume->comps[compno].x0, tcd->volume->comps[compno].factor[0]); - int offset_y = int_ceildivpow2(tcd->volume->comps[compno].y0, tcd->volume->comps[compno].factor[1]); - int offset_z = int_ceildivpow2(tcd->volume->comps[compno].z0, tcd->volume->comps[compno].factor[2]); - - if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) { - adjust = 0; - } else { - adjust = tcd->volume->comps[compno].sgnd ? 0 : 1 << (tcd->volume->comps[compno].prec - 1); //sign=='+' --> 2^(prec-1) - if (tcd->volume->comps[compno].dcoffset != 0){ - adjust += tcd->volume->comps[compno].dcoffset; - fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",tcd->volume->comps[compno].dcoffset,adjust); - } - } - - for (k = res->z0; k < res->z1; k++) { - for (j = res->y0; j < res->y1; j++) { - for (i = res->x0; i < res->x1; i++) { - int v; - float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th]) / 8192.0); - - if (tcd->tcp->tccps[compno].reversible == 1) { - v = tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th]; - } else { - int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); - v = ((tmp < 0) ? -tmp2:tmp2); - } - v += adjust; - - tcd->volume->comps[compno].data[(i - offset_x) + (j - offset_y) * w + (k - offset_z) * w * h] = int_clamp(v, minval, maxval); - } - } - } - } - - tile_time = opj_clock() - tile_time; /* time needed to decode a tile */ - opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time); - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_free(tcd->tcd_volume->tiles[tileno].comps[compno].data); - tcd->tcd_volume->tiles[tileno].comps[compno].data = NULL; - } - - if (eof) { - return false; - } - - return true; -} - diff --git a/jp3d/libjp3dvm/tcd.h b/jp3d/libjp3dvm/tcd.h deleted file mode 100755 index 0de60b8c..00000000 --- a/jp3d/libjp3dvm/tcd.h +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __TCD_H -#define __TCD_H -/** -@file tcd.h -@brief Implementation of a tile coder/decoder (TCD) - -The functions in TCD.C have for goal to encode or decode each tile independently from -each other. The functions in TCD.C are used by some function in JP3D.C. -*/ - -/** @defgroup TCD TCD - Implementation of a tile coder/decoder */ -/*@{*/ - -/** -Tile coder/decoder: segment instance -*/ -typedef struct opj_tcd_seg { -/** Number of passes in the segment */ - int numpasses; -/** Length of information */ - int len; -/** Data */ - unsigned char *data; -/** Number of passes posible for the segment */ - int maxpasses; -/** Number of passes added to the segment */ - int numnewpasses; -/** New length after inclusion of segments */ - int newlen; -} opj_tcd_seg_t; - -/** -Tile coder/decoder: pass instance -*/ -typedef struct opj_tcd_pass { -/** Rate obtained in the pass*/ - int rate; -/** Distorsion obtained in the pass*/ - double distortiondec; - int term; -/** Length of information */ - int len; -} opj_tcd_pass_t; - -/** -Tile coder/decoder: layer instance -*/ -typedef struct opj_tcd_layer { -/** Number of passes in the layer */ - int numpasses; -/** Length of information */ - int len; -/** Distortion within layer */ - double disto; /* add for index (Cfr. Marcela) */ - unsigned char *data; /* data */ -} opj_tcd_layer_t; - -/** -Tile coder/decoder: codeblock instance -*/ -typedef struct opj_tcd_cblk { -/** Dimension of the code-blocks : left upper corner (x0, y0, z0) */ - int x0, y0, z0; -/** Dimension of the code-blocks : right low corner (x1,y1,z1) */ - int x1, y1, z1; -/** Number of bits per simbol in codeblock */ - int numbps; - int numlenbits; - int len; /* length */ -/** Number of pass already done for the code-blocks */ - int numpasses; -/** number of pass added to the code-blocks */ - int numnewpasses; -/** Number of segments */ - int numsegs; -/** Segments informations */ - opj_tcd_seg_t segs[100]; -/** Number of passes in the layer */ - int numpassesinlayers; -/** Layer information */ - opj_tcd_layer_t layers[100]; -/** Total number of passes */ - int totalpasses; -/** Information about the passes */ - opj_tcd_pass_t passes[100]; -/* Data */ - unsigned char data[524288]; - //unsigned char *data; -} opj_tcd_cblk_t; - -/** -Tile coder/decoder: precint instance -*/ -typedef struct opj_tcd_precinct { -/** Dimension of the precint : left upper corner (x0, y0, z0) */ - int x0, y0, z0; -/** Dimension of the precint : right low corner (x1,y1,z1) */ - int x1, y1, z1; -/** Number of codeblocks in precinct in width and heigth and length*/ - int cblkno[3]; -/** Information about the codeblocks */ - opj_tcd_cblk_t *cblks; -/** Inclusion tree */ - opj_tgt_tree_t *incltree; -/** Missing MSBs tree */ - opj_tgt_tree_t *imsbtree; -} opj_tcd_precinct_t; - -/** -Tile coder/decoder: subband instance -*/ -typedef struct opj_tcd_band { -/** Dimension of the subband : left upper corner (x0, y0, z0) */ - int x0, y0, z0; -/** Dimension of the subband : right low corner (x1,y1,z1) */ - int x1, y1, z1; -/** Information about the precints */ - opj_tcd_precinct_t *precincts; /* precinct information */ -/** Number of bits per symbol in band */ - int numbps; -/** Quantization stepsize associated */ - float stepsize; -/** Band orientation (O->LLL,...,7->HHH) */ - int bandno; -} opj_tcd_band_t; - -/** -Tile coder/decoder: resolution instance -*/ -typedef struct opj_tcd_resolution { -/** Dimension of the resolution level : left upper corner (x0, y0, z0) */ - int x0, y0, z0; -/** Dimension of the resolution level : right low corner (x1,y1,z1) */ - int x1, y1, z1; -/** Number of precints in each dimension for the resolution level */ - int prctno[3]; -/** Number of subbands for the resolution level */ - int numbands; -/** Subband information */ - opj_tcd_band_t *bands; -} opj_tcd_resolution_t; - -/** -Tile coder/decoder: component instance -*/ -typedef struct opj_tcd_tilecomp { -/** Dimension of the component : left upper corner (x0, y0, z0) */ - int x0, y0, z0; -/** Dimension of the component : right low corner (x1,y1,z1) */ - int x1, y1, z1; -/** Number of resolutions level if DWT transform*/ - int numresolution[3]; -/** Resolution information */ - opj_tcd_resolution_t *resolutions; -/** Data of the component */ - int *data; -/** Fixed_quality related */ - int nbpix; -/** Number of bits per voxel in component */ - int bpp; -} opj_tcd_tilecomp_t; - -/** -Tile coder/decoder: tile instance -*/ -typedef struct opj_tcd_tile { -/** Dimension of the tile : left upper corner (x0, y0, z0) */ - int x0, y0, z0; -/** Dimension of the tile : right low corner (x1,y1,z1) */ - int x1, y1, z1; -/** Number of components in tile */ - int numcomps; -/** Components information */ - opj_tcd_tilecomp_t *comps; -/** Fixed_quality related : no of bytes of data*/ - int nbpix; -/** Fixed_quality related : distortion achieved in tile */ - double distotile; -/** Fixed_quality related : distortion achieved in each layer */ - double distolayer[100]; -} opj_tcd_tile_t; - -/** -Tile coder/decoder: volume instance -*/ -typedef struct opj_tcd_volume { -/** Number of tiles in width and heigth and length */ - int tw, th, tl; -/** Tiles information */ - opj_tcd_tile_t *tiles; -} opj_tcd_volume_t; - -/** -Tile coder/decoder -*/ -typedef struct opj_tcd { -/** Codec context */ - opj_common_ptr cinfo; -/** Volume information */ - opj_volume_t *volume; -/** Coding parameters */ - opj_cp_t *cp; -/** Coding/decoding parameters common to all tiles */ - opj_tcp_t *tcp; -/** Info on each volume tile */ - opj_tcd_volume_t *tcd_volume; -/** Pointer to the current encoded/decoded tile */ - opj_tcd_tile_t *tcd_tile; -/** Current encoded/decoded tile */ - int tcd_tileno; - - /**@name working variables */ - /*@{*/ - opj_tcd_tile_t *tile; - opj_tcd_tilecomp_t *tilec; - opj_tcd_resolution_t *res; - opj_tcd_band_t *band; - opj_tcd_precinct_t *prc; - opj_tcd_cblk_t *cblk; - /*@}*/ -} opj_tcd_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ - -/** -Dump the content of a tcd structure -*/ -void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t *img); -/** -Create a new TCD handle -@param cinfo Codec context info -@return Returns a new TCD handle if successful returns NULL otherwise -*/ -opj_tcd_t* tcd_create(opj_common_ptr cinfo); -/** -Destroy a previously created TCD handle -@param tcd TCD handle to destroy -*/ -void tcd_destroy(opj_tcd_t *tcd); -/** -Initialize the tile coder (allocate the memory) -@param tcd TCD handle -@param volume Raw volume -@param cp Coding parameters -@param curtileno Number that identifies the tile that will be encoded -*/ -void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno); -/** -Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)(for 3D-DWT) -@param tcd TCD handle -@param volume Raw volume -@param cp Coding parameters -@param curtileno Number that identifies the tile that will be encoded -*/ -void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno); -/** -Free the memory allocated for encoding -@param tcd TCD handle -*/ -void tcd_free_encode(opj_tcd_t *tcd); -/** -Initialize the tile decoder -@param tcd TCD handle -@param volume Raw volume -@param cp Coding parameters -*/ -void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp); - -void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final); -void tcd_rateallocate_fixed(opj_tcd_t *tcd); -void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final); -bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_volume_info_t * volume_info); -/** -Encode a tile from the raw volume into a buffer -@param tcd TCD handle -@param tileno Number that identifies one of the tiles to be encoded -@param dest Destination buffer -@param len Length of destination buffer -@param volume_info Creation of index file -@return -*/ -int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_volume_info_t * volume_info); -/** -Decode a tile from a buffer into a raw volume -@param tcd TCD handle -@param src Source buffer -@param len Length of source buffer -@param tileno Number that identifies one of the tiles to be decoded -*/ -bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno); -/** -Free the memory allocated for decoding -@param tcd TCD handle -*/ -void tcd_free_decode(opj_tcd_t *tcd); - -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __TCD_H */ diff --git a/jp3d/libjp3dvm/tgt.c b/jp3d/libjp3dvm/tgt.c deleted file mode 100755 index 9398ea94..00000000 --- a/jp3d/libjp3dvm/tgt.c +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -/* -========================================================== - Tag-tree coder interface -========================================================== -*/ -void tgt_tree_dump (FILE *fd, opj_tgt_tree_t * tree){ - int nodesno; - - fprintf(fd, "TGT_TREE {\n"); - fprintf(fd, " numnodes: %d \n", tree->numnodes); - fprintf(fd, " numleafsh: %d, numleafsv: %d, numleafsz: %d,\n", tree->numleafsh, tree->numleafsv, tree->numleafsz); - - for (nodesno = 0; nodesno < tree->numnodes; nodesno++) { - fprintf(fd, "tgt_node %d {\n", nodesno); - fprintf(fd, " value: %d \n", tree->nodes[nodesno].value); - fprintf(fd, " low: %d \n", tree->nodes[nodesno].low); - fprintf(fd, " known: %d \n", tree->nodes[nodesno].known); - if (tree->nodes[nodesno].parent) { - fprintf(fd, " parent.value: %d \n", tree->nodes[nodesno].parent->value); - fprintf(fd, " parent.low: %d \n", tree->nodes[nodesno].parent->low); - fprintf(fd, " parent.known: %d \n", tree->nodes[nodesno].parent->known); - } - fprintf(fd, "}\n"); - - } - fprintf(fd, "}\n"); - -} - - -opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz) { - - int nplh[32]; - int nplv[32]; - int nplz[32]; - opj_tgt_node_t *node = NULL; - opj_tgt_node_t *parentnode = NULL; - opj_tgt_node_t *parentnode0 = NULL; - opj_tgt_tree_t *tree = NULL; - int i, j, k, p, p0; - int numlvls; - int n, z = 0; - - tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t)); - if(!tree) - return NULL; - tree->numleafsh = numleafsh; - tree->numleafsv = numleafsv; - tree->numleafsz = numleafsz; - - numlvls = 0; - nplh[0] = numleafsh; - nplv[0] = numleafsv; - nplz[0] = numleafsz; - tree->numnodes = 0; - do { - n = nplh[numlvls] * nplv[numlvls] * nplz[numlvls]; - nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; - nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; - nplz[numlvls + 1] = (nplz[numlvls] + 1) / 2; - tree->numnodes += n; - ++numlvls; - } while (n > 1); - - if (tree->numnodes == 0) { - opj_free(tree); - return NULL; - } - - tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t)); - if(!tree->nodes) { - opj_free(tree); - return NULL; - } - - node = tree->nodes; - parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv * tree->numleafsz]; - parentnode0 = parentnode; - - p = tree->numleafsh * tree->numleafsv * tree->numleafsz; - p0 = p; - n = 0; - //fprintf(stdout,"\nH %d V %d Z %d numlvls %d nodes %d\n",tree->numleafsh,tree->numleafsv,tree->numleafsz,numlvls,tree->numnodes); - for (i = 0; i < numlvls - 1; ++i) { - for (j = 0; j < nplv[i]; ++j) { - k = nplh[i]*nplz[i]; - while (--k >= 0) { - node->parent = parentnode; //fprintf(stdout,"node[%d].parent = node[%d]\n",n,p); - ++node; ++n; - if (--k >= 0 && n < p) { - node->parent = parentnode; //fprintf(stdout,"node[%d].parent = node[%d]\n",n,p); - ++node; ++n; - } - if (nplz[i] != 1){ //2D operation vs 3D operation - if (--k >= 0 && n < p) { - node->parent = parentnode; //fprintf(stdout,"node[%d].parent = node[%d]\n",n,p); - ++node; ++n; - } - if (--k >= 0 && n < p) { - node->parent = parentnode; //fprintf(stdout,"node[%d].parent = node[%d]\n",n,p); - ++node; ++n; - } - } - ++parentnode; ++p; - } - if ((j & 1) || j == nplv[i] - 1) { - parentnode0 = parentnode; p0 = p; //fprintf(stdout,"parent = node[%d] \n",p); - } else { - parentnode = parentnode0; p = p0; //fprintf(stdout,"parent = node[%d] \n",p); - parentnode0 += nplh[i]*nplz[i]; p0 += nplh[i]*nplz[i]; - } - } - } - node->parent = 0; - - - tgt_reset(tree); - - return tree; -} - -void tgt_destroy(opj_tgt_tree_t *tree) { - opj_free(tree->nodes); - opj_free(tree); -} - -void tgt_reset(opj_tgt_tree_t *tree) { - int i; - - if (NULL == tree) - return; - - for (i = 0; i < tree->numnodes; i++) { - tree->nodes[i].value = 999; - tree->nodes[i].low = 0; - tree->nodes[i].known = 0; - } -} - -void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) { - opj_tgt_node_t *node; - node = &tree->nodes[leafno]; - while (node && node->value > value) { - node->value = value; - node = node->parent; - } -} - -void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) { - opj_tgt_node_t *stk[31]; - opj_tgt_node_t **stkptr; - opj_tgt_node_t *node; - int low; - - stkptr = stk; - node = &tree->nodes[leafno]; - while (node->parent) { - *stkptr++ = node; - node = node->parent; - } - - low = 0; - for (;;) { - if (low > node->low) { - node->low = low; - } else { - low = node->low; - } - - while (low < threshold) { - if (low >= node->value) { - if (!node->known) { - bio_write(bio, 1, 1); - node->known = 1; - } - break; - } - bio_write(bio, 0, 1); - ++low; - } - - node->low = low; - if (stkptr == stk) - break; - node = *--stkptr; - } -} - -int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) { - opj_tgt_node_t *stk[31]; - opj_tgt_node_t **stkptr; - opj_tgt_node_t *node; - int low; - - stkptr = stk; - node = &tree->nodes[leafno]; - while (node->parent) { - *stkptr++ = node; - node = node->parent; - } - - low = 0; - for (;;) { - if (low > node->low) { - node->low = low; - } else { - low = node->low; - } - while (low < threshold && low < node->value) { - if (bio_read(bio, 1)) { - node->value = low; - } else { - ++low; - } - } - node->low = low; - if (stkptr == stk) { - break; - } - node = *--stkptr; - } - - return (node->value < threshold) ? 1 : 0; -} diff --git a/jp3d/libjp3dvm/tgt.h b/jp3d/libjp3dvm/tgt.h deleted file mode 100755 index ad27f113..00000000 --- a/jp3d/libjp3dvm/tgt.h +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2001-2003, David Janssens - * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef __TGT_H -#define __TGT_H -/** -@file tgt.h -@brief Implementation of a tag-tree coder (TGT) - -The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C -are used by some function in T2.C. -*/ - -/** @defgroup TGT TGT - Implementation of a tag-tree coder */ -/*@{*/ - -/** -Tag node -*/ -typedef struct opj_tgt_node { -/** Node parent reference */ - struct opj_tgt_node *parent; -/** */ - int value; -/** */ - int low; -/** */ - int known; -} opj_tgt_node_t; - -/** -Tag tree -*/ -typedef struct opj_tgt_tree { -/** Number of leaves from horizontal axis */ - int numleafsh; -/** Number of leaves from vertical axis */ - int numleafsv; -/** Number of leaves from axial axis */ - int numleafsz; -/** Number of nodes */ - int numnodes; -/** Reference to each node instance */ - opj_tgt_node_t *nodes; -} opj_tgt_tree_t; - -/** @name Funciones generales */ -/*@{*/ -/* ----------------------------------------------------------------------- */ -/** -Create a tag-tree -@param numleafsh Width of the array of leafs of the tree -@param numleafsv Height of the array of leafs of the tree -@param numleafsz Depth of the array of leafs of the tree -@return Returns a new tag-tree if successful, returns NULL otherwise -*/ -opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz); -/** -Destroy a tag-tree, liberating memory -@param tree Tag-tree to destroy -*/ -void tgt_destroy(opj_tgt_tree_t *tree); -/** -Reset a tag-tree (set all leaves to 0) -@param tree Tag-tree to reset -*/ -void tgt_reset(opj_tgt_tree_t *tree); -/** -Set the value of a leaf of a tag-tree -@param tree Tag-tree to modify -@param leafno Number that identifies the leaf to modify -@param value New value of the leaf -*/ -void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value); -/** -Encode the value of a leaf of the tag-tree up to a given threshold -@param bio Pointer to a BIO handle -@param tree Tag-tree to modify -@param leafno Number that identifies the leaf to encode -@param threshold Threshold to use when encoding value of the leaf -*/ -void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold); -/** -Decode the value of a leaf of the tag-tree up to a given threshold -@param bio Pointer to a BIO handle -@param tree Tag-tree to decode -@param leafno Number that identifies the leaf to decode -@param threshold Threshold to use when decoding value of the leaf -@return Returns 1 if the node's value < threshold, returns 0 otherwise -*/ -int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold); - -/*@}*/ -/* ----------------------------------------------------------------------- */ -void tgt_tree_dump (FILE *fd, opj_tgt_tree_t * tree); - -#endif /* __TGT_H */ diff --git a/jp3d/libjp3dvm/volume.c b/jp3d/libjp3dvm/volume.c deleted file mode 100755 index 9066e390..00000000 --- a/jp3d/libjp3dvm/volume.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "opj_includes.h" - -opj_volume_t* OPJ_CALLCONV opj_volume_create(int numcmpts, opj_volume_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { - int compno; - opj_volume_t *volume = NULL; - - volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t)); - if(volume) { - volume->color_space = clrspc; - volume->numcomps = numcmpts; - /* allocate memory for the per-component information */ - volume->comps = (opj_volume_comp_t*)opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t)); - if(!volume->comps) { - opj_volume_destroy(volume); - return NULL; - } - /* create the individual volume components */ - for(compno = 0; compno < numcmpts; compno++) { - opj_volume_comp_t *comp = &volume->comps[compno]; - comp->dx = cmptparms[compno].dx; - comp->dy = cmptparms[compno].dy; - comp->dz = cmptparms[compno].dz; - comp->w = cmptparms[compno].w; - comp->h = cmptparms[compno].h; - comp->l = cmptparms[compno].l; - comp->x0 = cmptparms[compno].x0; - comp->y0 = cmptparms[compno].y0; - comp->z0 = cmptparms[compno].z0; - comp->prec = cmptparms[compno].prec; - comp->bpp = cmptparms[compno].bpp; - comp->sgnd = cmptparms[compno].sgnd; - comp->bigendian = cmptparms[compno].bigendian; - comp->dcoffset = cmptparms[compno].dcoffset; - comp->data = (int*)opj_malloc(comp->w * comp->h * comp->l * sizeof(int)); - if(!comp->data) { - fprintf(stdout,"Unable to malloc comp->data (%d x %d x %d x bytes)",comp->w,comp->h,comp->l); - opj_volume_destroy(volume); - return NULL; - } - //fprintf(stdout,"%d %d %d %d %d %d %d %d %d", comp->w,comp->h, comp->l, comp->dx, comp->dy, comp->dz, comp->prec, comp->bpp, comp->sgnd); - } - } - - return volume; -} - -void OPJ_CALLCONV opj_volume_destroy(opj_volume_t *volume) { - int i; - if(volume) { - if(volume->comps) { - /* volume components */ - for(i = 0; i < volume->numcomps; i++) { - opj_volume_comp_t *volume_comp = &volume->comps[i]; - if(volume_comp->data) { - opj_free(volume_comp->data); - } - } - opj_free(volume->comps); - } - opj_free(volume); - } -} - diff --git a/jp3d/libjp3dvm/volume.h b/jp3d/libjp3dvm/volume.h deleted file mode 100755 index 737bde79..00000000 --- a/jp3d/libjp3dvm/volume.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2005, Hervé Drolon, FreeImage Team - * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __VOLUME_H -#define __VOLUME_H -/** -@file volume.h -@brief Implementation of operations on volumes (VOLUME) - -The functions in VOLUME.C have for goal to realize operations on volumes. -*/ - -/** @defgroup VOLUME VOLUME - Implementation of operations on volumes */ -/*@{*/ - - -/*@}*/ - -#endif /* __VOLUME_H */ - diff --git a/jp3d/tcltk/LPI_JP3D_VM.tcl b/jp3d/tcltk/LPI_JP3D_VM.tcl deleted file mode 100755 index eb184de7..00000000 --- a/jp3d/tcltk/LPI_JP3D_VM.tcl +++ /dev/null @@ -1,114 +0,0 @@ -#!/bin/sh -# The next line is executed by /bin/sh, but not tcl \ -exec wish "$0" ${1+"$@"} - -namespace eval jp3dVM { - - variable _progress 0 - variable _afterid "" - variable _status "Compute in progress..." - variable notebook - variable mainframe - variable dataout "Process execution information" - variable status - variable prgtext - variable prgindic - - set pwd [pwd] - cd [file dirname [info script]] - variable VMDIR [pwd] - cd $pwd - - foreach script {encoder.tcl decoder.tcl} { - namespace inscope :: source $VMDIR/$script - } -} - - -proc jp3dVM::create { } { - variable notebook - variable mainframe - variable dataout - - bind all { catch {console show} } - - # Menu description - set descmenu { - "&File" {} {} 0 { - {command "E&xit" {} "Exit BWidget jp3dVM" {} -command exit} - } - "&Options" {} {} 0 { - {command "&Encode" {} "Show encoder" {} - -command {$jp3dVM::notebook raise [$jp3dVM::notebook page 0]} - } - {command "&Decode" {} "Show decoder" {} - -command {$jp3dVM::notebook raise [$jp3dVM::notebook page 1]} - } - } - "&Help" {} {} 0 { - {command "&About authors..." {} "Show info about authors" {} - -command {MessageDlg .msgdlg -parent . -title "About authors" -message " Copyright @ LPI-UVA 2006 " -type ok -icon info}} - } - } - - set mainframe [MainFrame .mainframe \ - -menu $descmenu \ - -textvariable jp3dVM::status \ - -progressvar jp3dVM::prgindic] - - $mainframe addindicator -text "JP3D Verification Model 1.0.0" - - # NoteBook creation - set frame [$mainframe getframe] - set notebook [NoteBook $frame.nb] - - set logo [frame $frame.logo] - #creo imagen logo - image create photo LPIimg -file logoLPI.gif - set logoimg [Label $logo.logoimg -image LPIimg] - - set f0 [VMEncoder::create $notebook] - set f1 [VMDecoder::create $notebook] - - set tfinfo [TitleFrame $frame.codinfo -text "Program Execution"] - set codinfo [$tfinfo getframe] - set sw [ScrolledWindow $codinfo.sw -relief sunken -borderwidth 2 -scrollbar both] - set sf [ScrollableFrame $codinfo.sf ] - $sw setwidget $sf - set subf [$sf getframe] - set labinfo [label $subf.labinfo -textvariable jp3dVM::dataout -justify left] - - pack $labinfo -side left - pack $sw - - $notebook compute_size - $notebook raise [$notebook page 0] - - pack $logoimg -side left -fill x -expand yes - pack $notebook -expand yes - pack $logo $tfinfo -side left -expand yes - pack $mainframe -fill both -expand yes - update idletasks -} - - -proc jp3dVM::main {} { - variable VMDIR - - lappend ::auto_path [file dirname $VMDIR] - namespace inscope :: package require BWidget - - option add *TitleFrame.l.font {helvetica 11 bold italic} - - wm withdraw . - wm title . "JP3D Verification Model @ LPI" - - jp3dVM::create - BWidget::place . 0 0 center - wm deiconify . - raise . - focus -force . -} - -jp3dVM::main -wm geom . [wm geom .] diff --git a/jp3d/tcltk/Thumbs.db b/jp3d/tcltk/Thumbs.db deleted file mode 100755 index 5d6ffdf9..00000000 Binary files a/jp3d/tcltk/Thumbs.db and /dev/null differ diff --git a/jp3d/tcltk/decoder.tcl b/jp3d/tcltk/decoder.tcl deleted file mode 100755 index 6fadbb18..00000000 --- a/jp3d/tcltk/decoder.tcl +++ /dev/null @@ -1,272 +0,0 @@ - -namespace eval VMDecoder { - variable var - variable JP3Ddecoder "../bin/jp3d_vm_dec.exe" - #variable JP3Ddecoder "jp3d_vm_dec.exe" -} - - -proc VMDecoder::create { nb } { - variable var - - set frameD [$nb insert end VMDecoder -text "Decoder"] - set topfD [frame $frameD.topfD] - set medfD [frame $frameD.medfD] - set bottomfD [frame $frameD.bottomfD] - set srcfD [TitleFrame $topfD.srcfD -text "Source"] - set dstfD [TitleFrame $topfD.dstfD -text "Destination"] - set paramfD [TitleFrame $medfD.paramfD -text "Decoding parameters"] - set infofD [TitleFrame $medfD.infofD -text "Distortion measures"] - - set frame1 [$srcfD getframe] - _sourceD $frame1 - set frame2 [$dstfD getframe] - _destinationD $frame2 - set frame3 [$infofD getframe] - _originalD $frame3 - set frame4 [$paramfD getframe] - _paramsD $frame4 - - set butD [Button $bottomfD.butD -text "Decode!" \ - -command "VMDecoder::_decode $frame1 $frame2 $frame3" \ - -helptext "Decoding trigger button"] - set butR [Button $bottomfD.butR -text "Save info" \ - -command "VMDecoder::_save $frame3" \ - -helptext "Save information"] - - pack $srcfD $dstfD -side left -fill both -padx 10 -ipadx 5 -expand yes - pack $topfD -pady 4 -fill x - - pack $paramfD $infofD -side left -fill both -padx 10 -pady 2 -ipadx 5 -expand yes - pack $medfD -pady 4 -fill x - - pack $butD $butR -side left -padx 4 -pady 5 -expand yes - pack $bottomfD -pady 4 -fill x - -return $frameD -} - - -proc fileDialogD {w ent operation} { - - variable file - - if {$operation == "open"} { - #-----Type names---------Extension(s)--- - set types { - {"JP3D Files" {.jp3d} } - {"All files" *} - } - set file [tk_getOpenFile -filetypes $types -parent $w ] - } elseif {$operation == "original"} { - #-----Type names---------Extension(s)--- - set types { - {"BIN Raw Image Files" {.bin} } - {"PGX Raw Image Files" {.pgx} } - {"All files" *} - } - set file [tk_getOpenFile -filetypes $types -parent $w ] - } else { - #-----Type names---------Extension(s)--- - set types { - {"BIN Raw Image Files" {.bin} } - {"PGX Raw Image Files" {.pgx} } - {"All files" *} - } - set file [tk_getSaveFile -filetypes $types -parent $w -initialfile Untitled -defaultextension "*.bin"] - } - if {[string compare $file ""]} { - $ent delete 0 end - $ent insert end $file - $ent xview moveto 1 - } -} - -proc VMDecoder::_sourceD { parent } { - - variable var - - set labsrcD [LabelFrame $parent.labsrcD -text "Select compressed file: " -side top \ - -anchor w -relief flat -borderwidth 0] - set subsrcD [$labsrcD getframe] - set listD [entry $subsrcD.entrysrcD -width 40 -textvariable VMDecoder::var(sourceD)] - - set labbrw [LabelFrame $parent.labbrw -side top -anchor w -relief flat -borderwidth 0] - set subbrw [$labbrw getframe] - set butbrw [button $subbrw.butbrw -image [Bitmap::get open] \ - -relief raised -borderwidth 1 -padx 1 -pady 1 \ - -command "fileDialogD . $subsrcD.entrysrcD open"] - - pack $listD -side top - pack $butbrw -side top - pack $labsrcD $labbrw -side left -fill both -expand yes - - -} - -proc VMDecoder::_destinationD { parent } { - - variable var - - set labdstD [LabelFrame $parent.labdstD -text "Save decompressed volume file(s) as: " -side top \ - -anchor w -relief flat -borderwidth 0] - set subdstD [$labdstD getframe] - set listD [entry $subdstD.entrydstD -width 40 -textvariable VMDecoder::var(destinationD)] - - set labbrw [LabelFrame $parent.labbrw -side top -anchor w -relief flat -borderwidth 0] - set subbrw [$labbrw getframe] - set butbrw [button $subbrw.butbrw -image [Bitmap::get save] \ - -relief raised -borderwidth 1 -padx 1 -pady 1 \ - -command "fileDialogD . $subdstD.entrydstD save"] - - pack $listD -side top - pack $butbrw -side top - pack $labdstD $labbrw -side left -fill both -expand yes -} - -proc VMDecoder::_originalD { parent } { - - variable var - - set laborgD [LabelFrame $parent.laborgD -text "Select original file: " -side top \ - -anchor w -relief flat -borderwidth 0] - set suborgD [$laborgD getframe] - set listorgD [entry $suborgD.entryorgD -width 30 -textvariable VMDecoder::var(originalD)] - - set labbrw2 [LabelFrame $parent.labbrw2 -side top -anchor w -relief flat -borderwidth 0] - set subbrw2 [$labbrw2 getframe] - set butbrw2 [button $subbrw2.butbrw2 -image [Bitmap::get open] \ - -relief raised -borderwidth 1 -padx 1 -pady 1 \ - -command "fileDialogD . $suborgD.entryorgD original"] - - set infoD [Label $parent.infoD -relief sunken -textvariable VMDecoder::var(decodinfo) -justify left] - - pack $listorgD -side left -anchor n - pack $butbrw2 -side left -anchor n - pack $infoD -side bottom -anchor nw -pady 4 -ipadx 150 -ipady 20 -expand yes - pack $laborgD $labbrw2 -side left -fill both - - -} - -proc VMDecoder::_paramsD { parent } { - - variable var - - ########### DECODING ############# - set labcod [LabelFrame $parent.labcod -side top -anchor w -relief sunken -borderwidth 1] - set subcod [$labcod getframe] - - set frameres [frame $subcod.frameres -borderwidth 1] - set labres [LabelEntry $frameres.labres -label "Resolutions to discard: " -labelwidth 20 -labelanchor w \ - -textvariable VMDecoder::var(resdiscard) -editable 1 \ - -helptext "Number of highest resolution levels to be discarded on each dimension" ] - set VMDecoder::var(resdiscard) "0,0,0" - - set framelayer [frame $subcod.framelayer -borderwidth 1] - set lablayer [LabelEntry $framelayer.lablayer -label "Layers to decode: " -labelwidth 20 -labelanchor w \ - -textvariable VMDecoder::var(layer) -editable 1 \ - -helptext "Maximum number of quality layers to decode" ] - set VMDecoder::var(layer) "All" - - set framebe [frame $subcod.framebe -borderwidth 1] - set chkbe [checkbutton $framebe.chkbe -text "Write decoded file with BigEndian byte order" \ - -variable VMDecoder::var(be) -onvalue 1 -offvalue 0 ] - - pack $labres -side left -padx 2 -anchor n - pack $lablayer -side left -padx 2 -anchor n - pack $chkbe -side left -padx 2 -anchor w - pack $frameres $framelayer $framebe -side top -anchor w - - pack $subcod -anchor n - pack $labcod -side left -fill both -padx 4 -expand yes -} - - -proc VMDecoder::_decode { framesrc framedst frameinfo} { - - variable var - - set sourceD [$framesrc.labsrcD.f.entrysrcD get ] - set destinationD [$framedst.labdstD.f.entrydstD get ] - set originD [$frameinfo.laborgD.f.entryorgD get ] - set cond1 [string match *.pgx [string tolower $destinationD]] - set cond2 [string match *\**.pgx [string tolower $destinationD]] - set cond3 [string match *.bin [string tolower $destinationD]] - - #comprobamos datos son correctos - if {($cond1 == 1) && ($cond2 == 0)} { - set pgx "*.pgx" - set pattern [string range $destinationD 0 [expr [string length $destinationD]-5]] - set destinationD $pattern$img - } elseif {$sourceD == ""} { - MessageDlg .msgdlg -parent . -message "Error : Source file is not defined !" -type ok -icon error - } elseif {$destinationD == ""} { - MessageDlg .msgdlg -parent . -message "Error : Destination file is not defined !" -type ok -icon error - } else { - - #creamos datain a partir de los parametros de entrada - #set dirJP3Ddecoder [mk_relativepath $VMDecoder::JP3Ddecoder] - set dirJP3Ddecoder $VMDecoder::JP3Ddecoder - set datain [concat " $dirJP3Ddecoder -i [mk_relativepath $sourceD] "] - set datain [concat " $datain -o [mk_relativepath $destinationD] "] - if {$originD != ""} { - set datain [concat " $datain -O [mk_relativepath $originD] "] - if {$cond3 == 1} { - set img ".img" - set pattern [string range $originD 0 [expr [string length $originD]-5]] - set pattern $pattern$img - if {[file exists $pattern]} { - set datain [concat " $datain -m [mk_relativepath $pattern] "] - } else { - MessageDlg .msgdlg -parent . -message "Error : IMG file associated to original BIN volume file not found in same directory !" -type ok -icon info - } - } - } - if {$VMDecoder::var(resdiscard) != "0,0,0"} { - set datain [concat " $datain -r $VMDecoder::var(resdiscard) "] - } - if {$VMDecoder::var(layer) != "All" && $VMDecoder::var(layer) > 0} { - set datain [concat " $datain -l $VMDecoder::var(layer) "] - } - if {$VMDecoder::var(be) == 1} { - set datain [concat " $datain -BE"] - } - - set VMDecoder::var(progval) 10 - ProgressDlg .progress -parent . -title "Wait..." \ - -type infinite \ - -width 20 \ - -textvariable "Compute in progress..."\ - -variable VMDecoder::progval \ - -stop "Stop" \ - -command {destroy .progress} - - after 200 set VMDecoder::var(progval) 2 - - set fp [open "| $datain " r+] - fconfigure $fp -buffering line - set jp3dVM::dataout [concat "EXECUTED PROGRAM:\n\t$datain"] - while {-1 != [gets $fp tmp]} { - set jp3dVM::dataout [concat "$jp3dVM::dataout\n$tmp"] - } - close $fp - destroy .progress - set cond [string first "ERROR" $jp3dVM::dataout] - set cond2 [string first "PSNR" $jp3dVM::dataout] - set cond3 [string first "RESULT" $jp3dVM::dataout] - if {$cond != -1} { - MessageDlg .msgdlg -parent . -message [string range $jp3dVM::dataout [expr $cond-1] end] -type ok -icon error - } elseif {$cond3 != -1} { - if {$cond2 != -1} { - set VMDecoder::var(decodinfo) [string range $jp3dVM::dataout [expr $cond2-1] end] - } - MessageDlg .msgdlg -parent . -message [string range $jp3dVM::dataout [expr $cond3-1] end] -type ok -icon info - } - } -} - -proc VMDecoder::_save { frameinfo } { - -} - diff --git a/jp3d/tcltk/encoder.tcl b/jp3d/tcltk/encoder.tcl deleted file mode 100755 index dc174b71..00000000 --- a/jp3d/tcltk/encoder.tcl +++ /dev/null @@ -1,470 +0,0 @@ - -namespace eval VMEncoder { - variable var - variable JP3Dencoder "../bin/jp3d_vm_enc.exe" -} - -proc VMEncoder::create { nb } { - - set frame [$nb insert end VMEncoder -text "Encoder"] - set topf [frame $frame.topf] - set midf [frame $frame.midf] - set bottomf [frame $frame.bottomf] - set srcf [TitleFrame $topf.srcf -text "Source"] - set dstf [TitleFrame $topf.dstf -text "Destination"] - set Tparf [TitleFrame $midf.parfT -text "Transform Parameters"] - set Cparf [TitleFrame $midf.parfC -text "Coding Parameters"] - - set frame1 [$srcf getframe] - VMEncoder::_sourceE $frame1 - - set frame2 [$dstf getframe] - VMEncoder::_destinationE $frame2 - - set frame3 [$Tparf getframe] - VMEncoder::_transformE $frame3 - - set frame4 [$Cparf getframe] - VMEncoder::_codingE $frame4 - - set butE [Button $bottomf.butE -text "Encode!" \ - -command "VMEncoder::_encode $frame1 $frame2" \ - -helptext "Encoding trigger button"] - set butR [Button $bottomf.butR -text "Restore defaults" \ - -command "VMEncoder::_reset $frame1 $frame2 $frame3 $frame4" \ - -helptext "Reset to default values"] - - pack $srcf $dstf -side left -fill y -padx 4 -expand yes - pack $topf -pady 2 -fill x - - pack $Tparf $Cparf -side left -fill both -padx 4 -expand yes - pack $midf -pady 2 -fill x - - pack $butE $butR -side left -padx 40 -pady 5 -fill y -expand yes - pack $bottomf -pady 2 -fill x - - return $frame -} - -proc VMEncoder::_sourceE { parent } { - - variable var - - set labsrc [LabelFrame $parent.labsrc -text "Select volume file to encode: " -side top \ - -anchor w -relief flat -borderwidth 0] - set subsrc [$labsrc getframe] - set list [entry $subsrc.entrysrc -width 30 -textvariable VMDecoder::var(source)] - - set labbrw [LabelFrame $parent.labbrw -side top -anchor w -relief flat -borderwidth 0] - set subbrw [$labbrw getframe] - set butbrw [button $subbrw.butbrw -image [Bitmap::get open] \ - -relief raised -borderwidth 1 -padx 1 -pady 1 \ - -command "fileDialogE . $subsrc.entrysrc open"] - - pack $list -side top - pack $butbrw -side top - pack $labsrc $labbrw -side left -fill both -expand yes -} - -proc VMEncoder::_destinationE { parent } { - - variable var - - set labdst [LabelFrame $parent.labdst -text "Save compressed volume as: " -side top \ - -anchor w -relief flat -borderwidth 0] - set subdst [$labdst getframe] - set list [entry $subdst.entrydst -width 30 -textvariable VMDecoder::var(destination)] - - set labbrw [LabelFrame $parent.labbrw -side top -anchor w -relief flat -borderwidth 0] - set subbrw [$labbrw getframe] - set butbrw [button $subbrw.butbrw -image [Bitmap::get save] \ - -relief raised -borderwidth 1 -padx 1 -pady 1 \ - -command "fileDialogE . $subdst.entrydst save"] - - pack $list -side top - pack $butbrw -side top - pack $labdst $labbrw -side left -fill both -expand yes -} - -proc VMEncoder::_codingE { parent } { - - - ########### CODING ############# - set labcod [LabelFrame $parent.labcod -side top -anchor w -relief sunken -borderwidth 1] - set subcod [$labcod getframe] - - set framerate [frame $subcod.framerate -borderwidth 1] - set labrate [LabelEntry $framerate.labrate -label "Rates: " -labelwidth 9 -labelanchor w \ - -textvariable VMEncoder::var(rate) -editable 1 \ - -helptext "Compression ratios for different layers (R1, R2, R3,...). If R=1, lossless coding" ] - set VMEncoder::var(rate) "1" - - set framecblk [frame $subcod.framecblk -borderwidth 1] - set labcblk [LabelEntry $framecblk.labcblk -label "Codeblock: " -labelwidth 9 -labelanchor w \ - -textvariable VMEncoder::var(cblksize) -editable 1 \ - -helptext "Codeblock size (X, Y, Z)" ] - set VMEncoder::var(cblksize) "64,64,64" - - set frametile [frame $subcod.frametile -borderwidth 1] - set labtile [LabelEntry $frametile.labtile -label "Tile size: " -labelwidth 9 -labelanchor w \ - -textvariable VMEncoder::var(tilesize) -editable 1 \ - -helptext "Tile size (X, Y, Z)" ] - set VMEncoder::var(tilesize) "512,512,512" - - set framesop [frame $subcod.framesop -borderwidth 1] - set chksop [checkbutton $framesop.chksop -text "Write SOP marker" \ - -variable VMEncoder::var(sop) -onvalue 1 -offvalue 0 ] - set frameeph [frame $subcod.frameeph -borderwidth 1] - set chkeph [checkbutton $frameeph.chkeph -text "Write EPH marker" \ - -variable VMEncoder::var(eph) -onvalue 1 -offvalue 0 ] - - set framepoc [frame $subcod.framepoc -borderwidth 1] - set labpoc [label $framepoc.labpoc -text "Progression order: " ] - set progorder [ComboBox $framepoc.progorder \ - -text {Choose a progression order} \ - -width 10 \ - -textvariable VMEncoder::var(progorder) \ - -values {"LRCP" "RLCP" "RPCL" "PCRL" "CPRL"} \ - -helptext "Progression order"] - set VMEncoder::var(progorder) "LRCP" - - pack $labrate -side left -padx 2 -anchor n - pack $labcblk -side left -padx 2 -anchor n - pack $labpoc $progorder -side left -padx 2 -anchor w - #pack $labtile -side left -padx 2 -anchor n - pack $chksop -side left -padx 2 -anchor w - pack $chkeph -side left -padx 2 -anchor w - ########### ENTROPY CODING ############# - set labent [LabelFrame $parent.labent -text "Entropy Coding" -side top -anchor w -relief sunken -borderwidth 1] - set subent [$labent getframe] - foreach entval {2EB 3EB} entropy {2D_EBCOT 3D_EBCOT} { - set rad [radiobutton $subent.$entval \ - -text $entropy \ - -variable VMEncoder::var(encoding) \ - -command "disableGR $entval $labcblk $progorder $labrate $chksop $chkeph" \ - -value $entval ] - pack $rad -anchor w - } - $subent.2EB select - - pack $subent -padx 2 -anchor n - - pack $framerate $framecblk $framepoc $framesop $frameeph -side top -anchor w - pack $subcod -anchor n - - pack $labent $labcod -side left -fill both -padx 4 -expand yes - - -} - -proc VMEncoder::_transformE { parent } { - - variable var - - ########### TRANSFORM ############# - set labtrf [LabelFrame $parent.labtrf -text "Transform" -side top -anchor w -relief sunken -borderwidth 1] - set subtrf [$labtrf getframe] - set labres [LabelFrame $parent.labres -side top -anchor w -relief sunken -borderwidth 1] - set subres [$labres getframe] - - ########### ATK ############# - set frameatk [frame $subres.frameatk -borderwidth 1] - set labatk [label $frameatk.labatk -text "Wavelet kernel: " -anchor w] - set atk [ComboBox $frameatk.atk \ - -textvariable VMEncoder::var(atk) \ - -width 20 \ - -text {Choose a wavelet kernel} \ - -editable false \ - -values {"R5.3" "I9.7"} ] - set VMEncoder::var(atk) "R5.3" - pack $labatk $atk -side left -anchor w - ########### RESOLUTIONS ############# - set frameres1 [frame $subres.frameres1 -borderwidth 1] - set labresolution [label $frameres1.labresol -text "Resolutions: " -anchor w ] - set frameres2 [frame $subres.frameres2 -borderwidth 1] - set labresX [label $frameres2.labresX -text " X" -anchor w ] - set labresY [label $frameres2.labresY -text " Y" -anchor w ] - set labresZ [label $frameres2.labresZ -text " Z" -anchor w ] - - - set resX [SpinBox $frameres2.spinresX \ - -range {1 6 1} -textvariable VMEncoder::var(resX) \ - -helptext "Number of resolutions in X" \ - -width 3 \ - -editable false ] - set resY [SpinBox $frameres2.spinresY \ - -range {1 6 1} -textvariable VMEncoder::var(resY) \ - -helptext "Number of resolutions in Y" \ - -width 3 \ - -editable false ] - set resZ [SpinBox $frameres2.spinresZ \ - -range {1 6 1} -textvariable VMEncoder::var(resZ) \ - -helptext "Number of resolutions in Z" \ - -width 3 \ - -editable false \ - -state disabled ] - set VMEncoder::var(resX) 3 - set VMEncoder::var(resY) 3 - set VMEncoder::var(resZ) 3 - - ########### TRF ############# - foreach trfval {2DWT 3DWT} trf {2D-DWT 3D-DWT} { - set rad [radiobutton $subtrf.$trfval -text $trf \ - -variable VMEncoder::var(transform) \ - -command "disable3RLS $trfval $atk $resX $resY $resZ"\ - -value $trfval ] - pack $rad -anchor w - } - $subtrf.2DWT select - - pack $subtrf -side left -padx 2 -pady 4 - - pack $labresolution -padx 2 -side left -anchor w - pack $labresX $resX -padx 2 -side left -anchor w - pack $labresY $resY -padx 2 -side left -anchor w - pack $labresZ $resZ -padx 2 -side left -anchor w - - pack $frameres1 -side top -fill x - pack $frameres2 $frameatk -side top -padx 2 -pady 4 -anchor n - - pack $subres -side left -padx 2 -pady 4 - pack $labtrf $labres -side left -fill both -padx 4 -expand yes -} - - -proc VMEncoder::_encode { framesrc framedst } { - - variable var - - set source [$framesrc.labsrc.f.entrysrc get ] - set destination [$framedst.labdst.f.entrydst get ] - set cond1 [string match *.pgx [string tolower $source]] - set cond2 [string match *-*.pgx [string tolower $source]] - set cond3 [string match *.bin [string tolower $source]] - - set img ".img" - set pattern [string range $source 0 [expr [string length $source]-5]] - set pattern $pattern$img - set exist [file exists $pattern] - - #comprobamos datos son correctos - if {($cond1 == 1) && ($cond2 == 0)} { - MessageDlg .msgdlg -parent . -message "Info : Really want to encode an slice instead of a volume?.\n For a group of .pgx slices, name must contain a - denoting a sequential index!" -type ok -icon info - } - - if {$source == ""} { - MessageDlg .msgdlg -parent . -message "Error : Source file is not defined !" -type ok -icon error - } elseif {$destination == ""} { - MessageDlg .msgdlg -parent . -message "Error : Destination file is not defined !" -type ok -icon error - } elseif { ($VMEncoder::var(transform) != "3RLS") && ($VMEncoder::var(atk) == "Choose a wavelet transformation kernel") } { - MessageDlg .msgdlg -parent . -title "Info" -message "Please choose a wavelet transformation kernel"\ - -type ok -icon warning - } elseif {($exist == 0) && ($cond1 == 0) && ($cond3 == 1)} { - MessageDlg .msgdlg -parent . -message "Error : IMG file associated to BIN volume file not found in same directory !" -type ok -icon info - } else { - - #creamos datain a partir de los parametros de entrada -# set dirJP3Dencoder [mk_relativepath $VMEncoder::JP3Dencoder] - set dirJP3Dencoder $VMEncoder::JP3Dencoder - set datain [concat " $dirJP3Dencoder -i [mk_relativepath $source] "] - if {$cond3 == 1} { - set datain [concat " $datain -m [mk_relativepath $pattern] "] - } - set datain [concat " $datain -o [mk_relativepath $destination] "] - if {$VMEncoder::var(encoding) != "2EB"} { - set datain [concat " $datain -C $VMEncoder::var(encoding) "] - } - if {$VMEncoder::var(transform) == "2DWT"} { - set datain [concat " $datain -n $VMEncoder::var(resX),$VMEncoder::var(resY) "] - } elseif {$VMEncoder::var(transform) == "3DWT"} { - set datain [concat " $datain -n $VMEncoder::var(resX),$VMEncoder::var(resY),$VMEncoder::var(resZ) "] - } - - set datain [concat " $datain -r $VMEncoder::var(rate) "] - - if {$VMEncoder::var(atk) == "I9.7"} { - set datain [concat " $datain -I "] - } - if {$VMEncoder::var(sop) == 1} { - set datain [concat " $datain -SOP "] - } - if {$VMEncoder::var(eph) == 1} { - set datain [concat " $datain -EPH "] - } - if {$VMEncoder::var(progorder) != "LRCP"} { - set datain [concat " $datain -p $VMEncoder::var(progorder) "] - } - if {$VMEncoder::var(cblksize) != "64,64,64"} { - set datain [concat " $datain -b $VMEncoder::var(cblksize) "] - } - - - #Making this work would be great !!! - set VMEncoder::var(progval) 10 - ProgressDlg .progress -parent . -title "Wait..." \ - -type infinite \ - -width 20 \ - -textvariable "Compute in progress..."\ - -variable VMEncoder::progval \ - -stop "Stop" \ - -command {destroy .progress} - after 200 set VMEncoder::var(progval) 2 - set fp [open "| $datain " r+] - fconfigure $fp -buffering line - set jp3dVM::dataout [concat "EXECUTED PROGRAM:\n\t$datain"] - while {-1 != [gets $fp tmp]} { - set jp3dVM::dataout [concat "$jp3dVM::dataout\n$tmp"] - } - destroy .progress - set cond [string first "ERROR" $jp3dVM::dataout] - set cond2 [string first "RESULT" $jp3dVM::dataout] - if {$cond != -1} { - MessageDlg .msgdlg -parent . -message [string range $jp3dVM::dataout [expr $cond-1] end] -type ok -icon error - } elseif {$cond2 != -1} { - MessageDlg .msgdlg -parent . -message [string range $jp3dVM::dataout [expr $cond2+7] end] -type ok -icon info - close $fp - } else { - #Must do something with this !!! [pid $fp] - close $fp - } - } -} - -proc VMEncoder::_reset { framesrc framedst frametrf framecod} { - - variable var - - #Restore defaults values - set VMEncoder::var(transform) 2DWT - set VMEncoder::var(encoding) 2EB - set VMEncoder::var(atk) "R5.3" - set VMEncoder::var(progorder) "LRCP" - set atk $frametrf.labres.f.frameatk.atk - set resX $frametrf.labres.f.frameres2.spinresX - set resY $frametrf.labres.f.frameres2.spinresY - set resZ $frametrf.labres.f.frameres2.spinresZ - disable3RLS 2DWT $atk $resX $resY $resZ - set labcblk $framecod.labcod.f.framecblk.labcblk - set progorder $framecod.labcod.f.framepoc.progorder - set labrate $framecod.labcod.f.framerate.labrate - set chksop $framecod.labcod.f.framesop.chksop - set chkeph $framecod.labcod.f.frameeph.chkeph - disableGR 3EB $labcblk $progorder $labrate $chksop $chkeph - - $framesrc.labsrc.f.entrysrc delete 0 end - $framedst.labdst.f.entrydst delete 0 end -} - -proc fileDialogE {w ent operation} { - - variable file - variable i j - - if {$operation == "open"} { - set types { - {"Source Image Files" {.pgx .bin} } - {"All files" *} - } - set file [tk_getOpenFile -filetypes $types -parent $w] - if {[string compare $file ""]} { - $ent delete 0 end - $ent insert end $file - $ent xview moveto 1 - } - } else { - set types { - {"JP3D Files" {.jp3d} } - {"JPEG2000 Files" {.j2k} } - {"All files" *} - } - set file [tk_getSaveFile -filetypes $types -parent $w \ - -initialfile Untitled -defaultextension .jp3d] - if {[string compare $file ""]} { - $ent delete 0 end - $ent insert end $file - $ent xview moveto 1 - } - } -} - -proc mk_relativepath {abspath} { - - set mydir [split [string trimleft [pwd] {/}] {/}] - set abspathcomps [split [string trimleft $abspath {/}] {/}] - - set i 0 - while {$i<[llength $mydir]} { - if {![string compare [lindex $abspathcomps $i] [lindex $mydir $i]]} { - incr i - } else { - break - } - } - set h [expr [llength $mydir]-$i] - set j [expr [llength $abspathcomps]-$i] - - if {!$h} { - set relpath "./" - } else { - set relpath "" - while { $h > 0 } { - set relpath "../$relpath" - incr h -1 - } - } - - set h [llength $abspathcomps] - while { $h > $i } { - set relpath [concat $relpath[lindex $abspathcomps [expr [llength $abspathcomps]-$j]]/] - incr h -1 - incr j -1 - } - return [string trim $relpath {/}] -} - -proc disable3RLS {flag atk resX resY resZ} { - - if {$flag == "3RLS"} { - $atk configure -state disabled - $resX configure -state disabled - $resY configure -state disabled - $resZ configure -state disabled - } elseif {$flag == "2DWT"} { - $atk configure -state normal - $resX configure -state normal - $resY configure -state normal - $resZ configure -state disabled - } elseif {$flag == "3DWT"} { - $atk configure -state normal - $resX configure -state normal - $resY configure -state normal - $resZ configure -state normal - } -} - -proc disableGR {flag labcblk progorder labrate chksop chkeph} { - - if {$flag == "2EB"} { - $labcblk configure -state normal - $progorder configure -state normal - $labrate configure -state normal - $chksop configure -state normal - $chkeph configure -state normal - set VMEncoder::var(cblksize) "64,64,64" - set VMEncoder::var(tilesize) "512,512,512" - } elseif {$flag == "3EB"} { - $labcblk configure -state normal - $progorder configure -state normal - $labrate configure -state normal - $chksop configure -state normal - $chkeph configure -state normal - set VMEncoder::var(cblksize) "64,64,64" - set VMEncoder::var(tilesize) "512,512,512" - } else { - $labcblk configure -state disabled - $progorder configure -state disabled - $labrate configure -state disabled - $chksop configure -state disabled - $chkeph configure -state disabled - } -} \ No newline at end of file diff --git a/jp3d/tcltk/logoLPI.gif b/jp3d/tcltk/logoLPI.gif deleted file mode 100755 index df795150..00000000 Binary files a/jp3d/tcltk/logoLPI.gif and /dev/null differ diff --git a/libopenjpeg/CMakeLists.txt b/libopenjpeg/CMakeLists.txt index 2814ad40..e310e1bb 100644 --- a/libopenjpeg/CMakeLists.txt +++ b/libopenjpeg/CMakeLists.txt @@ -1,6 +1,7 @@ INCLUDE_REGULAR_EXPRESSION("^.*$") # Defines the source code for the library SET(OPENJPEG_SRCS + tcd.c bio.c cio.c dwt.c @@ -16,9 +17,33 @@ SET(OPENJPEG_SRCS pi.c raw.c t1.c - t2.c - tcd.c tgt.c + profile.c + invert.c + function_list.c + t2.c + bio.h + cio.h + dwt.h + event.h + image.h + j2k.h + j2k_lib.h + jp2.h + jpt.h + mct.h + mqc.h + openjpeg.h + pi.h + int.h + raw.h + t1.h + t2.h + tcd.h + tgt.h + profile.h + invert.h + function_list.h ) # Pass proper definition to preprocessor to generate shared lib @@ -30,6 +55,10 @@ IF(WIN32) ENDIF(BUILD_SHARED_LIBS) ENDIF(WIN32) +IF(ENABLE_PROFILING) + ADD_DEFINITIONS(-D_PROFILE) +ENDIF(ENABLE_PROFILING) + # Create the library ADD_LIBRARY(${OPENJPEG_LIBRARY_NAME} ${OPENJPEG_SRCS}) SET_TARGET_PROPERTIES(${OPENJPEG_LIBRARY_NAME} PROPERTIES diff --git a/libopenjpeg/bio.c b/libopenjpeg/bio.c index 4c02f464..78fefa79 100644 --- a/libopenjpeg/bio.c +++ b/libopenjpeg/bio.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,7 +30,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" +#include "bio.h" +#include "opj_malloc.h" /** @defgroup BIO BIO - Individual bit input-output stream */ /*@{*/ @@ -42,25 +44,25 @@ Write a bit @param bio BIO handle @param b Bit to write (0 or 1) */ -static void bio_putbit(opj_bio_t *bio, int b); +static void bio_putbit(opj_bio_t *bio, OPJ_UINT32 b); /** Read a bit @param bio BIO handle @return Returns the read bit */ -static int bio_getbit(opj_bio_t *bio); +static OPJ_UINT32 bio_getbit(opj_bio_t *bio); /** Write a byte @param bio BIO handle @return Returns 0 if successful, returns 1 otherwise */ -static int bio_byteout(opj_bio_t *bio); +static bool bio_byteout(opj_bio_t *bio); /** Read a byte @param bio BIO handle @return Returns 0 if successful, returns 1 otherwise */ -static int bio_bytein(opj_bio_t *bio); +static bool bio_bytein(opj_bio_t *bio); /*@}*/ @@ -72,27 +74,27 @@ static int bio_bytein(opj_bio_t *bio); ========================================================== */ -static int bio_byteout(opj_bio_t *bio) { +static bool bio_byteout(opj_bio_t *bio) { bio->buf = (bio->buf << 8) & 0xffff; bio->ct = bio->buf == 0xff00 ? 7 : 8; if (bio->bp >= bio->end) { - return 1; + return true; } *bio->bp++ = bio->buf >> 8; - return 0; + return false; } -static int bio_bytein(opj_bio_t *bio) { +static bool bio_bytein(opj_bio_t *bio) { bio->buf = (bio->buf << 8) & 0xffff; bio->ct = bio->buf == 0xff00 ? 7 : 8; if (bio->bp >= bio->end) { - return 1; + return true; } bio->buf |= *bio->bp++; - return 0; + return false; } -static void bio_putbit(opj_bio_t *bio, int b) { +static void bio_putbit(opj_bio_t *bio, OPJ_UINT32 b) { if (bio->ct == 0) { bio_byteout(bio); } @@ -100,7 +102,7 @@ static void bio_putbit(opj_bio_t *bio, int b) { bio->buf |= b << bio->ct; } -static int bio_getbit(opj_bio_t *bio) { +static OPJ_UINT32 bio_getbit(opj_bio_t *bio) { if (bio->ct == 0) { bio_bytein(bio); } @@ -125,11 +127,11 @@ void bio_destroy(opj_bio_t *bio) { } } -int bio_numbytes(opj_bio_t *bio) { +OPJ_UINT32 bio_numbytes(opj_bio_t *bio) { return (bio->bp - bio->start); } -void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) { +void bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) { bio->start = bp; bio->end = bp + len; bio->bp = bp; @@ -137,7 +139,7 @@ void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) { bio->ct = 8; } -void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) { +void bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) { bio->start = bp; bio->end = bp + len; bio->bp = bp; @@ -145,43 +147,43 @@ void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) { bio->ct = 0; } -void bio_write(opj_bio_t *bio, int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { +void bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n) { + OPJ_UINT32 i; + for (i = n - 1; i != -1 ; --i) { bio_putbit(bio, (v >> i) & 1); } } -int bio_read(opj_bio_t *bio, int n) { - int i, v; +OPJ_UINT32 bio_read(opj_bio_t *bio, OPJ_UINT32 n) { + OPJ_UINT32 i, v; v = 0; - for (i = n - 1; i >= 0; i--) { + for (i = n - 1; i != -1 ; --i) { v += bio_getbit(bio) << i; } return v; } -int bio_flush(opj_bio_t *bio) { +bool bio_flush(opj_bio_t *bio) { bio->ct = 0; if (bio_byteout(bio)) { - return 1; + return true; } if (bio->ct == 7) { bio->ct = 0; if (bio_byteout(bio)) { - return 1; + return true; } } - return 0; + return false; } -int bio_inalign(opj_bio_t *bio) { +bool bio_inalign(opj_bio_t *bio) { bio->ct = 0; if ((bio->buf & 0xff) == 0xff) { if (bio_bytein(bio)) { - return 1; + return true; } bio->ct = 0; } - return 0; + return false; } diff --git a/libopenjpeg/bio.h b/libopenjpeg/bio.h index 764d7cb2..cc25aa39 100644 --- a/libopenjpeg/bio.h +++ b/libopenjpeg/bio.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,7 +38,7 @@ The functions in BIO.C have for goal to realize an individual bit input - output. */ - +#include "openjpeg.h" /** @defgroup BIO BIO - Individual bit input-output stream */ /*@{*/ @@ -46,15 +47,15 @@ Individual bit input-output stream (BIO) */ typedef struct opj_bio { /** pointer to the start of the buffer */ - unsigned char *start; + OPJ_BYTE *start; /** pointer to the end of the buffer */ - unsigned char *end; + OPJ_BYTE *end; /** pointer to the present position in the buffer */ - unsigned char *bp; + OPJ_BYTE *bp; /** temporary place where each byte is read or written */ - unsigned int buf; + OPJ_UINT32 buf; /** coder : number of bits free to write. decoder : number of bits read */ - int ct; + OPJ_UINT32 ct; } opj_bio_t; /** @name Exported functions */ @@ -75,47 +76,47 @@ Number of bytes written. @param bio BIO handle @return Returns the number of bytes written */ -int bio_numbytes(opj_bio_t *bio); +OPJ_UINT32 bio_numbytes(opj_bio_t *bio); /** Init encoder @param bio BIO handle @param bp Output buffer @param len Output buffer length */ -void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len); +void bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len); /** Init decoder @param bio BIO handle @param bp Input buffer @param len Input buffer length */ -void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len); +void bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len); /** Write bits @param bio BIO handle @param v Value of bits @param n Number of bits to write */ -void bio_write(opj_bio_t *bio, int v, int n); +void bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n); /** Read bits @param bio BIO handle @param n Number of bits to read @return Returns the corresponding read number */ -int bio_read(opj_bio_t *bio, int n); +OPJ_UINT32 bio_read(opj_bio_t *bio, OPJ_UINT32 n); /** Flush bits @param bio BIO handle @return Returns 1 if successful, returns 0 otherwise */ -int bio_flush(opj_bio_t *bio); +bool bio_flush(opj_bio_t *bio); /** Passes the ending bits (coming from flushing) @param bio BIO handle @return Returns 1 if successful, returns 0 otherwise */ -int bio_inalign(opj_bio_t *bio); +bool bio_inalign(opj_bio_t *bio); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/cio.c b/libopenjpeg/cio.c index 2ac262a1..05c7101c 100644 --- a/libopenjpeg/cio.c +++ b/libopenjpeg/cio.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,162 +30,790 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "cio.h" #include "opj_includes.h" +#include "opj_malloc.h" +#include "event.h" /* ----------------------------------------------------------------------- */ -opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) { - opj_cp_t *cp = NULL; - opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t)); - if(!cio) return NULL; - cio->cinfo = cinfo; - if(buffer && length) { - /* wrap a user buffer containing the encoded image */ - cio->openmode = OPJ_STREAM_READ; - cio->buffer = buffer; - cio->length = length; + +/** + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + * @param p_nb_bytes the number of bytes to write +*/ +void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes; + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + memcpy(p_buffer,l_data_ptr,p_nb_bytes); +} + +/** + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + * @param p_nb_bytes the number of bytes to write + * @return the number of bytes written or -1 if an error occured +*/ +void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes) +{ + const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1; + OPJ_UINT32 i; + + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + for + (i=0;iopenmode = OPJ_STREAM_WRITE; - switch(cinfo->codec_format) { - case CODEC_J2K: - cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp; - break; - case CODEC_JP2: - cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp; - break; - default: - opj_free(cio); - return NULL; +} + +/** + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + * @param p_nb_bytes the nb bytes to read. + * @return the number of bytes read or -1 if an error occured. + */ +void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value); + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + *p_value = 0; + memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes); +} + +/** + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + * @param p_nb_bytes the nb bytes to read. + * @return the number of bytes read or -1 if an error occured. + */ +void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes) +{ + OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes-1; + OPJ_UINT32 i; + + assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32)); + *p_value = 0; + for + (i=0;im_buffer_size = p_size; + l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_size); + if + (! l_stream->m_stored_data) + { + opj_free(l_stream); + return 00; + } + l_stream->m_current_data = l_stream->m_stored_data; + if + (l_is_input) + { + l_stream->m_status |= opj_stream_e_input; + l_stream->m_opj_skip = opj_stream_read_skip; + l_stream->m_opj_seek = opj_stream_read_seek; + } + else + { + l_stream->m_status |= opj_stream_e_output; + l_stream->m_opj_skip = opj_stream_write_skip; + l_stream->m_opj_seek = opj_stream_write_seek; + } + l_stream->m_read_fn = opj_stream_default_read; + l_stream->m_write_fn = opj_stream_default_write; + l_stream->m_skip_fn = opj_stream_default_skip; + l_stream->m_seek_fn = opj_stream_default_seek; + + return (opj_stream_t *) l_stream; +} + +/** + * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream. + * @return a stream object. +*/ +opj_stream_t* opj_stream_default_create(bool l_is_input) +{ + return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input); +} + +/** + * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must + * close its own implementation of the stream. + */ +OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if + (l_stream) + { + opj_free(l_stream->m_stored_data); + l_stream->m_stored_data = 00; + opj_free(l_stream); + } + +} + +/** + * Sets the given function to be used as a read function. + * @param p_stream the stream to modify + * @param p_function the function to use a read function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if + ((!l_stream) || (! (l_stream->m_status & opj_stream_e_input))) + { + return; + } + l_stream->m_read_fn = p_function; +} + +OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if + (!l_stream) + { + return; + } + l_stream->m_seek_fn = p_function; +} + +/** + * Sets the given function to be used as a write function. + * @param p_stream the stream to modify + * @param p_function the function to use a write function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if + ((!l_stream )|| (! (l_stream->m_status & opj_stream_e_output))) + { + return; + } + l_stream->m_write_fn = p_function; +} + +/** + * Sets the given function to be used as a skip function. + * @param p_stream the stream to modify + * @param p_function the function to use a skip function. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + if + (! l_stream) + { + return; + } + l_stream->m_skip_fn = p_function; +} + +/** + * Sets the given data to be used as a user data for the stream. + * @param p_stream the stream to modify + * @param p_data the data to set. +*/ +OPJ_API void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data) +{ + opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream; + l_stream->m_user_data = p_data; +} + +/** + * Reads some bytes from the stream. + * @param p_stream the stream to read data from. + * @param p_buffer pointer to the data buffer that will receive the data. + * @param p_size number of bytes to read. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes read, or -1 if an error occured or if the stream is at the end. + */ +OPJ_UINT32 opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, opj_event_mgr_t * p_event_mgr) +{ + OPJ_UINT32 l_read_nb_bytes = 0; + if + (p_stream->m_bytes_in_buffer >= p_size) + { + memcpy(p_buffer,p_stream->m_current_data,p_size); + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer -= p_size; + l_read_nb_bytes += p_size; + p_stream->m_byte_offset += p_size; + return l_read_nb_bytes; + } + + // we are now in the case when the remaining data if not sufficient + if + (p_stream->m_status & opj_stream_e_end) + { + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer); + p_stream->m_current_data += p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + return l_read_nb_bytes ? l_read_nb_bytes : -1; + } + + // the flag is not set, we copy data and then do an actual read on the stream + if + (p_stream->m_bytes_in_buffer) + { + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer); + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += p_stream->m_bytes_in_buffer; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } + + while + (true) + { + // we should read less than a chunk -> read a chunk + if + (p_size < p_stream->m_buffer_size) + { + // we should do an actual read on the media + p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_stream->m_stored_data,p_stream->m_buffer_size,p_stream->m_user_data); + if + (p_stream->m_bytes_in_buffer == -1) + { + // end of stream + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + p_stream->m_bytes_in_buffer = 0; + p_stream->m_status |= opj_stream_e_end; + // end of stream + return l_read_nb_bytes ? l_read_nb_bytes : -1; + } + else if + (p_stream->m_bytes_in_buffer < p_size) + { + // not enough data + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer); + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += p_stream->m_bytes_in_buffer; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } + else + { + l_read_nb_bytes += p_size; + memcpy(p_buffer,p_stream->m_current_data,p_size); + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer -= p_size; + p_stream->m_byte_offset += p_size; + return l_read_nb_bytes; + } } - cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */ - cio->buffer = (unsigned char *)opj_malloc(cio->length); - if(!cio->buffer) { - opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n"); - opj_free(cio); - return NULL; + else + { + // direct read on the dest buffer + p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_buffer,p_size,p_stream->m_user_data); + if + (p_stream->m_bytes_in_buffer == -1) + { + // end of stream + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + p_stream->m_bytes_in_buffer = 0; + p_stream->m_status |= opj_stream_e_end; + // end of stream + return l_read_nb_bytes ? l_read_nb_bytes : -1; + } + else if + (p_stream->m_bytes_in_buffer < p_size) + { + // not enough data + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += p_stream->m_bytes_in_buffer; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + } + else + { + // we have read the exact size + l_read_nb_bytes += p_stream->m_bytes_in_buffer; + p_stream->m_byte_offset += p_stream->m_bytes_in_buffer; + p_stream->m_current_data = p_stream->m_stored_data; + p_stream->m_bytes_in_buffer = 0; + return l_read_nb_bytes; + } } } - else { - opj_free(cio); - return NULL; - } - - /* Initialize byte IO */ - cio->start = cio->buffer; - cio->end = cio->buffer + cio->length; - cio->bp = cio->buffer; - - return cio; } -void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) { - if(cio) { - if(cio->openmode == OPJ_STREAM_WRITE) { - /* destroy the allocated buffer */ - opj_free(cio->buffer); +/** + * Writes some bytes from the stream. + * @param p_stream the stream to write data to. + * @param p_buffer pointer to the data buffer holds the data to be writtent. + * @param p_size number of bytes to write. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes writtent, or -1 if an error occured. + */ +OPJ_UINT32 opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer,OPJ_UINT32 p_size, opj_event_mgr_t * p_event_mgr) +{ + OPJ_UINT32 l_remaining_bytes = 0; + OPJ_UINT32 l_write_nb_bytes = 0; + + if + (p_stream->m_status & opj_stream_e_error) + { + return -1; + } + + while + (true) + { + l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer; + // we have more memory than required + if + (l_remaining_bytes >= p_size) + { + memcpy(p_stream->m_current_data,p_buffer,p_size); + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer += p_size; + l_write_nb_bytes += p_size; + p_stream->m_byte_offset += p_size; + return l_write_nb_bytes; + } + + // we copy data and then do an actual read on the stream + if + (l_remaining_bytes) + { + l_write_nb_bytes += l_remaining_bytes; + memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes); + p_stream->m_current_data = p_stream->m_stored_data; + p_buffer += l_remaining_bytes; + p_size -= l_remaining_bytes; + p_stream->m_bytes_in_buffer += l_remaining_bytes; + p_stream->m_byte_offset += l_remaining_bytes; + } + if + (! opj_stream_flush(p_stream, p_event_mgr)) + { + return -1; } - /* destroy the cio */ - opj_free(cio); } + } - -/* ----------------------------------------------------------------------- */ - -/* - * Get position in byte stream. +/** + * Writes the content of the stream buffer to the stream. + * @param p_stream the stream to write data to. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes written, or -1 if an error occured. */ -int OPJ_CALLCONV cio_tell(opj_cio_t *cio) { - return cio->bp - cio->start; -} +bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr) +{ + // the number of bytes written on the media. + OPJ_UINT32 l_current_write_nb_bytes = 0; + p_stream->m_current_data = p_stream->m_stored_data; -/* - * Set position in byte stream. - * - * pos : position, in number of bytes, from the beginning of the stream - */ -void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) { - cio->bp = cio->start + pos; -} - -/* - * Number of bytes left before the end of the stream. - */ -int cio_numbytesleft(opj_cio_t *cio) { - return cio->end - cio->bp; -} - -/* - * Get pointer to the current position in the stream. - */ -unsigned char *cio_getbp(opj_cio_t *cio) { - return cio->bp; -} - -/* - * Write a byte. - */ -bool cio_byteout(opj_cio_t *cio, unsigned char v) { - if (cio->bp >= cio->end) { - opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n"); - return false; + while + (p_stream->m_bytes_in_buffer) + { + // we should do an actual write on the media + l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data,p_stream->m_bytes_in_buffer,p_stream->m_user_data); + if + (l_current_write_nb_bytes == -1) + { + p_stream->m_status |= opj_stream_e_error; + opj_event_msg(p_event_mgr, EVT_INFO, "Error on writting stream!\n"); + return false; + } + p_stream->m_current_data += l_current_write_nb_bytes; + p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes; } - *cio->bp++ = v; + p_stream->m_current_data = p_stream->m_stored_data; return true; } -/* - * Read a byte. +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. */ -unsigned char cio_bytein(opj_cio_t *cio) { - if (cio->bp >= cio->end) { - opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end); - return 0; +OPJ_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr) +{ + OPJ_SIZE_T l_skip_nb_bytes = 0; + OPJ_SIZE_T l_current_skip_nb_bytes = 0; + + if + (p_stream->m_bytes_in_buffer >= p_size) + { + p_stream->m_current_data += p_size; + p_stream->m_bytes_in_buffer -= p_size; + l_skip_nb_bytes += p_size; + p_stream->m_byte_offset += l_skip_nb_bytes; + return l_skip_nb_bytes; } - return *cio->bp++; -} -/* - * Write some bytes. - * - * v : value to write - * n : number of bytes to write - */ -unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) { - int i; - for (i = n - 1; i >= 0; i--) { - if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) ) - return 0; + // we are now in the case when the remaining data if not sufficient + if + (p_stream->m_status & opj_stream_e_end) + { + l_skip_nb_bytes += p_stream->m_bytes_in_buffer; + p_stream->m_current_data += p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; + p_stream->m_byte_offset += l_skip_nb_bytes; + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T) -1; } - return n; -} - -/* - * Read some bytes. - * - * n : number of bytes to read - * - * return : value of the n bytes read - */ -unsigned int cio_read(opj_cio_t *cio, int n) { - int i; - unsigned int v; - v = 0; - for (i = n - 1; i >= 0; i--) { - v += cio_bytein(cio) << (i << 3); + + // the flag is not set, we copy data and then do an actual skip on the stream + if + (p_stream->m_bytes_in_buffer) + { + l_skip_nb_bytes += p_stream->m_bytes_in_buffer; + p_stream->m_current_data = p_stream->m_stored_data; + p_size -= p_stream->m_bytes_in_buffer; + p_stream->m_bytes_in_buffer = 0; } - return v; + + while + (p_size > 0) + { + // we should do an actual skip on the media + l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data); + if + (l_current_skip_nb_bytes == (OPJ_SIZE_T) -1) + { + opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n"); + p_stream->m_status |= opj_stream_e_end; + p_stream->m_byte_offset += l_skip_nb_bytes; + // end if stream + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T) -1; + } + p_size -= l_current_skip_nb_bytes; + l_skip_nb_bytes += l_current_skip_nb_bytes; + } + p_stream->m_byte_offset += l_skip_nb_bytes; + return l_skip_nb_bytes; } -/* - * Skip some bytes. - * - * n : number of bytes to skip +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. */ -void cio_skip(opj_cio_t *cio, int n) { - cio->bp += n; +OPJ_SIZE_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr) +{ + bool l_is_written = 0; + OPJ_SIZE_T l_current_skip_nb_bytes = 0; + OPJ_SIZE_T l_skip_nb_bytes = 0; + + if + (p_stream->m_status & opj_stream_e_error) + { + return (OPJ_SIZE_T) -1; + } + + // we should flush data + l_is_written = opj_stream_flush (p_stream, p_event_mgr); + if + (! l_is_written) + { + p_stream->m_status |= opj_stream_e_error; + p_stream->m_bytes_in_buffer = 0; + p_stream->m_current_data = p_stream->m_current_data; + return (OPJ_SIZE_T) -1; + } + // then skip + + while + (p_size > 0) + { + // we should do an actual skip on the media + l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data); + if + (l_current_skip_nb_bytes == (OPJ_SIZE_T)-1) + { + opj_event_msg(p_event_mgr, EVT_INFO, "Stream error!\n"); + p_stream->m_status |= opj_stream_e_error; + p_stream->m_byte_offset += l_skip_nb_bytes; + // end if stream + return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T)-1; + } + p_size -= l_current_skip_nb_bytes; + l_skip_nb_bytes += l_current_skip_nb_bytes; + } + p_stream->m_byte_offset += l_skip_nb_bytes; + return l_skip_nb_bytes; +} + +/** + * Tells the byte offset on the stream (similar to ftell). + * + * @param p_stream the stream to get the information from. + * + * @return the current position o fthe stream. + */ +OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream) +{ + return p_stream->m_byte_offset; +} + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr) +{ + return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr); +} + + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr) +{ + p_stream->m_current_data = p_stream->m_stored_data; + p_stream->m_bytes_in_buffer = 0; + if + (! p_stream->m_seek_fn(p_size,p_stream->m_user_data)) + { + p_stream->m_status |= opj_stream_e_end; + return false; + } + else + { + // reset stream status + p_stream->m_status &= (~opj_stream_e_end); + p_stream->m_byte_offset = p_size; + + } + return true; +} + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr) +{ + if + (! opj_stream_flush(p_stream,p_event_mgr)) + { + p_stream->m_status |= opj_stream_e_error; + return false; + } + + p_stream->m_current_data = p_stream->m_stored_data; + p_stream->m_bytes_in_buffer = 0; + + if + (! p_stream->m_seek_fn(p_size,p_stream->m_user_data)) + { + p_stream->m_status |= opj_stream_e_error; + return false; + } + else + { + p_stream->m_byte_offset = p_size; + } + return true; +} + + +/** + * Seeks a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return true if the stream is seekable. + */ +bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr) +{ + return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr); +} + +/** + * Tells if the given stream is seekable. + */ +bool opj_stream_has_seek (const opj_stream_private_t * p_stream) +{ + return p_stream->m_seek_fn != opj_stream_default_seek; +} + + + + + +OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data) +{ + return (OPJ_UINT32) -1; +} +OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data) +{ + return (OPJ_UINT32) -1; +} +OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data) +{ + return (OPJ_SIZE_T) -1; +} + +bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data) +{ + return false; } diff --git a/libopenjpeg/cio.h b/libopenjpeg/cio.h index 580bf9c0..12055ff0 100644 --- a/libopenjpeg/cio.h +++ b/libopenjpeg/cio.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -41,42 +42,314 @@ The functions in CIO.C have for goal to realize a byte input / output process. /** @defgroup CIO CIO - byte input-output stream */ /*@{*/ +#include "openjpeg.h" +#include "opj_configure.h" +struct opj_event_mgr; /** @name Exported functions (see also openjpeg.h) */ /*@{*/ /* ----------------------------------------------------------------------- */ + +#if defined(OPJ_BIG_ENDIAN) + #if !defined(OPJ_LITTLE_ENDIAN) + #define opj_write_bytes opj_write_bytes_BE + #define opj_read_bytes opj_read_bytes_BE + #define opj_write_double opj_write_double_BE + #define opj_read_double opj_read_double_BE + #define opj_write_float opj_write_float_BE + #define opj_read_float opj_read_float_BE + #else + #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not both." + #endif +#else + #if defined(OPJ_LITTLE_ENDIAN) + #define opj_write_bytes opj_write_bytes_LE + #define opj_read_bytes opj_read_bytes_LE + #define opj_write_double opj_write_double_LE + #define opj_read_double opj_read_double_LE + #define opj_write_float opj_write_float_LE + #define opj_read_float opj_read_float_LE + #else + #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not none." + #endif +#endif + + + +typedef enum +{ + opj_stream_e_output = 0x1, + opj_stream_e_input = 0x2, + opj_stream_e_end = 0x4, + opj_stream_e_error = 0x8 +} +opj_stream_flag ; + /** -Number of bytes left before the end of the stream -@param cio CIO handle -@return Returns the number of bytes before the end of the stream +Byte input-output stream. */ -int cio_numbytesleft(opj_cio_t *cio); +typedef struct opj_stream_private +{ + /** + * User data, be it files, ... The actual data depends on the type of the stream. + */ + void * m_user_data; + + /** + * Pointer to actual read function (NULL at the initialization of the cio. + */ + opj_stream_read_fn m_read_fn; + + /** + * Pointer to actual write function (NULL at the initialization of the cio. + */ + opj_stream_write_fn m_write_fn; + + /** + * Pointer to actual skip function (NULL at the initialization of the cio. + * There is no seek function to prevent from back and forth slow procedures. + */ + opj_stream_skip_fn m_skip_fn; + + /** + * Pointer to actual seek function (if available). + */ + opj_stream_seek_fn m_seek_fn; + + + + + /** + * Actual data stored into the stream if readed from. Data is read by chunk of fixed size. + * you should never access this data directly. + */ + OPJ_BYTE * m_stored_data; + + /** + * Pointer to the current read data. + */ + OPJ_BYTE * m_current_data; + + OPJ_SIZE_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_SIZE_T , struct opj_event_mgr *); + + bool (* m_opj_seek) (struct opj_stream_private * , OPJ_SIZE_T , struct opj_event_mgr *); + + /** + * number of bytes containing in the buffer. + */ + OPJ_UINT32 m_bytes_in_buffer; + + /** + * The number of bytes read/written. + */ + OPJ_SIZE_T m_byte_offset; + + /** + * The size of the buffer. + */ + OPJ_UINT32 m_buffer_size; + + /** + * Flags to tell the status of the stream. + */ + OPJ_UINT32 m_status; + +} +opj_stream_private_t; + + /** -Get pointer to the current position in the stream -@param cio CIO handle -@return Returns a pointer to the current position + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + * @param p_nb_bytes the number of bytes to write */ -unsigned char *cio_getbp(opj_cio_t *cio); +void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes); + /** -Write some bytes -@param cio CIO handle -@param v Value to write -@param n Number of bytes to write -@return Returns the number of bytes written or 0 if an error occured -*/ -unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n); + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + * @param p_nb_bytes the nb bytes to read. + * @return the number of bytes read or -1 if an error occured. + */ +void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes); + /** -Read some bytes -@param cio CIO handle -@param n Number of bytes to read -@return Returns the value of the n bytes read + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + * @param p_nb_bytes the number of bytes to write + * @return the number of bytes written or -1 if an error occured */ -unsigned int cio_read(opj_cio_t *cio, int n); +void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes); + /** -Skip some bytes -@param cio CIO handle -@param n Number of bytes to skip -*/ -void cio_skip(opj_cio_t *cio, int n); + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + * @param p_nb_bytes the nb bytes to read. + * @return the number of bytes read or -1 if an error occured. + */ +void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes); + + +/** + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value); + +/*** + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value); + +/** + * Reads some bytes from the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to read data from. + * @param p_value pointer to the value that will store the data. + */ +void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value); + +/** + * Write some bytes to the given data buffer, this function is used in Little Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value); + +/*** + * Write some bytes to the given data buffer, this function is used in Big Endian cpus. + * @param p_buffer pointer the data buffer to write data to. + * @param p_value the value to write + */ +void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value); + +/** + * Reads some bytes from the stream. + * @param p_stream the stream to read data from. + * @param p_buffer pointer to the data buffer that will receive the data. + * @param p_size number of bytes to read. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes read, or -1 if an error occured or if the stream is at the end. + */ +OPJ_UINT32 opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Writes some bytes to the stream. + * @param p_stream the stream to write data to. + * @param p_buffer pointer to the data buffer holds the data to be writtent. + * @param p_size number of bytes to write. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes writtent, or -1 if an error occured. + */ +OPJ_UINT32 opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Writes the content of the stream buffer to the stream. + * @param p_stream the stream to write data to. + * @param p_event_mgr the user event manager to be notified of special events. + * @return true if the data could be flushed, false else. + */ +bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Tells the byte offset on the stream (similar to ftell). + * + * @param p_stream the stream to get the information from. + * + * @return the current position o fthe stream. + */ +OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +OPJ_SIZE_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +OPJ_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Skips a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return the number of bytes skipped, or -1 if an error occured. + */ +bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Seeks a number of bytes from the stream. + * @param p_stream the stream to skip data from. + * @param p_size the number of bytes to skip. + * @param p_event_mgr the user event manager to be notified of special events. + * @return true if the stream is seekable. + */ +bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr); + +/** + * Tells if the given stream is seekable. + */ +bool opj_stream_has_seek (const opj_stream_private_t * p_stream); + +OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data); +OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data); +OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data); +bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data); + /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/dwt.c b/libopenjpeg/dwt.c index 78d18d17..a6ffd180 100644 --- a/libopenjpeg/dwt.c +++ b/libopenjpeg/dwt.c @@ -7,6 +7,7 @@ * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2007, Jonathan Ballard * Copyright (c) 2007, Callum Lerwick + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +36,12 @@ #include #endif -#include "opj_includes.h" +#include "dwt.h" +#include "j2k.h" +#include "tcd.h" +#include "fix.h" +#include "opj_malloc.h" +#include "int.h" /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ /*@{*/ @@ -47,31 +53,31 @@ /*@{*/ typedef struct dwt_local { - int* mem; - int dn; - int sn; - int cas; + OPJ_INT32* mem; + OPJ_INT32 dn; + OPJ_INT32 sn; + OPJ_INT32 cas; } dwt_t; typedef union { - float f[4]; + OPJ_FLOAT32 f[4]; } v4; typedef struct v4dwt_local { v4* wavelet ; - int dn ; - int sn ; - int cas ; + OPJ_INT32 dn ; + OPJ_INT32 sn ; + OPJ_INT32 cas ; } v4dwt_t ; -static const float dwt_alpha = 1.586134342f; // 12994 -static const float dwt_beta = 0.052980118f; // 434 -static const float dwt_gamma = -0.882911075f; // -7233 -static const float dwt_delta = -0.443506852f; // -3633 +static const OPJ_FLOAT32 dwt_alpha = 1.586134342f; // 12994 +static const OPJ_FLOAT32 dwt_beta = 0.052980118f; // 434 +static const OPJ_FLOAT32 dwt_gamma = -0.882911075f; // -7233 +static const OPJ_FLOAT32 delta = -0.443506852f; // -3633 -static const float K = 1.230174105f; // 10078 +static const OPJ_FLOAT32 K = 1.230174105f; // 10078 /* FIXME: What is this constant? */ -static const float c13318 = 1.625732422f; +static const OPJ_FLOAT32 c13318 = 1.625732422f; /*@}*/ @@ -86,23 +92,23 @@ typedef void (*DWT1DFN)(dwt_t* v); /** Forward lazy transform (horizontal) */ -static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas); +static void dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas); /** Forward lazy transform (vertical) */ -static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas); +static void dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas); /** Inverse lazy transform (horizontal) */ -static void dwt_interleave_h(dwt_t* h, int *a); +static void dwt_interleave_h(dwt_t* h, OPJ_INT32 *a); /** Inverse lazy transform (vertical) */ -static void dwt_interleave_v(dwt_t* v, int *a, int x); +static void dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x); /** Forward 5-3 wavelet transform in 1-D */ -static void dwt_encode_1(int *a, int dn, int sn, int cas); +static void dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas); /** Inverse 5-3 wavelet transform in 1-D */ @@ -110,16 +116,19 @@ static void dwt_decode_1(dwt_t *v); /** Forward 9-7 wavelet transform in 1-D */ -static void dwt_encode_1_real(int *a, int dn, int sn, int cas); +static void dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas); /** Explicit calculation of the Quantization Stepsizes */ -static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize); +static void dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize); /** Inverse wavelet transform in 2-D. */ -static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn); +static bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn); +static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i); + +static INLINE bool dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) ); /*@}*/ /*@}*/ @@ -135,7 +144,7 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn); /* */ /* This table contains the norms of the 5-3 wavelets for different bands. */ /* */ -static const double dwt_norms[4][10] = { +static const OPJ_FLOAT64 dwt_norms[4][10] = { {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3}, {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9}, @@ -145,7 +154,7 @@ static const double dwt_norms[4][10] = { /* */ /* This table contains the norms of the 9-7 wavelets for different bands. */ /* */ -static const double dwt_norms_real[4][10] = { +static const OPJ_FLOAT64 dwt_norms_real[4][10] = { {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9}, {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0}, @@ -161,48 +170,88 @@ static const double dwt_norms_real[4][10] = { /* */ /* Forward lazy transform (horizontal). */ /* */ -static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) { - int i; - for (i=0; i */ /* Forward lazy transform (vertical). */ /* */ -static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) { - int i; - for (i=0; i */ /* Inverse lazy transform (horizontal). */ /* */ -static void dwt_interleave_h(dwt_t* h, int *a) { - int *ai = a; - int *bi = h->mem + h->cas; - int i = h->sn; - while( i-- ) { - *bi = *(ai++); - bi += 2; +static void dwt_interleave_h(dwt_t* h, OPJ_INT32 *a) { + OPJ_INT32 *ai = a; + OPJ_INT32 *bi = h->mem + h->cas; + OPJ_INT32 i = h->sn; + while + ( i-- ) + { + *bi = *(ai++); + bi += 2; } ai = a + h->sn; bi = h->mem + 1 - h->cas; i = h->dn ; - while( i-- ) { - *bi = *(ai++); - bi += 2; + while + ( i-- ) + { + *bi = *(ai++); + bi += 2; } } /* */ /* Inverse lazy transform (vertical). */ /* */ -static void dwt_interleave_v(dwt_t* v, int *a, int x) { - int *ai = a; - int *bi = v->mem + v->cas; - int i = v->sn; +static void dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) { + OPJ_INT32 *ai = a; + OPJ_INT32 *bi = v->mem + v->cas; + OPJ_INT32 i = v->sn; while( i-- ) { *bi = *ai; bi += 2; @@ -222,8 +271,8 @@ static void dwt_interleave_v(dwt_t* v, int *a, int x) { /* */ /* Forward 5-3 wavelet transform in 1-D. */ /* */ -static void dwt_encode_1(int *a, int dn, int sn, int cas) { - int i; +static void dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) { + OPJ_INT32 i; if (!cas) { if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ @@ -243,8 +292,8 @@ static void dwt_encode_1(int *a, int dn, int sn, int cas) { /* */ /* Inverse 5-3 wavelet transform in 1-D. */ /* */ -static void dwt_decode_1_(int *a, int dn, int sn, int cas) { - int i; +static void dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) { + OPJ_INT32 i; if (!cas) { if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ @@ -271,8 +320,8 @@ static void dwt_decode_1(dwt_t *v) { /* */ /* Forward 9-7 wavelet transform in 1-D. */ /* */ -static void dwt_encode_1_real(int *a, int dn, int sn, int cas) { - int i; +static void dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) { + OPJ_INT32 i; if (!cas) { if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ for (i = 0; i < dn; i++) @@ -306,8 +355,8 @@ static void dwt_encode_1_real(int *a, int dn, int sn, int cas) { } } -static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) { - int p, n; +static void dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) { + OPJ_INT32 p, n; p = int_floorlog2(stepsize) - 13; n = 11 - int_floorlog2(stepsize); bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; @@ -323,71 +372,105 @@ static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno /* */ /* Forward 5-3 wavelet transform in 2-D. */ /* */ -void dwt_encode(opj_tcd_tilecomp_t * tilec) { - int i, j, k; - int *a = NULL; - int *aj = NULL; - int *bj = NULL; - int w, l; +INLINE bool dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) ) +{ + OPJ_INT32 i, j, k; + OPJ_INT32 *a = 00; + OPJ_INT32 *aj = 00; + OPJ_INT32 *bj = 00; + OPJ_INT32 w, l; + OPJ_INT32 rw; /* width of the resolution level computed */ + OPJ_INT32 rh; /* height of the resolution level computed */ + OPJ_INT32 l_data_size; + + opj_tcd_resolution_t * l_cur_res = 0; + opj_tcd_resolution_t * l_last_res = 0; + w = tilec->x1-tilec->x0; l = tilec->numresolutions-1; a = tilec->data; - for (i = 0; i < l; i++) { - int rw; /* width of the resolution level computed */ - int rh; /* height of the resolution level computed */ - int rw1; /* width of the resolution level once lower than computed one */ - int rh1; /* height of the resolution level once lower than computed one */ - int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ - int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ - int dn, sn; + l_cur_res = tilec->resolutions + l; + l_last_res = l_cur_res - 1; + + rw = l_cur_res->x1 - l_cur_res->x0; + rh = l_cur_res->y1 - l_cur_res->y0; + + l_data_size = dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32); + bj = opj_malloc(l_data_size); + if + (! bj) + { + return false; + } + i = l; + + while + (i--) + { + OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */ + OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */ + OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ + OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ + OPJ_INT32 dn, sn; - rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0; - rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0; - rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0; - rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0; + rw = l_cur_res->x1 - l_cur_res->x0; + rh = l_cur_res->y1 - l_cur_res->y0; + rw1 = l_last_res->x1 - l_last_res->x0; + rh1 = l_last_res->y1 - l_last_res->y0; - cas_row = tilec->resolutions[l - i].x0 % 2; - cas_col = tilec->resolutions[l - i].y0 % 2; + cas_row = l_cur_res->x0 & 1; + cas_col = l_cur_res->y0 & 1; sn = rh1; dn = rh - rh1; - bj = (int*)opj_malloc(rh * sizeof(int)); - for (j = 0; j < rw; j++) { + for + (j = 0; j < rw; ++j) + { aj = a + j; - for (k = 0; k < rh; k++) bj[k] = aj[k*w]; - dwt_encode_1(bj, dn, sn, cas_col); + for + (k = 0; k < rh; ++k) + { + bj[k] = aj[k*w]; + } + (*p_function) (bj, dn, sn, cas_col); dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col); } - opj_free(bj); - sn = rw1; dn = rw - rw1; - bj = (int*)opj_malloc(rw * sizeof(int)); - for (j = 0; j < rh; j++) { + for (j = 0; j < rh; j++) + { aj = a + j * w; for (k = 0; k < rw; k++) bj[k] = aj[k]; - dwt_encode_1(bj, dn, sn, cas_row); + (*p_function) (bj, dn, sn, cas_row); dwt_deinterleave_h(bj, aj, dn, sn, cas_row); } - opj_free(bj); + l_cur_res = l_last_res; + --l_last_res; } + opj_free(bj); + return true; +} +/* Forward 5-3 wavelet transform in 2-D. */ +/* */ +bool dwt_encode(opj_tcd_tilecomp_t * tilec) +{ + return dwt_encode_procedure(tilec,dwt_encode_1); } - /* */ /* Inverse 5-3 wavelet transform in 2-D. */ /* */ -void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres) { - dwt_decode_tile(tilec, numres, &dwt_decode_1); +bool dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) { + return dwt_decode_tile(tilec, numres, &dwt_decode_1); } /* */ /* Get gain of 5-3 wavelet transform. */ /* */ -int dwt_getgain(int orient) { +OPJ_UINT32 dwt_getgain(OPJ_UINT32 orient) { if (orient == 0) return 0; if (orient == 1 || orient == 2) @@ -398,71 +481,24 @@ int dwt_getgain(int orient) { /* */ /* Get norm of 5-3 wavelet. */ /* */ -double dwt_getnorm(int level, int orient) { +OPJ_FLOAT64 dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) { return dwt_norms[orient][level]; } /* */ /* Forward 9-7 wavelet transform in 2-D. */ /* */ - -void dwt_encode_real(opj_tcd_tilecomp_t * tilec) { - int i, j, k; - int *a = NULL; - int *aj = NULL; - int *bj = NULL; - int w, l; - - w = tilec->x1-tilec->x0; - l = tilec->numresolutions-1; - a = tilec->data; - - for (i = 0; i < l; i++) { - int rw; /* width of the resolution level computed */ - int rh; /* height of the resolution level computed */ - int rw1; /* width of the resolution level once lower than computed one */ - int rh1; /* height of the resolution level once lower than computed one */ - int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */ - int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */ - int dn, sn; - - rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0; - rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0; - rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0; - rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0; - - cas_row = tilec->resolutions[l - i].x0 % 2; - cas_col = tilec->resolutions[l - i].y0 % 2; - - sn = rh1; - dn = rh - rh1; - bj = (int*)opj_malloc(rh * sizeof(int)); - for (j = 0; j < rw; j++) { - aj = a + j; - for (k = 0; k < rh; k++) bj[k] = aj[k*w]; - dwt_encode_1_real(bj, dn, sn, cas_col); - dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col); - } - opj_free(bj); - - sn = rw1; - dn = rw - rw1; - bj = (int*)opj_malloc(rw * sizeof(int)); - for (j = 0; j < rh; j++) { - aj = a + j * w; - for (k = 0; k < rw; k++) bj[k] = aj[k]; - dwt_encode_1_real(bj, dn, sn, cas_row); - dwt_deinterleave_h(bj, aj, dn, sn, cas_row); - } - opj_free(bj); - } +bool dwt_encode_real(opj_tcd_tilecomp_t * tilec) +{ + return dwt_encode_procedure(tilec,dwt_encode_1_real); } + /* */ /* Get gain of 9-7 wavelet transform. */ /* */ -int dwt_getgain_real(int orient) { +OPJ_UINT32 dwt_getgain_real(OPJ_UINT32 orient) { (void)orient; return 0; } @@ -470,16 +506,16 @@ int dwt_getgain_real(int orient) { /* */ /* Get norm of 9-7 wavelet. */ /* */ -double dwt_getnorm_real(int level, int orient) { +OPJ_FLOAT64 dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) { return dwt_norms_real[orient][level]; } -void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) { - int numbands, bandno; +void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) { + OPJ_UINT32 numbands, bandno; numbands = 3 * tccp->numresolutions - 2; for (bandno = 0; bandno < numbands; bandno++) { - double stepsize; - int resno, level, orient, gain; + OPJ_FLOAT64 stepsize; + OPJ_UINT32 resno, level, orient, gain; resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1); orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1); @@ -488,10 +524,10 @@ void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) { if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { stepsize = 1.0; } else { - double norm = dwt_norms_real[orient][level]; + OPJ_FLOAT64 norm = dwt_norms_real[orient][level]; stepsize = (1 << (gain)) / norm; } - dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]); + dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]); } } @@ -499,11 +535,11 @@ void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) { /* */ /* Determine maximum computed resolution level for inverse wavelet transform */ /* */ -static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) { - int mr = 1; - int w; +static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) { + OPJ_UINT32 mr = 0; + OPJ_UINT32 w; while( --i ) { - r++; + ++r; if( mr < ( w = r->x1 - r->x0 ) ) mr = w ; if( mr < ( w = r->y1 - r->y0 ) ) @@ -516,23 +552,29 @@ static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) { /* */ /* Inverse wavelet transform in 2-D. */ /* */ -static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1D) { +static bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) { dwt_t h; dwt_t v; opj_tcd_resolution_t* tr = tilec->resolutions; - int rw = tr->x1 - tr->x0; /* width of the resolution level computed */ - int rh = tr->y1 - tr->y0; /* height of the resolution level computed */ + OPJ_UINT32 rw = tr->x1 - tr->x0; /* width of the resolution level computed */ + OPJ_UINT32 rh = tr->y1 - tr->y0; /* height of the resolution level computed */ - int w = tilec->x1 - tilec->x0; + OPJ_UINT32 w = tilec->x1 - tilec->x0; + + h.mem = opj_aligned_malloc(dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32)); + if + (! h.mem) + { + return false; + } - h.mem = opj_aligned_malloc(dwt_decode_max_resolution(tr, numres) * sizeof(int)); v.mem = h.mem; while( --numres) { - int * restrict tiledp = tilec->data; - int j; + OPJ_INT32 * restrict tiledp = tilec->data; + OPJ_UINT32 j; ++tr; h.sn = rw; @@ -547,14 +589,14 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1 for(j = 0; j < rh; ++j) { dwt_interleave_h(&h, &tiledp[j*w]); (dwt_1D)(&h); - memcpy(&tiledp[j*w], h.mem, rw * sizeof(int)); + memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32)); } v.dn = rh - v.sn; v.cas = tr->y0 % 2; for(j = 0; j < rw; ++j){ - int k; + OPJ_UINT32 k; dwt_interleave_v(&v, &tiledp[j], w); (dwt_1D)(&v); for(k = 0; k < rh; ++k) { @@ -563,61 +605,62 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1 } } opj_aligned_free(h.mem); + return true; } -static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, int size){ - float* restrict bi = (float*) (w->wavelet + w->cas); - int count = w->sn; - int i, k; +static void v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){ + OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas); + OPJ_INT32 count = w->sn; + OPJ_INT32 i, k; for(k = 0; k < 2; ++k){ for(i = 0; i < count; ++i){ - int j = i; + OPJ_INT32 j = i; bi[i*8 ] = a[j]; j += x; - if(j > size) continue; + if(j >= size) continue; bi[i*8 + 1] = a[j]; j += x; - if(j > size) continue; + if(j >= size) continue; bi[i*8 + 2] = a[j]; j += x; - if(j > size) continue; + if(j >= size) continue; bi[i*8 + 3] = a[j]; } - bi = (float*) (w->wavelet + 1 - w->cas); + bi = (OPJ_FLOAT32*) (w->wavelet + 1 - w->cas); a += w->sn; size -= w->sn; count = w->dn; } } -static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x){ +static void v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x){ v4* restrict bi = v->wavelet + v->cas; - int i; + OPJ_INT32 i; for(i = 0; i < v->sn; ++i){ - memcpy(&bi[i*2], &a[i*x], 4 * sizeof(float)); + memcpy(&bi[i*2], &a[i*x], 4 * sizeof(OPJ_FLOAT32)); } a += v->sn * x; bi = v->wavelet + 1 - v->cas; for(i = 0; i < v->dn; ++i){ - memcpy(&bi[i*2], &a[i*x], 4 * sizeof(float)); + memcpy(&bi[i*2], &a[i*x], 4 * sizeof(OPJ_FLOAT32)); } } #ifdef __SSE__ -static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){ +static void v4dwt_decode_step1_sse(v4* w, OPJ_INT32 count, const __m128 c){ __m128* restrict vw = (__m128*) w; - int i; + OPJ_INT32 i; for(i = 0; i < count; ++i){ __m128 tmp = vw[i*2]; vw[i*2] = tmp * c; } } -static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){ +static void v4dwt_decode_step2_sse(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c){ __m128* restrict vl = (__m128*) l; __m128* restrict vw = (__m128*) w; - int i; + OPJ_INT32 i; for(i = 0; i < m; ++i){ __m128 tmp1 = vl[ 0]; __m128 tmp2 = vw[-1]; @@ -640,14 +683,14 @@ static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){ #else -static void v4dwt_decode_step1(v4* w, int count, const float c){ - float* restrict fw = (float*) w; - int i; +static void v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c){ + OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w; + OPJ_INT32 i; for(i = 0; i < count; ++i){ - float tmp1 = fw[i*8 ]; - float tmp2 = fw[i*8 + 1]; - float tmp3 = fw[i*8 + 2]; - float tmp4 = fw[i*8 + 3]; + OPJ_FLOAT32 tmp1 = fw[i*8 ]; + OPJ_FLOAT32 tmp2 = fw[i*8 + 1]; + OPJ_FLOAT32 tmp3 = fw[i*8 + 2]; + OPJ_FLOAT32 tmp4 = fw[i*8 + 3]; fw[i*8 ] = tmp1 * c; fw[i*8 + 1] = tmp2 * c; fw[i*8 + 2] = tmp3 * c; @@ -655,23 +698,23 @@ static void v4dwt_decode_step1(v4* w, int count, const float c){ } } -static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){ - float* restrict fl = (float*) l; - float* restrict fw = (float*) w; - int i; +static void v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c){ + OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l; + OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w; + OPJ_INT32 i; for(i = 0; i < m; ++i){ - float tmp1_1 = fl[0]; - float tmp1_2 = fl[1]; - float tmp1_3 = fl[2]; - float tmp1_4 = fl[3]; - float tmp2_1 = fw[-4]; - float tmp2_2 = fw[-3]; - float tmp2_3 = fw[-2]; - float tmp2_4 = fw[-1]; - float tmp3_1 = fw[0]; - float tmp3_2 = fw[1]; - float tmp3_3 = fw[2]; - float tmp3_4 = fw[3]; + OPJ_FLOAT32 tmp1_1 = fl[0]; + OPJ_FLOAT32 tmp1_2 = fl[1]; + OPJ_FLOAT32 tmp1_3 = fl[2]; + OPJ_FLOAT32 tmp1_4 = fl[3]; + OPJ_FLOAT32 tmp2_1 = fw[-4]; + OPJ_FLOAT32 tmp2_2 = fw[-3]; + OPJ_FLOAT32 tmp2_3 = fw[-2]; + OPJ_FLOAT32 tmp2_4 = fw[-1]; + OPJ_FLOAT32 tmp3_1 = fw[0]; + OPJ_FLOAT32 tmp3_2 = fw[1]; + OPJ_FLOAT32 tmp3_3 = fw[2]; + OPJ_FLOAT32 tmp3_4 = fw[3]; fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c); fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c); fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c); @@ -680,20 +723,20 @@ static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){ fw += 8; } if(m < k){ - float c1; - float c2; - float c3; - float c4; + OPJ_FLOAT32 c1; + OPJ_FLOAT32 c2; + OPJ_FLOAT32 c3; + OPJ_FLOAT32 c4; c += c; c1 = fl[0] * c; c2 = fl[1] * c; c3 = fl[2] * c; c4 = fl[3] * c; for(; m < k; ++m){ - float tmp1 = fw[-4]; - float tmp2 = fw[-3]; - float tmp3 = fw[-2]; - float tmp4 = fw[-1]; + OPJ_FLOAT32 tmp1 = fw[-4]; + OPJ_FLOAT32 tmp2 = fw[-3]; + OPJ_FLOAT32 tmp3 = fw[-2]; + OPJ_FLOAT32 tmp4 = fw[-1]; fw[-4] = tmp1 + c1; fw[-3] = tmp2 + c2; fw[-2] = tmp3 + c3; @@ -709,7 +752,7 @@ static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){ /* Inverse 9-7 wavelet transform in 1-D. */ /* */ static void v4dwt_decode(v4dwt_t* restrict dwt){ - int a, b; + OPJ_INT32 a, b; if(dwt->cas == 0) { if(!((dwt->dn > 0) || (dwt->sn > 1))){ return; @@ -726,14 +769,14 @@ static void v4dwt_decode(v4dwt_t* restrict dwt){ #ifdef __SSE__ v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(K)); v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(c13318)); - v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_delta)); + v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(delta)); v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_gamma)); v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_beta)); v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_alpha)); #else v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, K); v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, c13318); - v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_delta); + v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), delta); v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_gamma); v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_beta); v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_alpha); @@ -743,24 +786,24 @@ static void v4dwt_decode(v4dwt_t* restrict dwt){ /* */ /* Inverse 9-7 wavelet transform in 2-D. */ /* */ -void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ +bool dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres){ v4dwt_t h; v4dwt_t v; opj_tcd_resolution_t* res = tilec->resolutions; - int rw = res->x1 - res->x0; /* width of the resolution level computed */ - int rh = res->y1 - res->y0; /* height of the resolution level computed */ + OPJ_UINT32 rw = res->x1 - res->x0; /* width of the resolution level computed */ + OPJ_UINT32 rh = res->y1 - res->y0; /* height of the resolution level computed */ - int w = tilec->x1 - tilec->x0; + OPJ_UINT32 w = tilec->x1 - tilec->x0; - h.wavelet = (v4*) opj_aligned_malloc((dwt_decode_max_resolution(res, numres)+5) * sizeof(v4)); + h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution(res, numres)+5) * sizeof(v4)); v.wavelet = h.wavelet; while( --numres) { - float * restrict aj = (float*) tilec->data; - int bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0); - int j; + OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data; + OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0); + OPJ_INT32 j; h.sn = rw; v.sn = rh; @@ -771,22 +814,26 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ rh = res->y1 - res->y0; /* height of the resolution level computed */ h.dn = rw - h.sn; - h.cas = res->x0 % 2; + h.cas = res->x0 & 1; for(j = rh; j > 0; j -= 4){ v4dwt_interleave_h(&h, aj, w, bufsize); v4dwt_decode(&h); if(j >= 4){ - int k; - for(k = rw; --k >= 0;){ + OPJ_INT32 k = rw; + while + (--k >= 0) + { aj[k ] = h.wavelet[k].f[0]; aj[k+w ] = h.wavelet[k].f[1]; aj[k+w*2] = h.wavelet[k].f[2]; aj[k+w*3] = h.wavelet[k].f[3]; } }else{ - int k; - for(k = rw; --k >= 0;){ + OPJ_INT32 k = rw; + while + (--k >= 0) + { switch(j) { case 3: aj[k+w*2] = h.wavelet[k].f[2]; case 2: aj[k+w ] = h.wavelet[k].f[1]; @@ -801,19 +848,19 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ v.dn = rh - v.sn; v.cas = res->y0 % 2; - aj = (float*) tilec->data; + aj = (OPJ_FLOAT32*) tilec->data; for(j = rw; j > 0; j -= 4){ v4dwt_interleave_v(&v, aj, w); v4dwt_decode(&v); if(j >= 4){ - int k; + OPJ_UINT32 k; for(k = 0; k < rh; ++k){ - memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float)); + memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32)); } }else{ - int k; + OPJ_UINT32 k; for(k = 0; k < rh; ++k){ - memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float)); + memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32)); } } aj += 4; @@ -821,5 +868,6 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ } opj_aligned_free(h.wavelet); + return true; } diff --git a/libopenjpeg/dwt.h b/libopenjpeg/dwt.h index adf73e54..d200b065 100644 --- a/libopenjpeg/dwt.h +++ b/libopenjpeg/dwt.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,6 +40,10 @@ The functions in DWT.C have for goal to realize forward and inverse discret wave transform with filter 5-3 (reversible) and filter 9-7 (irreversible). The functions in DWT.C are used by some function in TCD.C. */ +#include "openjpeg.h" + +struct opj_tcd_tilecomp; +struct opj_tccp; /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ /*@{*/ @@ -52,59 +57,59 @@ Forward 5-3 wavelet tranform in 2-D. Apply a reversible DWT transform to a component of an image. @param tilec Tile component information (current tile) */ -void dwt_encode(opj_tcd_tilecomp_t * tilec); +bool dwt_encode(struct opj_tcd_tilecomp * tilec); /** Inverse 5-3 wavelet tranform in 2-D. Apply a reversible inverse DWT transform to a component of an image. @param tilec Tile component information (current tile) @param numres Number of resolution levels to decode */ -void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres); +bool dwt_decode(struct opj_tcd_tilecomp* tilec, OPJ_UINT32 numres); /** Get the gain of a subband for the reversible 5-3 DWT. @param orient Number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH) @return Returns 0 if orient = 0, returns 1 if orient = 1 or 2, returns 2 otherwise */ -int dwt_getgain(int orient); +OPJ_UINT32 dwt_getgain(OPJ_UINT32 orient); /** Get the norm of a wavelet function of a subband at a specified level for the reversible 5-3 DWT. @param level Level of the wavelet function @param orient Band of the wavelet function @return Returns the norm of the wavelet function */ -double dwt_getnorm(int level, int orient); +OPJ_FLOAT64 dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient); /** Forward 9-7 wavelet transform in 2-D. Apply an irreversible DWT transform to a component of an image. @param tilec Tile component information (current tile) */ -void dwt_encode_real(opj_tcd_tilecomp_t * tilec); +bool dwt_encode_real(struct opj_tcd_tilecomp * tilec); /** Inverse 9-7 wavelet transform in 2-D. Apply an irreversible inverse DWT transform to a component of an image. @param tilec Tile component information (current tile) @param numres Number of resolution levels to decode */ -void dwt_decode_real(opj_tcd_tilecomp_t* tilec, int numres); +bool dwt_decode_real(struct opj_tcd_tilecomp* tilec, OPJ_UINT32 numres); /** Get the gain of a subband for the irreversible 9-7 DWT. @param orient Number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH) @return Returns the gain of the 9-7 wavelet transform */ -int dwt_getgain_real(int orient); +OPJ_UINT32 dwt_getgain_real(OPJ_UINT32 orient); /** Get the norm of a wavelet function of a subband at a specified level for the irreversible 9-7 DWT @param level Level of the wavelet function @param orient Band of the wavelet function @return Returns the norm of the 9-7 wavelet */ -double dwt_getnorm_real(int level, int orient); +OPJ_FLOAT64 dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient); /** Explicit calculation of the Quantization Stepsizes @param tccp Tile-component coding parameters @param prec Precint analyzed */ -void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec); +void dwt_calc_explicit_stepsizes(struct opj_tccp * tccp, OPJ_UINT32 prec); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/event.c b/libopenjpeg/event.c index 291ff585..eb40c6aa 100644 --- a/libopenjpeg/event.c +++ b/libopenjpeg/event.c @@ -24,84 +24,59 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "event.h" +#include "openjpeg.h" #include "opj_includes.h" + /* ========================================================== Utility functions ==========================================================*/ #if !defined(_MSC_VER) && !defined(__MINGW32__) -static char* -i2a(unsigned i, char *a, unsigned r) { +static OPJ_CHAR* +i2a(OPJ_UINT32 i, OPJ_CHAR *a, OPJ_UINT32 r) { if (i/r > 0) a = i2a(i/r,a,r); *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r]; return a+1; } - -/** - Transforms integer i into an ascii string and stores the result in a; - string is encoded in the base indicated by r. - @param i Number to be converted - @param a String result - @param r Base of value; must be in the range 2 - 36 - @return Returns a -*/ -static char * -_itoa(int i, char *a, int r) { - r = ((r < 2) || (r > 36)) ? 10 : r; - if(i < 0) { - *a = '-'; - *i2a(-i, a+1, r) = 0; - } - else *i2a(i, a, r) = 0; - return a; -} - -#endif /* !WIN32 */ - +#endif /* ----------------------------------------------------------------------- */ -opj_event_mgr_t* OPJ_CALLCONV opj_set_event_mgr(opj_common_ptr cinfo, opj_event_mgr_t *event_mgr, void *context) { - if(cinfo) { - opj_event_mgr_t *previous = cinfo->event_mgr; - cinfo->event_mgr = event_mgr; - cinfo->client_data = context; - return previous; - } - - return NULL; -} - -bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) { +bool opj_event_msg(opj_event_mgr_t * p_event_mgr, OPJ_INT32 event_type, const OPJ_CHAR *fmt, ...) { #define MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */ - opj_msg_callback msg_handler = NULL; + opj_msg_callback msg_handler = 00; + void * l_data = 00; - opj_event_mgr_t *event_mgr = cinfo->event_mgr; - if(event_mgr != NULL) { + + if(p_event_mgr != 00) { switch(event_type) { case EVT_ERROR: - msg_handler = event_mgr->error_handler; + msg_handler = p_event_mgr->error_handler; + l_data = p_event_mgr->m_error_data; break; case EVT_WARNING: - msg_handler = event_mgr->warning_handler; + msg_handler = p_event_mgr->warning_handler; + l_data = p_event_mgr->m_warning_data; break; case EVT_INFO: - msg_handler = event_mgr->info_handler; + msg_handler = p_event_mgr->info_handler; + l_data = p_event_mgr->m_info_data; break; default: break; } - if(msg_handler == NULL) { + if(msg_handler == 00) { return false; } } else { return false; } - if ((fmt != NULL) && (event_mgr != NULL)) { + if ((fmt != 00) && (p_event_mgr != 00)) { va_list arg; - int str_length/*, i, j*/; /* UniPG */ - char message[MSG_SIZE]; + OPJ_INT32 str_length/*, i, j*/; /* UniPG */ + OPJ_CHAR message[MSG_SIZE]; memset(message, 0, MSG_SIZE); /* initialize the optional parameter list */ va_start(arg, fmt); @@ -113,7 +88,7 @@ bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...) { va_end(arg); /* output the message to the user program */ - msg_handler(message, cinfo->client_data); + msg_handler(message, l_data); } return true; diff --git a/libopenjpeg/event.h b/libopenjpeg/event.h index 11910b0e..4c035a88 100644 --- a/libopenjpeg/event.h +++ b/libopenjpeg/event.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,12 +26,39 @@ */ #ifndef __EVENT_H #define __EVENT_H + +#include "openjpeg.h" + /** @file event.h @brief Implementation of a event callback system The functions in EVENT.C have for goal to send output messages (errors, warnings, debug) to the user. */ +/** +Message handler object +used for +
    +
  • Error messages +
  • Warning messages +
  • Debugging messages +
+*/ +typedef struct opj_event_mgr +{ + /** Data to call the event manager upon */ + void * m_error_data; + /** Data to call the event manager upon */ + void * m_warning_data; + /** Data to call the event manager upon */ + void * m_info_data; + /** Error message callback if available, NULL otherwise */ + opj_msg_callback error_handler; + /** Warning message callback if available, NULL otherwise */ + opj_msg_callback warning_handler; + /** Debug message callback if available, NULL otherwise */ + opj_msg_callback info_handler; +} opj_event_mgr_t; #define EVT_ERROR 1 /**< Error event type */ #define EVT_WARNING 2 /**< Warning event type */ @@ -43,13 +71,13 @@ The functions in EVENT.C have for goal to send output messages (errors, warnings /*@{*/ /* ----------------------------------------------------------------------- */ /** -Write formatted data to a string and send the string to a user callback. -@param cinfo Codec context info -@param event_type Event type or callback to use to send the message -@param fmt Format-control string (plus optionnal arguments) -@return Returns true if successful, returns false otherwise + * Writes formatted data to a string and send the string to a user callback. + * @param p_event_mgr the event manager to display messages. + * @param event_type Event type of the message + * @param fmt Format-control string (plus optionnal arguments) + * @return Returns true if successful, returns false otherwise */ -bool opj_event_msg(opj_common_ptr cinfo, int event_type, const char *fmt, ...); +bool opj_event_msg(struct opj_event_mgr * p_event_mgr, OPJ_INT32 event_type, const OPJ_CHAR *fmt, ...); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/fix.h b/libopenjpeg/fix.h index bcb2acb5..f4bb87f6 100644 --- a/libopenjpeg/fix.h +++ b/libopenjpeg/fix.h @@ -31,11 +31,8 @@ #ifndef __FIX_H #define __FIX_H -#if defined(_MSC_VER) || defined(__BORLANDC__) -#define int64 __int64 -#else -#define int64 long long -#endif +#include "openjpeg.h" +#include "opj_includes.h" /** @file fix.h @@ -43,7 +40,6 @@ The functions in FIX.H have for goal to realize specific multiplication. */ - /** @defgroup FIX FIX - Implementation of operations of specific multiplication */ /*@{*/ @@ -54,7 +50,7 @@ Multiply two fixed-precision rational numbers. @return Returns a * b */ static INLINE int fix_mul(int a, int b) { - int64 temp = (int64) a * (int64) b ; + OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ; temp += temp & 4096; return (int) (temp >> 13) ; } diff --git a/libopenjpeg/function_list.c b/libopenjpeg/function_list.c new file mode 100644 index 00000000..29eab611 --- /dev/null +++ b/libopenjpeg/function_list.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "function_list.h" +#include "opj_includes.h" +#include "opj_malloc.h" +/** + * Default size of the validation list, if not sufficient, data will be reallocated with a double size. + */ +#define OPJ_VALIDATION_SIZE 10 + +/** + * Creates a validation list. + * + * @return the newly created validation list. + */ +opj_procedure_list_t * opj_procedure_list_create() +{ + /* memory allocation */ + opj_procedure_list_t * l_validation = (opj_procedure_list_t *) opj_malloc(sizeof(opj_procedure_list_t)); + if + (! l_validation) + { + return 00; + } + /* initialization */ + memset(l_validation,0,sizeof(opj_procedure_list_t)); + l_validation->m_nb_max_procedures = OPJ_VALIDATION_SIZE; + l_validation->m_procedures = opj_malloc(OPJ_VALIDATION_SIZE * sizeof(opj_procedure)); + if + (! l_validation->m_procedures) + { + opj_free(l_validation); + return 00; + } + memset(l_validation->m_procedures,0,OPJ_VALIDATION_SIZE * sizeof(opj_procedure)); + return l_validation; +} + + + +/** + * Destroys a validation list. + * + * @param p_list the list to destroy. + */ +void opj_procedure_list_destroy(opj_procedure_list_t * p_list) +{ + if + (! p_list) + { + return; + } + /* initialization */ + if + (p_list->m_procedures) + { + opj_free(p_list->m_procedures); + } + opj_free(p_list); +} + +/** + * Adds a new validation procedure. + * + * @param p_validation_list the list of procedure to modify. + * @param p_procedure the procedure to add. + */ +bool opj_procedure_list_add_procedure (opj_procedure_list_t * p_validation_list, opj_procedure p_procedure) +{ + if + (p_validation_list->m_nb_max_procedures == p_validation_list->m_nb_procedures) + { + p_validation_list->m_nb_max_procedures += OPJ_VALIDATION_SIZE; + p_validation_list->m_procedures = opj_realloc(p_validation_list->m_procedures,p_validation_list->m_nb_max_procedures * sizeof(opj_procedure)); + if + (! p_validation_list->m_procedures) + { + p_validation_list->m_nb_max_procedures = 0; + p_validation_list->m_nb_procedures = 0; + return false; + } + } + p_validation_list->m_procedures[p_validation_list->m_nb_procedures] = p_procedure; + ++p_validation_list->m_nb_procedures; + return true; +} + +/** + * Gets the number of validation procedures. + * + * @param p_validation_list the list of procedure to modify. + * + * @return the number of validation procedures. + */ +OPJ_UINT32 opj_procedure_list_get_nb_procedures (opj_procedure_list_t * p_validation_list) +{ + return p_validation_list->m_nb_procedures; +} + +/** + * Gets the pointer on the first validation procedure. This function is similar to the C++ + * iterator class to iterate through all the procedures inside the validation list. + * the caller does not take ownership of the pointer. + * + * @param p_validation_list the list of procedure to get the first procedure from. + * + * @return a pointer to the first procedure. + */ +opj_procedure* opj_procedure_list_get_first_procedure (opj_procedure_list_t * p_validation_list) +{ + return p_validation_list->m_procedures; +} + +/** + * Clears the list of validation procedures. + * + * @param p_validation_list the list of procedure to clear. + * + */ +void opj_procedure_list_clear (opj_procedure_list_t * p_validation_list) +{ + p_validation_list->m_nb_procedures = 0; +} diff --git a/libopenjpeg/function_list.h b/libopenjpeg/function_list.h new file mode 100644 index 00000000..7cfedf10 --- /dev/null +++ b/libopenjpeg/function_list.h @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __FUNCTION_LIST_H +#define __FUNCTION_LIST_H + +/** + * @file function_list.h + * @brief Implementation of a list of procedures. + + * The functions in validation.c aims to have access to a list of procedures. +*/ + +/** @defgroup validation validation procedure*/ +/*@{*/ + +#include "openjpeg.h" +/************************************************************************************************** + ***************************************** FORWARD DECLARATION ************************************ + **************************************************************************************************/ +struct opj_jp2; + +/** + * ARGGGG, when will the template be added to the C language ??? + * in order not to have to duplicate the code in a vast number of times, use void * and downcast + * it after => UGLY but faster and easier + * TODO : make the class template in C++, use STL vector or duplicate code for each procedure type. + */ +typedef void * opj_procedure; + +/** + * A list of procedures. +*/ +typedef struct opj_procedure_list +{ + /** + * The number of validation procedures. + */ + OPJ_UINT32 m_nb_procedures; + /** + * The number of the array of validation procedures. + */ + OPJ_UINT32 m_nb_max_procedures; + /** + * The array of procedures. + */ + opj_procedure * m_procedures; + +} opj_procedure_list_t; + +/* ----------------------------------------------------------------------- */ + +/** + * Creates a validation list. + * + * @return the newly created validation list. + */ +opj_procedure_list_t * opj_procedure_list_create(); + +/** + * Destroys a validation list. + * + * @param p_list the list to destroy. + */ +void opj_procedure_list_destroy(opj_procedure_list_t * p_list); + +/** + * Adds a new validation procedure. + * + * @param p_validation_list the list of procedure to modify. + * @param p_procedure the procedure to add. + * + * @return true if the procedure could ne added. + */ +bool opj_procedure_list_add_procedure (opj_procedure_list_t * p_validation_list, opj_procedure p_procedure); + +/** + * Gets the number of validation procedures. + * + * @param p_validation_list the list of procedure to modify. + * + * @return the number of validation procedures. + */ +OPJ_UINT32 opj_procedure_list_get_nb_procedures (opj_procedure_list_t * p_validation_list); + +/** + * Gets the pointer on the first validation procedure. This function is similar to the C++ + * iterator class to iterate through all the procedures inside the validation list. + * the caller does not take ownership of the pointer. + * + * @param p_validation_list the list of procedure to get the first procedure from. + * + * @return a pointer to the first procedure. + */ +opj_procedure* opj_procedure_list_get_first_procedure (opj_procedure_list_t * p_validation_list); + + +/** + * Clears the list of validation procedures. + * + * @param p_validation_list the list of procedure to clear. + * + */ +void opj_procedure_list_clear (opj_procedure_list_t * p_validation_list); + + +#endif /* __FUNCTION_LIST_H */ + diff --git a/libopenjpeg/image.c b/libopenjpeg/image.c index 30b7d139..c6faffd9 100644 --- a/libopenjpeg/image.c +++ b/libopenjpeg/image.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -23,29 +24,38 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - -#include "opj_includes.h" +#include "image.h" +#include "openjpeg.h" +#include "opj_malloc.h" +#include "j2k.h" +#include "int.h" opj_image_t* opj_image_create0(void) { - opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t)); + opj_image_t *image = (opj_image_t*)opj_malloc(sizeof(opj_image_t)); + memset(image,0,sizeof(opj_image_t)); return image; } -opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { - int compno; - opj_image_t *image = NULL; +opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { + OPJ_UINT32 compno; + opj_image_t *image = 00; - image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t)); - if(image) { + image = (opj_image_t*) opj_malloc(sizeof(opj_image_t)); + if + (image) + { + memset(image,0,sizeof(opj_image_t)); image->color_space = clrspc; image->numcomps = numcmpts; /* allocate memory for the per-component information */ image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t)); - if(!image->comps) { - fprintf(stderr,"Unable to allocate memory for image.\n"); + if + (!image->comps) + { opj_image_destroy(image); - return NULL; + return 00; } + memset(image->comps,0,image->numcomps * sizeof(opj_image_comp_t)); /* create the individual image components */ for(compno = 0; compno < numcmpts; compno++) { opj_image_comp_t *comp = &image->comps[compno]; @@ -56,24 +66,64 @@ opj_image_t* OPJ_CALLCONV opj_image_create(int numcmpts, opj_image_cmptparm_t *c comp->x0 = cmptparms[compno].x0; comp->y0 = cmptparms[compno].y0; comp->prec = cmptparms[compno].prec; - comp->bpp = cmptparms[compno].bpp; comp->sgnd = cmptparms[compno].sgnd; - comp->data = (int*) opj_calloc(comp->w * comp->h, sizeof(int)); - if(!comp->data) { - fprintf(stderr,"Unable to allocate memory for image.\n"); + comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32)); + if + (!comp->data) + { opj_image_destroy(image); - return NULL; + return 00; } } } + return image; +} +opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) { + OPJ_UINT32 compno; + opj_image_t *image = 00; + + image = (opj_image_t*) opj_malloc(sizeof(opj_image_t)); + if + (image) + { + memset(image,0,sizeof(opj_image_t)); + image->color_space = clrspc; + image->numcomps = numcmpts; + /* allocate memory for the per-component information */ + image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t)); + if + (!image->comps) + { + opj_image_destroy(image); + return 00; + } + memset(image->comps,0,image->numcomps * sizeof(opj_image_comp_t)); + /* create the individual image components */ + for(compno = 0; compno < numcmpts; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + comp->dx = cmptparms[compno].dx; + comp->dy = cmptparms[compno].dy; + comp->w = cmptparms[compno].w; + comp->h = cmptparms[compno].h; + comp->x0 = cmptparms[compno].x0; + comp->y0 = cmptparms[compno].y0; + comp->prec = cmptparms[compno].prec; + comp->sgnd = cmptparms[compno].sgnd; + comp->data = 0; + } + } return image; } void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) { - int i; - if(image) { - if(image->comps) { + OPJ_UINT32 i; + if + (image) + { + if + (image->comps) + { /* image components */ for(i = 0; i < image->numcomps; i++) { opj_image_comp_t *image_comp = &image->comps[i]; @@ -87,3 +137,39 @@ void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) { } } +/** + * Updates the components of the image from the coding parameters. + * + * @param p_image the image to update. + * @param p_cp the coding parameters from which to update the image. + */ +void opj_image_comp_update(opj_image_t * p_image,const opj_cp_t * p_cp) +{ + OPJ_UINT32 i, l_width, l_height; + OPJ_INT32 l_x0,l_y0,l_x1,l_y1; + OPJ_INT32 l_comp_x0,l_comp_y0,l_comp_x1,l_comp_y1; + opj_image_comp_t * l_img_comp = 00; + + l_x0 = int_max(p_cp->tx0 , p_image->x0); + l_y0 = int_max(p_cp->ty0 , p_image->y0); + l_x1 = int_min(p_cp->tx0 + p_cp->tw * p_cp->tdx, p_image->x1); + l_y1 = int_min(p_cp->ty0 + p_cp->th * p_cp->tdy, p_image->y1); + + l_img_comp = p_image->comps; + for + (i = 0; i < p_image->numcomps; ++i) + { + l_comp_x0 = int_ceildiv(l_x0, l_img_comp->dx); + l_comp_y0 = int_ceildiv(l_y0, l_img_comp->dy); + l_comp_x1 = int_ceildiv(l_x1, l_img_comp->dx); + l_comp_y1 = int_ceildiv(l_y1, l_img_comp->dy); + l_width = int_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor); + l_height = int_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor); + l_img_comp->w = l_width; + l_img_comp->h = l_height; + l_img_comp->x0 = l_x0; + l_img_comp->y0 = l_y0; + ++l_img_comp; + } +} + diff --git a/libopenjpeg/image.h b/libopenjpeg/image.h index 04c362eb..91582543 100644 --- a/libopenjpeg/image.h +++ b/libopenjpeg/image.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,7 +32,8 @@ The functions in IMAGE.C have for goal to realize operations on images. */ - +struct opj_image; +struct opj_cp; /** @defgroup IMAGE IMAGE - Implementation of operations on images */ /*@{*/ @@ -40,7 +42,15 @@ Create an empty image @todo this function should be removed @return returns an empty image if successful, returns NULL otherwise */ -opj_image_t* opj_image_create0(void); +struct opj_image* opj_image_create0(void); + +/** + * Updates the components of the image from the coding parameters. + * + * @param p_image the image to update. + * @param p_cp the coding parameters from which to update the image. + */ +void opj_image_comp_update(struct opj_image * p_image,const struct opj_cp * p_cp); /*@}*/ diff --git a/libopenjpeg/int.h b/libopenjpeg/int.h index 4e5fe08e..0bc58fa9 100644 --- a/libopenjpeg/int.h +++ b/libopenjpeg/int.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,7 +37,8 @@ The functions in INT.H have for goal to realize operations on integers. */ - +#include "openjpeg.h" +#include "opj_includes.h" /** @defgroup INT INT - Implementation of operations on integers */ /*@{*/ @@ -47,14 +49,31 @@ The functions in INT.H have for goal to realize operations on integers. Get the minimum of two integers @return Returns a if a < b else b */ -static INLINE int int_min(int a, int b) { +static INLINE OPJ_INT32 int_min(OPJ_INT32 a, OPJ_INT32 b) { return a < b ? a : b; } + +/** +Get the minimum of two integers +@return Returns a if a < b else b +*/ +static INLINE OPJ_UINT32 uint_min(OPJ_UINT32 a, OPJ_UINT32 b) { + return a < b ? a : b; +} + /** Get the maximum of two integers @return Returns a if a > b else b */ -static INLINE int int_max(int a, int b) { +static INLINE OPJ_INT32 int_max(OPJ_INT32 a, OPJ_INT32 b) { + return (a > b) ? a : b; +} + +/** +Get the maximum of two integers +@return Returns a if a > b else b +*/ +static INLINE OPJ_UINT32 uint_max(OPJ_UINT32 a, OPJ_UINT32 b) { return (a > b) ? a : b; } /** @@ -66,7 +85,7 @@ Clamp an integer inside an interval
  • Returns min if (a < min) */ -static INLINE int int_clamp(int a, int min, int max) { +static INLINE OPJ_INT32 int_clamp(OPJ_INT32 a, OPJ_INT32 min, OPJ_INT32 max) { if (a < min) return min; if (a > max) @@ -76,41 +95,62 @@ static INLINE int int_clamp(int a, int min, int max) { /** @return Get absolute value of integer */ -static INLINE int int_abs(int a) { +static INLINE OPJ_INT32 int_abs(OPJ_INT32 a) { return a < 0 ? -a : a; } /** Divide an integer and round upwards @return Returns a divided by b */ -static INLINE int int_ceildiv(int a, int b) { +static INLINE OPJ_INT32 int_ceildiv(OPJ_INT32 a, OPJ_INT32 b) { + return (a + b - 1) / b; +} + +/** +Divide an integer and round upwards +@return Returns a divided by b +*/ +static INLINE OPJ_UINT32 uint_ceildiv(OPJ_UINT32 a, OPJ_UINT32 b) { return (a + b - 1) / b; } /** Divide an integer by a power of 2 and round upwards @return Returns a divided by 2^b */ -static INLINE int int_ceildivpow2(int a, int b) { +static INLINE OPJ_INT32 int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b) { return (a + (1 << b) - 1) >> b; } /** Divide an integer by a power of 2 and round downwards @return Returns a divided by 2^b */ -static INLINE int int_floordivpow2(int a, int b) { +static INLINE OPJ_INT32 int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b) { return a >> b; } /** Get logarithm of an integer and round downwards @return Returns log2(a) */ -static INLINE int int_floorlog2(int a) { - int l; +static INLINE OPJ_INT32 int_floorlog2(OPJ_INT32 a) { + OPJ_INT32 l; for (l = 0; a > 1; l++) { a >>= 1; } return l; } + +/** +Get logarithm of an integer and round downwards +@return Returns log2(a) +*/ +static INLINE OPJ_UINT32 uint_floorlog2(OPJ_UINT32 a) { + OPJ_UINT32 l; + for (l = 0; a > 1; ++l) + { + a >>= 1; + } + return l; +} /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/invert.c b/libopenjpeg/invert.c new file mode 100644 index 00000000..1fce6d7a --- /dev/null +++ b/libopenjpeg/invert.c @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "invert.h" +#include "opj_malloc.h" + + +bool opj_lupDecompose(OPJ_FLOAT32 * matrix,OPJ_UINT32 * permutations, OPJ_FLOAT32 * p_swap_area,OPJ_UINT32 n); +void opj_lupSolve(OPJ_FLOAT32 * pResult, OPJ_FLOAT32* pMatrix, OPJ_FLOAT32* pVector, OPJ_UINT32* pPermutations, OPJ_UINT32 n,OPJ_FLOAT32 * p_intermediate_data); +void opj_lupInvert (OPJ_FLOAT32 * pSrcMatrix, + OPJ_FLOAT32 * pDestMatrix, + OPJ_UINT32 n, + OPJ_UINT32 * pPermutations, + OPJ_FLOAT32 * p_src_temp, + OPJ_FLOAT32 * p_dest_temp, + OPJ_FLOAT32 * p_swap_area); + +/** + * Matrix inversion. + */ +bool opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix,OPJ_FLOAT32 * pDestMatrix, OPJ_UINT32 n) +{ + OPJ_BYTE * l_data = 00; + OPJ_UINT32 l_permutation_size = n * sizeof(OPJ_UINT32); + OPJ_UINT32 l_swap_size = n * sizeof(OPJ_FLOAT32); + OPJ_UINT32 l_total_size = l_permutation_size + 3 * l_swap_size; + OPJ_UINT32 * lPermutations = 00; + OPJ_FLOAT32 * l_double_data = 00; + + l_data = (OPJ_BYTE *) opj_malloc(l_total_size); + if + (l_data == 0) + { + return false; + } + lPermutations = (OPJ_UINT32 *) l_data; + l_double_data = (OPJ_FLOAT32 *) (l_data + l_permutation_size); + memset(lPermutations,0,l_permutation_size); + + if + (! opj_lupDecompose(pSrcMatrix,lPermutations,l_double_data,n)) + { + opj_free(l_data); + return false; + } + opj_lupInvert(pSrcMatrix,pDestMatrix,n,lPermutations,l_double_data,l_double_data + n,l_double_data + 2*n); + opj_free(l_data); + return true; +} + + +/** + * LUP decomposition + */ +bool opj_lupDecompose(OPJ_FLOAT32 * matrix,OPJ_UINT32 * permutations, OPJ_FLOAT32 * p_swap_area,OPJ_UINT32 n) +{ + OPJ_UINT32 * tmpPermutations = permutations; + OPJ_UINT32 * dstPermutations; + OPJ_UINT32 k2=0,t; + OPJ_FLOAT32 temp; + OPJ_UINT32 i,j,k; + OPJ_FLOAT32 p; + OPJ_UINT32 lLastColum = n - 1; + OPJ_UINT32 lSwapSize = n * sizeof(OPJ_FLOAT32); + OPJ_FLOAT32 * lTmpMatrix = matrix; + OPJ_FLOAT32 * lColumnMatrix,* lDestMatrix; + OPJ_UINT32 offset = 1; + OPJ_UINT32 lStride = n-1; + + //initialize permutations + for + (i = 0; i < n; ++i) + { + *tmpPermutations++ = i; + } + + + + // now make a pivot with colum switch + tmpPermutations = permutations; + for + (k = 0; k < lLastColum; ++k) + { + p = 0.0; + + // take the middle element + lColumnMatrix = lTmpMatrix + k; + + // make permutation with the biggest value in the column + for + (i = k; i < n; ++i) + { + temp = ((*lColumnMatrix > 0) ? *lColumnMatrix : -(*lColumnMatrix)); + if + (temp > p) + { + p = temp; + k2 = i; + } + // next line + lColumnMatrix += n; + } + + // a whole rest of 0 -> non singular + if + (p == 0.0) + { + return false; + } + + // should we permute ? + if + (k2 != k) + { + //exchange of line + // k2 > k + dstPermutations = tmpPermutations + k2 - k; + // swap indices + t = *tmpPermutations; + *tmpPermutations = *dstPermutations; + *dstPermutations = t; + + // and swap entire line. + lColumnMatrix = lTmpMatrix + (k2 - k) * n; + memcpy(p_swap_area,lColumnMatrix,lSwapSize); + memcpy(lColumnMatrix,lTmpMatrix,lSwapSize); + memcpy(lTmpMatrix,p_swap_area,lSwapSize); + } + + // now update data in the rest of the line and line after + lDestMatrix = lTmpMatrix + k; + lColumnMatrix = lDestMatrix + n; + // take the middle element + temp = *(lDestMatrix++); + + // now compute up data (i.e. coeff up of the diagonal). + for (i = offset; i < n; ++i) + { + //lColumnMatrix; + // divide the lower column elements by the diagonal value + + // matrix[i][k] /= matrix[k][k]; + // p = matrix[i][k] + p = *lColumnMatrix / temp; + *(lColumnMatrix++) = p; + for + (j = /* k + 1 */ offset; j < n; ++j) + { + // matrix[i][j] -= matrix[i][k] * matrix[k][j]; + *(lColumnMatrix++) -= p * (*(lDestMatrix++)); + } + // come back to the k+1th element + lDestMatrix -= lStride; + // go to kth element of the next line + lColumnMatrix += k; + } + // offset is now k+2 + ++offset; + // 1 element less for stride + --lStride; + // next line + lTmpMatrix+=n; + // next permutation element + ++tmpPermutations; + } + return true; +} + + + +/** + * LUP solving + */ +void opj_lupSolve (OPJ_FLOAT32 * pResult, OPJ_FLOAT32 * pMatrix, OPJ_FLOAT32 * pVector, OPJ_UINT32* pPermutations, OPJ_UINT32 n,OPJ_FLOAT32 * p_intermediate_data) +{ + OPJ_UINT32 i,j; + OPJ_FLOAT32 sum; + OPJ_FLOAT32 u; + OPJ_UINT32 lStride = n+1; + OPJ_FLOAT32 * lCurrentPtr; + OPJ_FLOAT32 * lIntermediatePtr; + OPJ_FLOAT32 * lDestPtr; + OPJ_FLOAT32 * lTmpMatrix; + OPJ_FLOAT32 * lLineMatrix = pMatrix; + OPJ_FLOAT32 * lBeginPtr = pResult + n - 1; + OPJ_FLOAT32 * lGeneratedData; + OPJ_UINT32 * lCurrentPermutationPtr = pPermutations; + + + lIntermediatePtr = p_intermediate_data; + lGeneratedData = p_intermediate_data + n - 1; + + for + (i = 0; i < n; ++i) + { + sum = 0.0; + lCurrentPtr = p_intermediate_data; + lTmpMatrix = lLineMatrix; + for + (j = 1; j <= i; ++j) + { + // sum += matrix[i][j-1] * y[j-1]; + sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++)); + } + //y[i] = pVector[pPermutations[i]] - sum; + *(lIntermediatePtr++) = pVector[*(lCurrentPermutationPtr++)] - sum; + lLineMatrix += n; + } + + // we take the last point of the matrix + lLineMatrix = pMatrix + n*n - 1; + + // and we take after the last point of the destination vector + lDestPtr = pResult + n; + + for + (i = n - 1; i != -1 ; --i) + { + sum = 0.0; + lTmpMatrix = lLineMatrix; + u = *(lTmpMatrix++); + lCurrentPtr = lDestPtr--; + for + (j = i + 1; j < n; ++j) + { + // sum += matrix[i][j] * x[j] + sum += (*(lTmpMatrix++)) * (*(lCurrentPtr++)); + } + //x[i] = (y[i] - sum) / u; + *(lBeginPtr--) = (*(lGeneratedData--) - sum) / u; + lLineMatrix -= lStride; + } +} + +/** LUP inversion (call with the result of lupDecompose) + */ +void opj_lupInvert ( + OPJ_FLOAT32 * pSrcMatrix, + OPJ_FLOAT32 * pDestMatrix, + OPJ_UINT32 n, + OPJ_UINT32 * pPermutations, + OPJ_FLOAT32 * p_src_temp, + OPJ_FLOAT32 * p_dest_temp, + OPJ_FLOAT32 * p_swap_area + ) +{ + OPJ_UINT32 j,i; + OPJ_FLOAT32 * lCurrentPtr; + OPJ_FLOAT32 * lLineMatrix = pDestMatrix; + OPJ_UINT32 lSwapSize = n * sizeof(OPJ_FLOAT32); + + for + (j = 0; j < n; ++j) + { + lCurrentPtr = lLineMatrix++; + memset(p_src_temp,0,lSwapSize); + p_src_temp[j] = 1.0; + opj_lupSolve(p_dest_temp,pSrcMatrix,p_src_temp, pPermutations, n , p_swap_area); + + for + (i = 0; i < n; ++i) + { + *(lCurrentPtr) = p_dest_temp[i]; + lCurrentPtr+=n; + } + } +} + diff --git a/indexer_JPIP/fix.c b/libopenjpeg/invert.h similarity index 67% rename from indexer_JPIP/fix.c rename to libopenjpeg/invert.h index 9699bf6b..8a39d363 100644 --- a/indexer_JPIP/fix.c +++ b/libopenjpeg/invert.h @@ -1,7 +1,5 @@ /* - * Copyright (c) 2001-2002, David Janssens - * Copyright (c) 2003, Yannick Verschueren - * Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,17 +24,17 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "fix.h" - -#ifdef WIN32 -#define int64 __int64 -#else -#define int64 long long +#ifndef __INVERT_H +#define __INVERT_H +#include "openjpeg.h" +/** + * Calculates a n x n double matrix inversion with a LUP method. Data is aligned, rows after rows (or columns after columns). + * The function does not take ownership of any memory block, data must be fred by the user. + * + * @param pSrcMatrix the matrix to invert. + * @param pDestMatrix data to store the inverted matrix. + * @return 1 if the inversion is successful, 0 if the matrix is singular. + */ +bool opj_matrix_inversion_f(OPJ_FLOAT32 * pSrcMatrix,OPJ_FLOAT32 * pDestMatrix, OPJ_UINT32 n); #endif -/// -/// Multiply two fixed-precision rational numbers. -/// -int fix_mul(int a, int b) { - return (int)((int64)a*(int64)b>>13); -} diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c index 563efd68..451d57e0 100644 --- a/libopenjpeg/j2k.c +++ b/libopenjpeg/j2k.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,216 +31,1224 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "j2k.h" +#include "opj_malloc.h" #include "opj_includes.h" +#include "pi.h" +#include "event.h" +#include "cio.h" +#include "int.h" +#include "tcd.h" +#include "function_list.h" +#include "invert.h" +#include "dwt.h" +#include "mct.h" +#include "image.h" /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ /*@{*/ + +/*************************************************************************** + ********************** TYPEDEFS ******************************************* + ***************************************************************************/ +/** + * Correspondance prog order <-> string representation + */ +typedef struct j2k_prog_order +{ + OPJ_PROG_ORDER enum_prog; + OPJ_CHAR str_prog[4]; +} +j2k_prog_order_t; + +typedef struct opj_dec_memory_marker_handler +{ + /** marker value */ + OPJ_UINT32 id; + /** value of the state when the marker can appear */ + OPJ_UINT32 states; + /** action linked to the marker */ + bool (*handler) ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +} +opj_dec_memory_marker_handler_t; + + + /** @name Local static functions */ /*@{*/ +/** + * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. + * + * @param p_comp_no the component number to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + * +*/ +static bool j2k_write_SPCod_SPCoc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ); /** -Write the SOC marker (Start Of Codestream) -@param j2k J2K handle + * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. + * @param p_header_data the data contained in the COM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ -static void j2k_write_soc(opj_j2k_t *j2k); +static bool j2k_read_SPCod_SPCoc( + opj_j2k_t *p_j2k, + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ); + /** -Read the SOC marker (Start of Codestream) -@param j2k J2K handle -*/ -static void j2k_read_soc(opj_j2k_t *j2k); + * Gets the size taken by writting a SPCod or SPCoc for the given tile and component. + * + * @param p_tile_no the tile indix. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. + * + * @return the number of bytes taken by the SPCod element. + */ +static OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no + ); + /** -Write the SIZ marker (image and tile size) -@param j2k J2K handle + * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + * */ -static void j2k_write_siz(opj_j2k_t *j2k); +static bool j2k_write_SQcd_SQcc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ); + /** -Read the SIZ marker (image and tile size) -@param j2k J2K handle + * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + * */ -static void j2k_read_siz(opj_j2k_t *j2k); +static bool j2k_read_SQcd_SQcc( + opj_j2k_t *p_j2k, + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ); /** -Write the COM marker (comment) -@param j2k J2K handle -*/ -static void j2k_write_com(opj_j2k_t *j2k); + * Updates the Tile Length Marker. + */ +static void j2k_update_tlm ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_part_size); + /** -Read the COM marker (comment) -@param j2k J2K handle -*/ -static void j2k_read_com(opj_j2k_t *j2k); + * Gets the size taken by writting SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_tile_no the tile indix. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. + * + * @return the number of bytes taken by the SPCod element. + */ +static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no + + ); + /** -Write the value concerning the specified component in the marker COD and COC -@param j2k J2K handle -@param compno Number of the component concerned by the information written -*/ -static void j2k_write_cox(opj_j2k_t *j2k, int compno); + * Copies the tile component parameters of all the component from the first tile component. + * + * @param p_j2k the J2k codec. + */ +static void j2k_copy_tile_component_parameters( + opj_j2k_t *p_j2k + ); + /** -Read the value concerning the specified component in the marker COD and COC -@param j2k J2K handle -@param compno Number of the component concerned by the information read + * Writes the SOC marker (Start Of Codestream) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_read_cox(opj_j2k_t *j2k, int compno); + +static bool j2k_write_soc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); /** -Write the COD marker (coding style default) -@param j2k J2K handle + * Reads a SOC marker (Start of Codestream) + * @param p_header_data the data contained in the SOC box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SOC marker. + * @param p_manager the user event manager. */ -static void j2k_write_cod(opj_j2k_t *j2k); +static bool j2k_read_soc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); /** -Read the COD marker (coding style default) -@param j2k J2K handle + * Writes the SIZ marker (image and tile size) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_read_cod(opj_j2k_t *j2k); +static bool j2k_write_siz( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); /** -Write the COC marker (coding style component) -@param j2k J2K handle -@param compno Number of the component concerned by the information written + * Writes the CBD-MCT-MCC-MCO markers (Multi components transform) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_write_coc(opj_j2k_t *j2k, int compno); +static bool j2k_write_mct_data_group( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + /** -Read the COC marker (coding style component) -@param j2k J2K handle + * Reads a SIZ marker (image and tile size) + * @param p_header_data the data contained in the SIZ box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SIZ marker. + * @param p_manager the user event manager. */ -static void j2k_read_coc(opj_j2k_t *j2k); +static bool j2k_read_siz ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); /** -Write the value concerning the specified component in the marker QCD and QCC -@param j2k J2K handle -@param compno Number of the component concerned by the information written + * Writes the COM marker (comment) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_write_qcx(opj_j2k_t *j2k, int compno); +static bool j2k_write_com( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); /** -Read the value concerning the specified component in the marker QCD and QCC -@param j2k J2K handle -@param compno Number of the component concern by the information read -@param len Length of the information in the QCX part of the marker QCD/QCC + * Reads a COM marker (comments) + * @param p_header_data the data contained in the COM box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. */ -static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len); +static bool j2k_read_com ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); + + + /** -Write the QCD marker (quantization default) -@param j2k J2K handle + * Writes the COD marker (Coding style default) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_write_qcd(opj_j2k_t *j2k); +static bool j2k_write_cod( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); /** -Read the QCD marker (quantization default) -@param j2k J2K handle + * Reads a COD marker (Coding Styke defaults) + * @param p_header_data the data contained in the COD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COD marker. + * @param p_manager the user event manager. */ -static void j2k_read_qcd(opj_j2k_t *j2k); +static bool j2k_read_cod ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); + /** -Write the QCC marker (quantization component) -@param j2k J2K handle -@param compno Number of the component concerned by the information written + * Writes the COC marker (Coding style component) + * + * @param p_comp_number the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_write_qcc(opj_j2k_t *j2k, int compno); +static bool j2k_write_coc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_number, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + /** -Read the QCC marker (quantization component) -@param j2k J2K handle + * Writes the COC marker (Coding style component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_read_qcc(opj_j2k_t *j2k); +static void j2k_write_coc_in_memory( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + struct opj_event_mgr * p_manager + ); /** -Write the POC marker (progression order change) -@param j2k J2K handle -*/ -static void j2k_write_poc(opj_j2k_t *j2k); + * Gets the maximum size taken by a coc. + * + * @param p_j2k the jpeg2000 codec to use. + */ +static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_t *p_j2k); + /** -Read the POC marker (progression order change) -@param j2k J2K handle + * Reads a COC marker (Coding Style Component) + * @param p_header_data the data contained in the COC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COC marker. + * @param p_manager the user event manager. */ -static void j2k_read_poc(opj_j2k_t *j2k); +static bool j2k_read_coc ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); + /** -Read the CRG marker (component registration) -@param j2k J2K handle + * Writes the QCD marker (quantization default) + * + * @param p_comp_number the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_read_crg(opj_j2k_t *j2k); +static bool j2k_write_qcd( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + + /** -Read the TLM marker (tile-part lengths) -@param j2k J2K handle + * Reads a QCD marker (Quantization defaults) + * @param p_header_data the data contained in the QCD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCD marker. + * @param p_manager the user event manager. */ -static void j2k_read_tlm(opj_j2k_t *j2k); +static bool j2k_read_qcd ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); /** -Read the PLM marker (packet length, main header) -@param j2k J2K handle + * Writes the QCC marker (quantization component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_read_plm(opj_j2k_t *j2k); +static bool j2k_write_qcc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); /** -Read the PLT marker (packet length, tile-part header) -@param j2k J2K handle + * Writes the QCC marker (quantization component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_read_plt(opj_j2k_t *j2k); +static void j2k_write_qcc_in_memory( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + struct opj_event_mgr * p_manager + ); /** -Read the PPM marker (packet packet headers, main header) -@param j2k J2K handle -*/ -static void j2k_read_ppm(opj_j2k_t *j2k); + * Gets the maximum size taken by a qcc. + */ +static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_t *p_j2k); + /** -Read the PPT marker (packet packet headers, tile-part header) -@param j2k J2K handle + * Reads a QCC marker (Quantization component) + * @param p_header_data the data contained in the QCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCC marker. + * @param p_manager the user event manager. */ -static void j2k_read_ppt(opj_j2k_t *j2k); +static bool j2k_read_qcc( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager); /** -Write the TLM marker (Mainheader) -@param j2k J2K handle + * Writes the POC marker (Progression Order Change) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_write_tlm(opj_j2k_t *j2k); +static bool j2k_write_poc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + /** -Write the SOT marker (start of tile-part) -@param j2k J2K handle + * Writes the updated tlm. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. */ -static void j2k_write_sot(opj_j2k_t *j2k); +static bool j2k_write_updated_tlm( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + /** -Read the SOT marker (start of tile-part) -@param j2k J2K handle -*/ -static void j2k_read_sot(opj_j2k_t *j2k); + * Writes the POC marker (Progression Order Change) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + */ +static void j2k_write_poc_in_memory( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + struct opj_event_mgr * p_manager + ); + /** -Write the SOD marker (start of data) -@param j2k J2K handle -@param tile_coder Pointer to a TCD handle -*/ -static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder); + * Gets the maximum size taken by the writting of a POC. + */ +static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_t *p_j2k); + /** -Read the SOD marker (start of data) -@param j2k J2K handle -*/ -static void j2k_read_sod(opj_j2k_t *j2k); + * Gets the maximum size taken by the toc headers of all the tile parts of any given tile. + */ +static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_t *p_j2k); + /** -Write the RGN marker (region-of-interest) -@param j2k J2K handle -@param compno Number of the component concerned by the information written -@param tileno Number of the tile concerned by the information written -*/ -static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno); + * Gets the maximum size taken by the headers of the SOT. + * + * @param p_j2k the jpeg2000 codec to use. + */ +static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_t *p_j2k); + /** -Read the RGN marker (region-of-interest) -@param j2k J2K handle + * Reads a POC marker (Progression Order Change) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. */ -static void j2k_read_rgn(opj_j2k_t *j2k); +static bool j2k_read_poc ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); /** -Write the EOC marker (end of codestream) -@param j2k J2K handle + * Reads a CRG marker (Component registration) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ -static void j2k_write_eoc(opj_j2k_t *j2k); +static bool j2k_read_crg ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); /** -Read the EOC marker (end of codestream) -@param j2k J2K handle + * Reads a TLM marker (Tile Length Marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ -static void j2k_read_eoc(opj_j2k_t *j2k); +static bool j2k_read_tlm ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); /** -Read an unknown marker -@param j2k J2K handle + * Reads a PLM marker (Packet length, main header marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. */ -static void j2k_read_unk(opj_j2k_t *j2k); +static bool j2k_read_plm ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +/** + * Reads a PLT marker (Packet length, tile-part header) + * + * @param p_header_data the data contained in the PLT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PLT marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_plt ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +/** + * Reads a PPM marker (Packed packet headers, main header) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_ppm ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +/** + * Reads a PPT marker (Packed packet headers, tile-part header) + * + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_ppt ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +/** + * Writes the TLM marker (Tile Length Marker) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_tlm( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Writes the SOT marker (Start of tile-part) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_sot( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + const struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Reads a PPT marker (Packed packet headers, tile-part header) + * + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_sot ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +/** + * Writes the SOD marker (Start of data) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_sod( + opj_j2k_t *p_j2k, + struct opj_tcd * p_tile_coder, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + const struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Reads a SOD marker (Start Of Data) + * + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_sod ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Writes the RGN marker (Region Of Interest) + * + * @param p_tile_no the tile to output + * @param p_comp_no the component to output + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_rgn( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Reads a RGN marker (Region Of Interest) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_rgn ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) ; +/** + * Writes the EOC marker (End of Codestream) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_eoc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Copies the tile component parameters of all the component from the first tile component. + * + * @param p_j2k the J2k codec. + */ +static void j2k_copy_tile_quantization_parameters( + opj_j2k_t *p_j2k + ); + +/** + * Reads a EOC marker (End Of Codestream) + * + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_eoc ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) ; + +/** + * Inits the Info + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_init_info( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Reads an unknown marker + * + * @param p_stream the stream object to read from. + * @param p_j2k the jpeg2000 codec. + * @param p_manager the user event manager. + * + * @return true if the marker could be deduced. +*/ +static bool j2k_read_unk ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Ends the encoding, i.e. frees memory. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_end_encoding( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Writes the CBD marker (Component bit depth definition) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_cbd( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a CBD marker (Component bit depth definition) + * @param p_header_data the data contained in the CBD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the CBD marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_cbd ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager); + +/** + * Writes the MCT marker (Multiple Component Transform) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_mct_record( + opj_j2k_t *p_j2k, + opj_mct_data_t * p_mct_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a MCT marker (Multiple Component Transform) + * + * @param p_header_data the data contained in the MCT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCT marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_mct ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Writes the MCC marker (Multiple Component Collection) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_mcc_record( + opj_j2k_t *p_j2k, + struct opj_simple_mcc_decorrelation_data * p_mcc_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a MCC marker (Multiple Component Collection) + * + * @param p_header_data the data contained in the MCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCC marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_mcc ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Writes the MCO marker (Multiple component transformation ordering) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_mco( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a MCO marker (Multiple Component Transform Ordering) + * + * @param p_header_data the data contained in the MCO box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCO marker. + * @param p_manager the user event manager. +*/ +static bool j2k_read_mco ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ); +/** + * Writes the image components. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_image_components( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Writes regions of interests. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_regions( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Writes EPC ???? + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_write_epc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Checks the progression order changes values. Tells of the poc given as input are valid. + * A nice message is outputted at errors. + * + * @param p_pocs the progression order changes. + * @param p_nb_pocs the number of progression order changes. + * @param p_nb_resolutions the number of resolutions. + * @param numcomps the number of components + * @param numlayers the number of layers. + * + * @return true if the pocs are valid. + */ +static bool j2k_check_poc_val( + const opj_poc_t *p_pocs, + OPJ_UINT32 p_nb_pocs, + OPJ_UINT32 p_nb_resolutions, + OPJ_UINT32 numcomps, + OPJ_UINT32 numlayers, + opj_event_mgr_t * p_manager); + +/** + * Gets the number of tile parts used for the given change of progression (if any) and the given tile. + * + * @param cp the coding parameters. + * @param pino the offset of the given poc (i.e. its position in the coding parameter). + * @param tileno the given tile. + * + * @return the number of tile parts. + */ +static OPJ_UINT32 j2k_get_num_tp( + opj_cp_t *cp, + OPJ_UINT32 pino, + OPJ_UINT32 tileno); +/** + * Calculates the total number of tile parts needed by the encoder to + * encode such an image. If not enough memory is available, then the function return false. + * + * @param p_nb_tiles pointer that will hold the number of tile parts. + * @param cp the coding parameters for the image. + * @param image the image to encode. + * @param p_j2k the p_j2k encoder. + * @param p_manager the user event manager. + * + * @return true if the function was successful, false else. + */ +static bool j2k_calculate_tp( + opj_j2k_t *p_j2k, + opj_cp_t *cp, + OPJ_UINT32 * p_nb_tiles, + opj_image_t *image, + opj_event_mgr_t * p_manager); + +static bool j2k_write_first_tile_part ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager + ); +static bool j2k_write_all_tile_parts( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Reads the lookup table containing all the marker, status and action, and returns the handler associated + * with the marker value. + * @param p_id Marker value to look up + * + * @return the handler associated with the id. +*/ +static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id); + +/** + * Destroys a tile coding parameter structure. + * + * @param p_tcp the tile coding parameter to destroy. + */ +static void j2k_tcp_destroy (opj_tcp_t *p_tcp); + +static void j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data); + +/** + * Destroys a coding parameter structure. + * + * @param p_cp the coding parameter to destroy. + */ +static void j2k_cp_destroy (opj_cp_t *p_cp); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static void j2k_setup_encoding_validation (opj_j2k_t *p_j2k); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static void j2k_setup_decoding_validation (opj_j2k_t *p_j2k); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static void j2k_setup_end_compress (opj_j2k_t *p_j2k); + +/** + * Creates a tile-coder decoder. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_create_tcd( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * Excutes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param p_j2k the jpeg2000 codec to execute the procedures on. + * @param p_stream the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +static bool j2k_exec ( + opj_j2k_t * p_j2k, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * Updates the rates of the tcp. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_update_rates( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +/** + * The default encoding validation procedure without any extension. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool j2k_encoding_validation ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * The read header procedure. + */ +bool j2k_read_header_procedure( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager); + +/** + * The default decoding validation procedure without any extension. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool j2k_decoding_validation ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * Reads the tiles. + */ +bool j2k_decode_tiles ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager); + +/** + * The mct encoding validation procedure. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool j2k_mct_validation ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * Builds the tcd decoder to use to decode tile. + */ +bool j2k_build_decoder ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * Builds the tcd encoder to use to encode tile. + */ +bool j2k_build_encoder ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * Copies the decoding tile parameters onto all the tile parameters. + * Creates also the tile decoder. + */ +bool j2k_copy_default_tcp_and_create_tcd( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +/** + * Destroys the memory associated with the decoding of headers. + */ +bool j2k_destroy_header_memory ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); + +/** + * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures. + */ +void j2k_setup_header_writting (opj_j2k_t *p_j2k); + +/** + * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures. + */ +void j2k_setup_header_reading (opj_j2k_t *p_j2k); + +/** + * Writes a tile. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +static bool j2k_post_write_tile ( + opj_j2k_t * p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); + +static bool j2k_pre_write_tile ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ); +static bool j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data); + +static bool j2k_add_mct(opj_tcp_t * p_tcp,opj_image_t * p_image, OPJ_UINT32 p_index); +/** + * Gets the offset of the header. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +static bool j2k_get_end_header( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + +static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); + +static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); + +static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); +static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); + + /*@}*/ /*@}*/ /* ----------------------------------------------------------------------- */ -typedef struct j2k_prog_order{ - OPJ_PROG_ORDER enum_prog; - char str_prog[4]; -}j2k_prog_order_t; -j2k_prog_order_t j2k_prog_order_list[] = { + + +/**************************************************************************** + ********************* CONSTANTS ******************************************** + ****************************************************************************/ + + + + +/** + * List of progression orders. + */ +const j2k_prog_order_t j2k_prog_order_list [] = +{ {CPRL, "CPRL"}, {LRCP, "LRCP"}, {PCRL, "PCRL"}, @@ -248,264 +1257,884 @@ j2k_prog_order_t j2k_prog_order_list[] = { {-1, ""} }; -char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){ - j2k_prog_order_t *po; - for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){ - if(po->enum_prog == prg_order){ - break; +const OPJ_UINT32 MCT_ELEMENT_SIZE [] = +{ + 2, + 4, + 4, + 8 +}; + +typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem); + +const j2k_mct_function j2k_mct_read_functions_to_float [] = +{ + j2k_read_int16_to_float, + j2k_read_int32_to_float, + j2k_read_float32_to_float, + j2k_read_float64_to_float +}; + +const j2k_mct_function j2k_mct_read_functions_to_int32 [] = +{ + j2k_read_int16_to_int32, + j2k_read_int32_to_int32, + j2k_read_float32_to_int32, + j2k_read_float64_to_int32 +}; + +const j2k_mct_function j2k_mct_write_functions_from_float [] = +{ + j2k_write_float_to_int16, + j2k_write_float_to_int32, + j2k_write_float_to_float, + j2k_write_float_to_float64 +}; + + + + +/*const opj_dec_stream_marker_handler_t j2k_stream_marker_handler_tab[] = +{ + {J2K_MS_SOC, J2K_DEC_STATE_MHSOC, j2k_read_soc}, + {J2K_MS_SOD, J2K_DEC_STATE_TPH, j2k_read_sod}, + {J2K_MS_EOC, J2K_DEC_STATE_TPHSOT, j2k_read_eoc}, + {J2K_MS_SOP, 0, 0}, +#ifdef USE_JPWL + {J2K_MS_EPC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epc}, + {J2K_MS_EPB, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epb}, + {J2K_MS_ESD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_esd}, + {J2K_MS_RED, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_red}, +#endif +#ifdef USE_JPSEC + {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, + {J2K_MS_INSEC, 0, j2k_read_insec}, +#endif + + {0, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_unk} +};*/ + +const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] = +{ + {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot}, + {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod}, + {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc}, + {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn}, + {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd}, + {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc}, + {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc}, + {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz}, + {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm}, + {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm}, + {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt}, + {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm}, + {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt}, + {J2K_MS_SOP, 0, 0}, + {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg}, + {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com}, + {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct}, + {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd}, + {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc}, + {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco} +#ifdef USE_JPWL + {J2K_MS_EPC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epc}, + {J2K_MS_EPB, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epb}, + {J2K_MS_ESD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_esd}, + {J2K_MS_RED, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_red}, +#endif /* USE_JPWL */ +#ifdef USE_JPSEC + {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec}, + {J2K_MS_INSEC, 0, j2k_read_insec} +#endif /* USE_JPSEC */ +}; + +void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem) +{ + OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data; + OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data; + OPJ_UINT32 i; + OPJ_UINT32 l_temp; + + for + (i=0;ienum_prog != -1; ++po ) + { + if + (po->enum_prog == p_prg_order) + { + return po->str_prog; } } return po->str_prog; } -void j2k_dump_image(FILE *fd, opj_image_t * img) { - int compno; - fprintf(fd, "image {\n"); - fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1); - fprintf(fd, " numcomps=%d\n", img->numcomps); - for (compno = 0; compno < img->numcomps; compno++) { - opj_image_comp_t *comp = &img->comps[compno]; - fprintf(fd, " comp %d {\n", compno); - fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy); - fprintf(fd, " prec=%d\n", comp->prec); - fprintf(fd, " sgnd=%d\n", comp->sgnd); - fprintf(fd, " }\n"); + + + + + + +/** + * Checks the progression order changes values. Tells if the poc given as input are valid. + * + * @param p_pocs the progression order changes. + * @param p_nb_pocs the number of progression order changes. + * @param p_nb_resolutions the number of resolutions. + * @param numcomps the number of components + * @param numlayers the number of layers. + * @param p_manager the user event manager. + * + * @return true if the pocs are valid. + */ +bool j2k_check_poc_val(const opj_poc_t *p_pocs, OPJ_UINT32 p_nb_pocs, OPJ_UINT32 p_nb_resolutions, OPJ_UINT32 p_num_comps, OPJ_UINT32 p_num_layers, opj_event_mgr_t * p_manager) +{ + OPJ_UINT32* packet_array; + OPJ_UINT32 index , resno, compno, layno; + OPJ_UINT32 i; + OPJ_UINT32 step_c = 1; + OPJ_UINT32 step_r = p_num_comps * step_c; + OPJ_UINT32 step_l = p_nb_resolutions * step_r; + bool loss = false; + OPJ_UINT32 layno0 = 0; + + packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32)); + if + (packet_array == 00) + { + opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n"); + return false; } - fprintf(fd, "}\n"); + memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32)); + if + (p_nb_pocs == 0) + { + return true; + } + + index = step_r * p_pocs->resno0; + // take each resolution for each poc + for + (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) + { + OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; + // take each comp of each resolution for each poc + for + (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) + { + OPJ_UINT32 comp_index = res_index + layno0 * step_l; + // and finally take each layer of each res of ... + for + (layno = layno0; layno < p_pocs->layno1 ; ++layno) + { + //index = step_r * resno + step_c * compno + step_l * layno; + packet_array[comp_index] = 1; + comp_index += step_l; + } + res_index += step_c; + } + index += step_r; + } + ++p_pocs; + // iterate through all the pocs + for + (i = 1; i < p_nb_pocs ; ++i) + { + OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ; + layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0; + index = step_r * p_pocs->resno0; + // take each resolution for each poc + for + (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) + { + OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c; + // take each comp of each resolution for each poc + for + (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) + { + OPJ_UINT32 comp_index = res_index + layno0 * step_l; + // and finally take each layer of each res of ... + for + (layno = layno0; layno < p_pocs->layno1 ; ++layno) + { + //index = step_r * resno + step_c * compno + step_l * layno; + packet_array[comp_index] = 1; + comp_index += step_l; + } + res_index += step_c; + } + index += step_r; + } + ++p_pocs; + } + + index = 0; + for + (layno = 0; layno < p_num_layers ; ++layno) + { + for + (resno = 0; resno < p_nb_resolutions; ++resno) + { + for + (compno = 0; compno < p_num_comps; ++compno) + { + loss |= (packet_array[index]!=1); + //index = step_r * resno + step_c * compno + step_l * layno; + index += step_c; + } + } + } + if + (loss) + { + opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n"); + } + opj_free(packet_array); + return !loss; } -void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) { - int tileno, compno, layno, bandno, resno, numbands; - fprintf(fd, "coding parameters {\n"); - fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0); - fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy); - fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th); - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - opj_tcp_t *tcp = &cp->tcps[tileno]; - fprintf(fd, " tile %d {\n", tileno); - fprintf(fd, " csty=%x\n", tcp->csty); - fprintf(fd, " prg=%d\n", tcp->prg); - fprintf(fd, " numlayers=%d\n", tcp->numlayers); - fprintf(fd, " mct=%d\n", tcp->mct); - fprintf(fd, " rates="); - for (layno = 0; layno < tcp->numlayers; layno++) { - fprintf(fd, "%.1f ", tcp->rates[layno]); - } - fprintf(fd, "\n"); - for (compno = 0; compno < img->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - fprintf(fd, " comp %d {\n", compno); - fprintf(fd, " csty=%x\n", tccp->csty); - fprintf(fd, " numresolutions=%d\n", tccp->numresolutions); - fprintf(fd, " cblkw=%d\n", tccp->cblkw); - fprintf(fd, " cblkh=%d\n", tccp->cblkh); - fprintf(fd, " cblksty=%x\n", tccp->cblksty); - fprintf(fd, " qmfbid=%d\n", tccp->qmfbid); - fprintf(fd, " qntsty=%d\n", tccp->qntsty); - fprintf(fd, " numgbits=%d\n", tccp->numgbits); - fprintf(fd, " roishift=%d\n", tccp->roishift); - fprintf(fd, " stepsizes="); - numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; - for (bandno = 0; bandno < numbands; bandno++) { - fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant, - tccp->stepsizes[bandno].expn); - } - fprintf(fd, "\n"); - - if (tccp->csty & J2K_CCP_CSTY_PRT) { - fprintf(fd, " prcw="); - for (resno = 0; resno < tccp->numresolutions; resno++) { - fprintf(fd, "%d ", tccp->prcw[resno]); - } - fprintf(fd, "\n"); - fprintf(fd, " prch="); - for (resno = 0; resno < tccp->numresolutions; resno++) { - fprintf(fd, "%d ", tccp->prch[resno]); - } - fprintf(fd, "\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); -} /* ----------------------------------------------------------------------- */ -static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){ - char *prog; - int i; - int tpnum=1,tpend=0; - opj_tcp_t *tcp = &cp->tcps[tileno]; + +/** + * Gets the number of tile parts used for the given change of progression (if any) and the given tile. + * + * @param cp the coding parameters. + * @param pino the offset of the given poc (i.e. its position in the coding parameter). + * @param tileno the given tile. + * + * @return the number of tile parts. + */ +OPJ_UINT32 j2k_get_num_tp(opj_cp_t *cp,OPJ_UINT32 pino,OPJ_UINT32 tileno) +{ + const OPJ_CHAR *prog = 00; + OPJ_UINT32 i; + OPJ_UINT32 tpnum = 1; + opj_tcp_t *tcp = 00; + opj_poc_t * l_current_poc = 00; + + // preconditions only in debug + assert(tileno < (cp->tw * cp->th)); + assert(pino < (cp->tcps[tileno].numpocs + 1)); + + // get the given tile coding parameter + tcp = &cp->tcps[tileno]; + assert(tcp != 00); + l_current_poc = &(tcp->pocs[pino]); + assert(l_current_poc != 0); + + // get the progression order as a character string prog = j2k_convert_progression_order(tcp->prg); + assert(strlen(prog) > 0); - if(cp->tp_on == 1){ - for(i=0;i<4;i++){ - if(tpend!=1){ - if( cp->tp_flag == prog[i] ){ - tpend=1;cp->tp_pos=i; - } - switch(prog[i]){ + if + (cp->m_specific_param.m_enc.m_tp_on == 1) + { + for + (i=0;i<4;++i) + { + switch + (prog[i]) + { + // component wise case 'C': - tpnum= tpnum * tcp->pocs[pino].compE; + tpnum *= l_current_poc->compE; break; + // resolution wise case 'R': - tpnum= tpnum * tcp->pocs[pino].resE; + tpnum *= l_current_poc->resE; break; + // precinct wise case 'P': - tpnum= tpnum * tcp->pocs[pino].prcE; + tpnum *= l_current_poc->prcE; break; + // layer wise case 'L': - tpnum= tpnum * tcp->pocs[pino].layE; + tpnum *= l_current_poc->layE; break; - } + } + // whould we split here ? + if + ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) + { + cp->m_specific_param.m_enc.m_tp_pos=i; + break; } } - }else{ + } + else + { tpnum=1; } return tpnum; } -/** mem allocation for TLM marker*/ -int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){ - int pino,tileno,totnum_tp=0; - j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int)); - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - int cur_totnum_tp = 0; - opj_tcp_t *tcp = &cp->tcps[tileno]; - for(pino = 0; pino <= tcp->numpocs; pino++) { - int tp_num=0; - opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS); - if(!pi) { return -1;} - tp_num = j2k_get_num_tp(cp,pino,tileno); - totnum_tp = totnum_tp + tp_num; - cur_totnum_tp = cur_totnum_tp + tp_num; - pi_destroy(pi, cp, tileno); +/** + * Calculates the total number of tile parts needed by the encoder to + * encode such an image. If not enough memory is available, then the function return false. + * + * @param p_nb_tiles pointer that will hold the number of tile parts. + * @param cp the coding parameters for the image. + * @param image the image to encode. + * @param p_j2k the p_j2k encoder. + * @param p_manager the user event manager. + * + * @return true if the function was successful, false else. + */ +bool j2k_calculate_tp( + opj_j2k_t *p_j2k, + opj_cp_t *cp, + OPJ_UINT32 * p_nb_tiles, + opj_image_t *image, + opj_event_mgr_t * p_manager) +{ + OPJ_UINT32 pino,tileno; + OPJ_UINT32 l_nb_tiles; + opj_tcp_t *tcp; + + // preconditions + assert(p_nb_tiles != 00); + assert(cp != 00); + assert(image != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_nb_tiles = cp->tw * cp->th; + * p_nb_tiles = 0; + tcp = cp->tcps; + + /* INDEX >> */ + if + (p_j2k->cstr_info) + { + opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile; + for + (tileno = 0; tileno < l_nb_tiles; ++tileno) + { + OPJ_UINT32 cur_totnum_tp = 0; + pi_update_encoding_parameters(image,cp,tileno); + for + (pino = 0; pino <= tcp->numpocs; ++pino) + { + OPJ_UINT32 tp_num = j2k_get_num_tp(cp,pino,tileno); + *p_nb_tiles = *p_nb_tiles + tp_num; + cur_totnum_tp += tp_num; + } + tcp->m_nb_tile_parts = cur_totnum_tp; + l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); + if + (l_info_tile_ptr->tp == 00) + { + return false; + } + memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t)); + l_info_tile_ptr->num_tps = cur_totnum_tp; + ++l_info_tile_ptr; + ++tcp; } - j2k->cur_totnum_tp[tileno] = cur_totnum_tp; - /* INDEX >> */ - if (j2k->cstr_info) { - j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp; - j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t)); - } - /* << INDEX */ } - return totnum_tp; + else + { + for + (tileno = 0; tileno < l_nb_tiles; ++tileno) + { + OPJ_UINT32 cur_totnum_tp = 0; + pi_update_encoding_parameters(image,cp,tileno); + for + (pino = 0; pino <= tcp->numpocs; ++pino) + { + OPJ_UINT32 tp_num=0; + tp_num = j2k_get_num_tp(cp,pino,tileno); + *p_nb_tiles = *p_nb_tiles + tp_num; + cur_totnum_tp += tp_num; + } + tcp->m_nb_tile_parts = cur_totnum_tp; + ++tcp; + } + } + return true; } -static void j2k_write_soc(opj_j2k_t *j2k) { - opj_cio_t *cio = j2k->cio; - cio_write(cio, J2K_MS_SOC, 2); +/** + * Writes the SOC marker (Start Of Codestream) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_soc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + /* 2 bytes will be written */ + OPJ_BYTE * l_start_stream = 00; + + // preconditions + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* write SOC identifier */ + opj_write_bytes(l_start_stream,J2K_MS_SOC,2); + if + (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) + { + return false; + } /* UniPG>> */ #ifdef USE_JPWL - /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2); - + j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2); #endif /* USE_JPWL */ + return true; /* <state = J2K_STATE_MHSIZ; +/** + * Reads a SOC marker (Start of Codestream) + * @param p_header_data the data contained in the SOC box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SOC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_soc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) + +{ + OPJ_BYTE l_data [2]; + OPJ_UINT32 l_marker; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + if + (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) + { + return false; + } + opj_read_bytes(l_data,&l_marker,2); + if + (l_marker != J2K_MS_SOC) + { + return false; + } + /* assure length of data is correct (0) */ + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_MHSIZ; /* Index */ - if (j2k->cstr_info) { - j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2; - j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start; + if + (p_j2k->cstr_info) + { + //TODO p_j2k->cstr_info->main_head_start = opj_stream_tell(p_stream) - 2; // why - 2 ? + p_j2k->cstr_info->codestream_size = 0;/*p_stream_numbytesleft(p_j2k->p_stream) + 2 - p_j2k->cstr_info->main_head_start*/; } + return true; } -static void j2k_write_siz(opj_j2k_t *j2k) { - int i; - int lenp, len; +/** + * Writes the SIZ marker (image and tile size) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_siz( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_size_len; + OPJ_BYTE * l_current_ptr; + opj_image_t * l_image = 00; + opj_cp_t *cp = 00; + opj_image_comp_t * l_img_comp = 00; - opj_cio_t *cio = j2k->cio; - opj_image_t *image = j2k->image; - opj_cp_t *cp = j2k->cp; + // preconditions + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_image; + cp = &(p_j2k->m_cp); + l_size_len = 40 + 3 * l_image->numcomps; + l_img_comp = l_image->comps; - cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */ - cio_write(cio, image->x1, 4); /* Xsiz */ - cio_write(cio, image->y1, 4); /* Ysiz */ - cio_write(cio, image->x0, 4); /* X0siz */ - cio_write(cio, image->y0, 4); /* Y0siz */ - cio_write(cio, cp->tdx, 4); /* XTsiz */ - cio_write(cio, cp->tdy, 4); /* YTsiz */ - cio_write(cio, cp->tx0, 4); /* XT0siz */ - cio_write(cio, cp->ty0, 4); /* YT0siz */ - cio_write(cio, image->numcomps, 2); /* Csiz */ - for (i = 0; i < image->numcomps; i++) { - cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */ - cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */ - cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */ + if + (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_size_len); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len; } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lsiz */ - cio_seek(cio, lenp + len); + + l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* write SOC identifier */ + opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */ + l_current_ptr+=2; + opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */ + l_current_ptr+=2; + opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */ + l_current_ptr+=2; + opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */ + l_current_ptr+=4; + opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */ + l_current_ptr+=2; + for + (i = 0; i < l_image->numcomps; ++i) + { + // TODO here with MCT ? + opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */ + ++l_current_ptr; + opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */ + ++l_current_ptr; + opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */ + ++l_current_ptr; + ++l_img_comp; + } + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) + { + return false; + } + return true; } -static void j2k_read_siz(opj_j2k_t *j2k) { - int len, i; +/** + * Reads a SIZ marker (image and tile size) + * @param p_header_data the data contained in the SIZ box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the SIZ marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_siz ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_size, i; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_comp_remain; + OPJ_UINT32 l_remaining_size; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_tmp; + opj_image_t *l_image = 00; + opj_cp_t *l_cp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcp_t * l_current_tile_param = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); - opj_cio_t *cio = j2k->cio; - opj_image_t *image = j2k->image; - opj_cp_t *cp = j2k->cp; - - len = cio_read(cio, 2); /* Lsiz */ - cio_read(cio, 2); /* Rsiz (capabilities) */ - image->x1 = cio_read(cio, 4); /* Xsiz */ - image->y1 = cio_read(cio, 4); /* Ysiz */ - image->x0 = cio_read(cio, 4); /* X0siz */ - image->y0 = cio_read(cio, 4); /* Y0siz */ - cp->tdx = cio_read(cio, 4); /* XTsiz */ - cp->tdy = cio_read(cio, 4); /* YTsiz */ - cp->tx0 = cio_read(cio, 4); /* XT0siz */ - cp->ty0 = cio_read(cio, 4); /* YT0siz */ - - if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) { - opj_event_msg(j2k->cinfo, EVT_ERROR, - "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n", - image->x0,image->x1,image->y0,image->y1); - return; + l_image = p_j2k->m_image; + l_cp = &(p_j2k->m_cp); + if + (p_header_size < 36) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return false; + } + l_remaining_size = p_header_size - 36; + + l_nb_comp = l_remaining_size / 3; + l_nb_comp_remain = l_remaining_size % 3; + if + (l_nb_comp_remain != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return false; + } + l_size = p_header_size + 2; /* Lsiz */ + + opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */ + p_header_data+=2; + l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp; + opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&l_image->x1) ,4); /* Xsiz */ + p_header_data+=4; + opj_read_bytes(p_header_data,(OPJ_UINT32*) (&l_image->y1),4); /* Ysiz */ + p_header_data+=4; + opj_read_bytes(p_header_data,(OPJ_UINT32*) &l_image->x0,4); /* X0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data,(OPJ_UINT32*) &l_image->y0,4); /* Y0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data, (&l_cp->tdx),4); /* XTsiz */ + p_header_data+=4; + opj_read_bytes(p_header_data,&l_cp->tdy,4); /* YTsiz */ + p_header_data+=4; + opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&l_cp->tx0),4); /* XT0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&l_cp->ty0),4); /* YT0siz */ + p_header_data+=4; + opj_read_bytes(p_header_data,(&l_image->numcomps),2); /* Csiz */ + p_header_data+=2; + if + (l_image->numcomps != l_nb_comp) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n"); + return false; } - - image->numcomps = cio_read(cio, 2); /* Csiz */ #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { /* if JPWL is on, we check whether TX errors have damaged too much the SIZ parameters */ if (!(image->x1 * image->y1)) { - opj_event_msg(j2k->cinfo, EVT_ERROR, + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: bad image size (%d x %d)\n", image->x1, image->y1); if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } } if (image->numcomps != ((len - 38) / 3)) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n", image->numcomps, ((len - 38) / 3)); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"); + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"); if (image->numcomps < ((len - 38) / 3)) { len = 38 + 3 * image->numcomps; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n", len); } else { image->numcomps = ((len - 38) / 3); - opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n", image->numcomps); } } @@ -514,498 +2143,1579 @@ static void j2k_read_siz(opj_j2k_t *j2k) { cp->exp_comps = image->numcomps; } #endif /* USE_JPWL */ - - image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t)); - for (i = 0; i < image->numcomps; i++) { - int tmp, w, h; - tmp = cio_read(cio, 1); /* Ssiz_i */ - image->comps[i].prec = (tmp & 0x7f) + 1; - image->comps[i].sgnd = tmp >> 7; - image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */ - image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */ - + + l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t)); + if + (l_image->comps == 00) + { + l_image->numcomps = 0; + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; + } + memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t)); + l_img_comp = l_image->comps; + for + (i = 0; i < l_image->numcomps; ++i) + { + OPJ_UINT32 tmp; + opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */ + ++p_header_data; + l_img_comp->prec = (tmp & 0x7f) + 1; + l_img_comp->sgnd = tmp >> 7; + opj_read_bytes(p_header_data,&l_img_comp->dx,1); /* XRsiz_i */ + ++p_header_data; + opj_read_bytes(p_header_data,&l_img_comp->dy,1); /* YRsiz_i */ + ++p_header_data; #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { /* if JPWL is on, we check whether TX errors have damaged too much the SIZ parameters, again */ if (!(image->comps[i].dx * image->comps[i].dy)) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n", i, i, image->comps[i].dx, image->comps[i].dy); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"); + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"); if (!image->comps[i].dx) { image->comps[i].dx = 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n", i, image->comps[i].dx); } if (!image->comps[i].dy) { image->comps[i].dy = 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n", i, image->comps[i].dy); } } } #endif /* USE_JPWL */ - - /* TODO: unused ? */ - w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx); - h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy); - - image->comps[i].resno_decoded = 0; /* number of resolution decoded */ - image->comps[i].factor = cp->reduce; /* reducing factor per component */ + l_img_comp->resno_decoded = 0; /* number of resolution decoded */ + l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ + ++l_img_comp; } - cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx); - cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy); + l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx); + l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy); + l_nb_tiles = l_cp->tw * l_cp->th; + if + (p_j2k->m_specific_param.m_decoder.m_discard_tiles) + { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy); + } + else + { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; + p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th; + } #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { /* if JPWL is on, we check whether TX errors have damaged too much the SIZ parameters */ if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: bad number of tiles (%d x %d)\n", cp->tw, cp->th); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"); + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"); if (cp->tw < 1) { cp->tw= 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n", cp->tw); } if (cp->tw > cp->max_tiles) { cp->tw= 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n" + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n" "- setting %d tiles in x => HYPOTHESIS!!!\n", cp->max_tiles, cp->tw); } if (cp->th < 1) { cp->th= 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n", cp->th); } if (cp->th > cp->max_tiles) { cp->th= 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n", + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n", "- setting %d tiles in y => HYPOTHESIS!!!\n", cp->max_tiles, cp->th); } } } #endif /* USE_JPWL */ - - cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); - cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int)); - cp->tileno_size = 0; + /* memory allocations */ + l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t)); + if + (l_cp->tcps == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; + } + memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t)); #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { if (!cp->tcps) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: could not alloc tcps field of cp\n"); if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } } } #endif /* USE_JPWL */ - for (i = 0; i < cp->tw * cp->th; i++) { - cp->tcps[i].POC = 0; - cp->tcps[i].numpocs = 0; - cp->tcps[i].first = 1; + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t)); + if + (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; } + memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t)); - /* Initialization for PPM marker */ - cp->ppm = 0; - cp->ppm_data = NULL; - cp->ppm_data_first = NULL; - cp->ppm_previous = 0; - cp->ppm_store = 0; + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records = opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t)); + if + (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; + } + memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t)); + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS; + + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records = opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t)); + if + (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; + } + memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t)); + p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS; + + /* set up default dc level shift */ + for + (i=0;inumcomps;++i) + { + if + (! l_image->comps[i].sgnd) + { + p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1); + } + } + + l_current_tile_param = l_cp->tcps; + for + (i = 0; i < l_nb_tiles; ++i) + { + l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t)); + if + (l_current_tile_param->tccps == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; + } + memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t)); + + ++l_current_tile_param; - j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); - for (i = 0; i < cp->tw * cp->th; i++) { - cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t)); } - j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*)); - j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int)); - j2k->state = J2K_STATE_MH; + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_MH; + opj_image_comp_update(l_image,l_cp); /* Index */ - if (j2k->cstr_info) { - opj_codestream_info_t *cstr_info = j2k->cstr_info; - cstr_info->image_w = image->x1 - image->x0; - cstr_info->image_h = image->y1 - image->y0; - cstr_info->numcomps = image->numcomps; - cstr_info->tw = cp->tw; - cstr_info->th = cp->th; - cstr_info->tile_x = cp->tdx; - cstr_info->tile_y = cp->tdy; - cstr_info->tile_Ox = cp->tx0; - cstr_info->tile_Oy = cp->ty0; - cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t)); - } -} - -static void j2k_write_com(opj_j2k_t *j2k) { - unsigned int i; - int lenp, len; - - if(j2k->cp->comment) { - opj_cio_t *cio = j2k->cio; - char *comment = j2k->cp->comment; - - cio_write(cio, J2K_MS_COM, 2); - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */ - for (i = 0; i < strlen(comment); i++) { - cio_write(cio, comment[i], 1); + if + (p_j2k->cstr_info) + { + opj_codestream_info_t *cstr_info = p_j2k->cstr_info; + cstr_info->image_w = l_image->x1 - l_image->x0; + cstr_info->image_h = l_image->y1 - l_image->y0; + cstr_info->numcomps = l_image->numcomps; + cstr_info->tw = l_cp->tw; + cstr_info->th = l_cp->th; + cstr_info->tile_x = l_cp->tdx; + cstr_info->tile_y = l_cp->tdy; + cstr_info->tile_Ox = l_cp->tx0; + cstr_info->tile_Oy = l_cp->ty0; + cstr_info->tile = (opj_tile_info_t*) opj_calloc(l_nb_tiles, sizeof(opj_tile_info_t)); + if + (cstr_info->tile == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n"); + return false; } - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); - cio_seek(cio, lenp + len); + memset(cstr_info->tile,0,l_nb_tiles * sizeof(opj_tile_info_t)); } + return true; } -static void j2k_read_com(opj_j2k_t *j2k) { - int len; - - opj_cio_t *cio = j2k->cio; +/** + * Writes the COM marker (comment) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_com( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_comment_size; + OPJ_UINT32 l_total_com_size; + const OPJ_CHAR *l_comment; + OPJ_BYTE * l_current_ptr = 00; - len = cio_read(cio, 2); - cio_skip(cio, len - 2); -} - -static void j2k_write_cox(opj_j2k_t *j2k, int compno) { - int i; - - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j2k->cio; + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); - cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */ - cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */ - cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */ - cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */ - cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */ - - if (tccp->csty & J2K_CCP_CSTY_PRT) { - for (i = 0; i < tccp->numresolutions; i++) { - cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */ + l_comment = p_j2k->m_cp.comment; + l_comment_size = strlen(l_comment); + l_total_com_size = l_comment_size + 6; + + if + (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_total_com_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size; + } + l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */ + l_current_ptr+=2; + opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */ + l_current_ptr+=2; + opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */ + l_current_ptr+=2, + memcpy( l_current_ptr,l_comment,l_comment_size); + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) + { + return false; + } + return true; +} + +/** + * Reads a COM marker (comments) + * @param p_header_data the data contained in the COM box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_com ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + return true; +} + +/** + * Gets the size taken by writting a SPCod or SPCoc for the given tile and component. + * + * @param p_tile_no the tile indix. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. + * + * @return the number of bytes taken by the SPCod element. + */ +OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + // preconditions + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + // preconditions again + assert(p_tile_no < (l_cp->tw * l_cp->th)); + assert(p_comp_no < p_j2k->m_image->numcomps); + + if + (l_tccp->csty & J2K_CCP_CSTY_PRT) + { + return 5 + l_tccp->numresolutions; + } + else + { + return 5; } } -static void j2k_read_cox(opj_j2k_t *j2k, int compno) { - int i; - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j2k->cio; +/** + * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. + * + * @param p_comp_no the component number to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + * +*/ +bool j2k_write_SPCod_SPCoc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; - tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */ + // preconditions + assert(p_j2k != 00); + assert(p_header_size != 00); + assert(p_manager != 00); + assert(p_data != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + // preconditions again + assert(p_tile_no < (l_cp->tw * l_cp->th)); + assert(p_comp_no <(p_j2k->m_image->numcomps)); + + if + (*p_header_size < 5) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n"); + return false; + } + + opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */ + ++p_data; + opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */ + ++p_data; + opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */ + ++p_data; + opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */ + ++p_data; + opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */ + ++p_data; + + *p_header_size = *p_header_size - 5; + if + (l_tccp->csty & J2K_CCP_CSTY_PRT) + { + if + (*p_header_size < l_tccp->numresolutions) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n"); + return false; + } + for + (i = 0; i < l_tccp->numresolutions; ++i) + { + opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */ + ++p_data; + } + *p_header_size = *p_header_size - l_tccp->numresolutions; + + } + return true; +} + + +/** + * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile. + * @param p_header_data the data contained in the COM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_SPCod_SPCoc( + opj_j2k_t *p_j2k, + OPJ_UINT32 compno, + OPJ_BYTE * p_header_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ) +{ + // loop + OPJ_UINT32 i; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_BYTE * l_current_ptr = 00; + OPJ_UINT32 l_tmp; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); + + + l_cp = &(p_j2k->m_cp); + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + + // precondition again + assert(compno < p_j2k->m_image->numcomps); + l_tccp = &l_tcp->tccps[compno]; + l_current_ptr = p_header_data; + + + // make sure room is sufficient + if + (* p_header_size < 5) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + return false; + } + opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */ + ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */ + ++l_current_ptr; // If user wants to remove more resolutions than the codestream contains, return error - if (cp->reduce >= tccp->numresolutions) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number " + if + (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) + { + opj_event_msg(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number " "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno); - j2k->state |= J2K_STATE_ERR; + p_j2k->m_specific_param.m_decoder.m_state |= J2K_DEC_STATE_ERR; + return false; } + + opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */ + ++l_current_ptr; + l_tccp->cblkw += 2; - tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */ - tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */ - tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */ - tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */ - if (tccp->csty & J2K_CP_CSTY_PRT) { - for (i = 0; i < tccp->numresolutions; i++) { - int tmp = cio_read(cio, 1); /* SPcox (I_i) */ - tccp->prcw[i] = tmp & 0xf; - tccp->prch[i] = tmp >> 4; + opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */ + ++l_current_ptr; + l_tccp->cblkh += 2; + + opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */ + ++l_current_ptr; + + opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */ + ++l_current_ptr; + + * p_header_size = * p_header_size - 5; + + // use custom precinct size ? + if + (l_tccp->csty & J2K_CCP_CSTY_PRT) + { + if + (* p_header_size < l_tccp->numresolutions) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n"); + return false; + } + for + (i = 0; i < l_tccp->numresolutions; ++i) + { + opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */ + ++l_current_ptr; + l_tccp->prcw[i] = l_tmp & 0xf; + l_tccp->prch[i] = l_tmp >> 4; + } + * p_header_size = * p_header_size - l_tccp->numresolutions; + } + else + { + /* set default size for the precinct width and height */ + for + (i = 0; i < l_tccp->numresolutions; ++i) + { + l_tccp->prcw[i] = 15; + l_tccp->prch[i] = 15; } } /* INDEX >> */ - if(j2k->cstr_info && compno == 0) { - for (i = 0; i < tccp->numresolutions; i++) { - if (tccp->csty & J2K_CP_CSTY_PRT) { - j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i]; - j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i]; - } - else { - j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15; - j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15; - } - } + if + (p_j2k->cstr_info && compno == 0) + { + OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32); + memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size); + memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size); } /* << INDEX */ + return true; } -static void j2k_write_cod(opj_j2k_t *j2k) { - opj_cp_t *cp = NULL; - opj_tcp_t *tcp = NULL; - int lenp, len; +/** + * Copies the tile component parameters of all the component from the first tile component. + * + * @param p_j2k the J2k codec. + */ +void j2k_copy_tile_component_parameters( + opj_j2k_t *p_j2k + ) +{ + // loop + OPJ_UINT32 i; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_ref_tccp = 00; + opj_tccp_t *l_copied_tccp = 00; + OPJ_UINT32 l_prc_size; + // preconditions + assert(p_j2k != 00); - opj_cio_t *cio = j2k->cio; + l_cp = &(p_j2k->m_cp); + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; - cio_write(cio, J2K_MS_COD, 2); /* COD */ - - lenp = cio_tell(cio); - cio_skip(cio, 2); - - cp = j2k->cp; - tcp = &cp->tcps[j2k->curtileno]; + l_ref_tccp = &l_tcp->tccps[0]; + l_copied_tccp = l_ref_tccp + 1; + l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32); - cio_write(cio, tcp->csty, 1); /* Scod */ - cio_write(cio, tcp->prg, 1); /* SGcod (A) */ - cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */ - cio_write(cio, tcp->mct, 1); /* SGcod (C) */ - - j2k_write_cox(j2k, 0); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lcod */ - cio_seek(cio, lenp + len); -} - -static void j2k_read_cod(opj_j2k_t *j2k) { - int len, i, pos; - - opj_cio_t *cio = j2k->cio; - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; - opj_image_t *image = j2k->image; - - len = cio_read(cio, 2); /* Lcod */ - tcp->csty = cio_read(cio, 1); /* Scod */ - tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */ - tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */ - tcp->mct = cio_read(cio, 1); /* SGcod (C) */ - - pos = cio_tell(cio); - for (i = 0; i < image->numcomps; i++) { - tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT; - cio_seek(cio, pos); - j2k_read_cox(j2k, i); + for + (i=1;im_image->numcomps;++i) + { + l_copied_tccp->numresolutions = l_ref_tccp->numresolutions; + l_copied_tccp->cblkw = l_ref_tccp->cblkw; + l_copied_tccp->cblkh = l_ref_tccp->cblkh; + l_copied_tccp->cblksty = l_ref_tccp->cblksty; + l_copied_tccp->qmfbid = l_ref_tccp->qmfbid; + memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size); + memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size); + ++l_copied_tccp; } +} + + + +/** + * Writes the COD marker (Coding style default) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_cod( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_code_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0); + l_remaining_size = l_code_size; + + if + (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_code_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */ + l_current_data += 2; + + opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */ + l_current_data += 2; + + opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */ + ++l_current_data; + + opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */ + ++l_current_data; + + opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */ + l_current_data+=2; + + opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */ + ++l_current_data; + + l_remaining_size -= 9; + + if + (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting COD marker\n"); + return false; + } + if + (l_remaining_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting COD marker\n"); + return false; + } + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) + { + return false; + } + return true; +} + +/** + * Reads a COD marker (Coding Styke defaults) + * @param p_header_data the data contained in the COD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COD marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_cod ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + // loop + OPJ_UINT32 i; + OPJ_UINT32 l_tmp; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_image_t *l_image = 00; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH) ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_image = p_j2k->m_image; + + // make sure room is sufficient + if + (p_header_size < 5) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return false; + } + + opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */ + ++p_header_data; + opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */ + ++p_header_data; + l_tcp->prg = (OPJ_PROG_ORDER) l_tmp; + opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */ + p_header_data+=2; + if + (l_cp->m_specific_param.m_dec.m_layer) + { + l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer; + } + else + { + l_tcp->num_layers_to_decode = l_tcp->numlayers; + } + + opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */ + ++p_header_data; + + p_header_size -= 5; + for + (i = 0; i < l_image->numcomps; ++i) + { + l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT; + } + + if + (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return false; + } + if + (p_header_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n"); + return false; + } + j2k_copy_tile_component_parameters(p_j2k); + /* Index */ - if (j2k->cstr_info) { - opj_codestream_info_t *cstr_info = j2k->cstr_info; - cstr_info->prog = tcp->prg; - cstr_info->numlayers = tcp->numlayers; - cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int)); - for (i = 0; i < image->numcomps; i++) { - cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1; + if + (p_j2k->cstr_info) + { + opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info; + l_cstr_info->prog = l_tcp->prg; + l_cstr_info->numlayers = l_tcp->numlayers; + l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32)); + for + (i = 0; i < l_image->numcomps; ++i) + { + l_cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1; } } + return true; +} + +/** + * Writes the COC marker (Coding style component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_coc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_coc_size,l_remaining_size; + OPJ_UINT32 l_comp_room; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_comp_room = (p_j2k->m_image->numcomps <= 256) ? 1 : 2; + + l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + if + (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_coc_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size; + } + + j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) + { + return false; + } + return true; +} + +/** + * Gets the maximum size taken by a coc. + * + * @param p_j2k the jpeg2000 codec to use. + */ +OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 i,j; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max = 0; + + // preconditions + + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; + l_nb_comp = p_j2k->m_image->numcomps; + + for + (i=0;im_cp.tcps; + l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ; + + for + (i=0;im_nb_tile_parts); + ++l_tcp; + } + return 12 * l_max; +} + + +/** + * Gets the maximum size taken by the headers of the SOT. + * + * @param p_j2k the jpeg2000 codec to use. + */ +OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_t *p_j2k) +{ + OPJ_UINT32 l_nb_bytes = 0; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_coc_bytes,l_qcc_bytes; + + + l_nb_comps = p_j2k->m_image->numcomps - 1; + l_nb_bytes += j2k_get_max_toc_size(p_j2k); + if + (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) + { + l_coc_bytes = j2k_get_max_coc_size(p_j2k); + l_nb_bytes += l_nb_comps * l_coc_bytes; + l_qcc_bytes = j2k_get_max_qcc_size(p_j2k); + l_nb_bytes += l_nb_comps * l_qcc_bytes; + } + l_nb_bytes += j2k_get_max_poc_size(p_j2k); + /*** DEVELOPER CORNER, Add room for your headers ***/ + + + return l_nb_bytes; +} + + +/** + * Writes the COC marker (Coding style component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +void j2k_write_coc_in_memory( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + struct opj_event_mgr * p_manager + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_coc_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + opj_image_t *l_image = 00; + OPJ_UINT32 l_comp_room; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_image = p_j2k->m_image; + l_comp_room = (l_image->numcomps <= 256) ? 1 : 2; + + l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + l_remaining_size = l_coc_size; + + l_current_data = p_data; + + opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */ + l_current_data += 2; + opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */ + l_current_data+=l_comp_room; + opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */ + ++l_current_data; + l_remaining_size -= (5 + l_comp_room); + j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager); + * p_data_written = l_coc_size; +} + + +/** + * Reads a COC marker (Coding Style Component) + * @param p_header_data the data contained in the COC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_coc ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_image_t *l_image = 00; + OPJ_UINT32 l_comp_room; + OPJ_UINT32 l_comp_no; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH) ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_image = p_j2k->m_image; + + l_comp_room = l_image->numcomps <= 256 ? 1 : 2; + // make sure room is sufficient + if + (p_header_size < l_comp_room + 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return false; + } + p_header_size -= l_comp_room + 1; + + opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */ + p_header_data += l_comp_room; + if + (l_comp_no >= l_image->numcomps) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n"); + return false; + } + opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */ + ++p_header_data ; + + if + (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return false; + } + if + (p_header_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n"); + return false; + } + return true; +} + +/** + * Gets the size taken by writting SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC. + * + * @param p_tile_no the tile indix. + * @param p_comp_no the component being outputted. + * @param p_j2k the J2K codec. + * + * @return the number of bytes taken by the SPCod element. + */ +OPJ_UINT32 j2k_get_SQcd_SQcc_size ( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no + ) +{ + OPJ_UINT32 l_num_bands; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + + // preconditions + assert(p_j2k != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + // preconditions again + assert(p_tile_no < l_cp->tw * l_cp->th); + assert(p_comp_no < p_j2k->m_image->numcomps); + + l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); + + if + (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) + { + return 1 + l_num_bands; + } + else + { + return 1 + 2*l_num_bands; + } } -static void j2k_write_coc(opj_j2k_t *j2k, int compno) { - int lenp, len; +/** + * Writes a SQcd or SQcc element, i.e. the quantization values of a band. + * + * @param p_tile_no the tile to output. + * @param p_comp_no the component number to output. + * @param p_data the data buffer. + * @param p_header_size pointer to the size of the data buffer, it is changed by the function. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. + * +*/ +bool j2k_write_SQcd_SQcc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_header_size; + OPJ_UINT32 l_band_no, l_num_bands; + OPJ_UINT32 l_expn,l_mant; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; - opj_image_t *image = j2k->image; - opj_cio_t *cio = j2k->cio; - - cio_write(cio, J2K_MS_COC, 2); /* COC */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */ - cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */ - j2k_write_cox(j2k, compno); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lcoc */ - cio_seek(cio, lenp + len); -} + // preconditions + assert(p_j2k != 00); + assert(p_header_size != 00); + assert(p_manager != 00); + assert(p_data != 00); -static void j2k_read_coc(opj_j2k_t *j2k) { - int len, compno; - - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; - opj_image_t *image = j2k->image; - opj_cio_t *cio = j2k->cio; + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; - len = cio_read(cio, 2); /* Lcoc */ - compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */ - tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */ - j2k_read_cox(j2k, compno); -} - -static void j2k_write_qcx(opj_j2k_t *j2k, int compno) { - int bandno, numbands; - int expn, mant; + // preconditions again + assert(p_tile_no < l_cp->tw * l_cp->th); + assert(p_comp_no m_image->numcomps); - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j2k->cio; + l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2); - cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */ - numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2; - - for (bandno = 0; bandno < numbands; bandno++) { - expn = tccp->stepsizes[bandno].expn; - mant = tccp->stepsizes[bandno].mant; - - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { - cio_write(cio, expn << 3, 1); /* SPqcx_i */ - } else { - cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */ + if + (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) + { + l_header_size = 1 + l_num_bands; + if + (*p_header_size < l_header_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting SQcd SQcc element\n"); + return false; + } + opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ + ++p_data; + for + (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) + { + l_expn = l_tccp->stepsizes[l_band_no].expn; + opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */ + ++p_data; } } + else + { + l_header_size = 1 + 2*l_num_bands; + if + (*p_header_size < l_header_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting SQcd SQcc element\n"); + return false; + } + opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */ + ++p_data; + for + (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) + { + l_expn = l_tccp->stepsizes[l_band_no].expn; + l_mant = l_tccp->stepsizes[l_band_no].mant; + opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */ + p_data += 2; + } + } + *p_header_size = *p_header_size - l_header_size; + return true; } -static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) { - int tmp; - int bandno, numbands; +/** + * Reads a SQcd or SQcc element, i.e. the quantization values of a band. + * + * @param p_comp_no the component being targeted. + * @param p_header_data the data contained in the COM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the COM marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_SQcd_SQcc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE* p_header_data, + OPJ_UINT32 * p_header_size, + struct opj_event_mgr * p_manager + ) +{ + // loop + OPJ_UINT32 l_band_no; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_BYTE * l_current_ptr = 00; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_num_band; - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_cio_t *cio = j2k->cio; + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_header_data != 00); - tmp = cio_read(cio, 1); /* Sqcx */ - tccp->qntsty = tmp & 0x1f; - tccp->numgbits = tmp >> 5; - numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? - 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2); + l_cp = &(p_j2k->m_cp); + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + // precondition again + assert(p_comp_no < p_j2k->m_image->numcomps); + l_tccp = &l_tcp->tccps[p_comp_no]; + l_current_ptr = p_header_data; + + if + (* p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n"); + return false; + } + * p_header_size -= 1; + + opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */ + ++l_current_ptr; + + l_tccp->qntsty = l_tmp & 0x1f; + l_tccp->numgbits = l_tmp >> 5; + if + (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) + { + l_num_band = 1; + } + else + { + l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? (*p_header_size) : (*p_header_size) / 2; + } #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { /* if JPWL is on, we check whether there are too many subbands */ if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) { - opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, + opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR, "JPWL: bad number of subbands in Sqcx (%d)\n", numbands); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ numbands = 1; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n" + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n" "- setting number of bands to %d => HYPOTHESIS!!!\n", numbands); }; }; #endif /* USE_JPWL */ - - for (bandno = 0; bandno < numbands; bandno++) { - int expn, mant; - if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { - expn = cio_read(cio, 1) >> 3; /* SPqcx_i */ - mant = 0; - } else { - tmp = cio_read(cio, 2); /* SPqcx_i */ - expn = tmp >> 11; - mant = tmp & 0x7ff; + if + (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) + { + for + (l_band_no = 0; l_band_no < l_num_band; l_band_no++) + { + opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */ + ++l_current_ptr; + l_tccp->stepsizes[l_band_no].expn = l_tmp>>3; + l_tccp->stepsizes[l_band_no].mant = 0; + } + * p_header_size = * p_header_size - l_num_band; + } + else + { + for + (l_band_no = 0; l_band_no < l_num_band; l_band_no++) + { + opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */ + l_current_ptr+=2; + l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11; + l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff; } - tccp->stepsizes[bandno].expn = expn; - tccp->stepsizes[bandno].mant = mant; + * p_header_size = * p_header_size - 2*l_num_band; } /* Add Antonin : if scalar_derived -> compute other stepsizes */ - if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) { - for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) { - tccp->stepsizes[bandno].expn = - ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? - (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0; - tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant; + if + (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) + { + for + (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) + { + l_tccp->stepsizes[l_band_no].expn = + ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ? + (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0; + l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant; } + } - /* ddA */ + return true; } -static void j2k_write_qcd(opj_j2k_t *j2k) { - int lenp, len; - opj_cio_t *cio = j2k->cio; + +/** + * Copies the tile component parameters of all the component from the first tile component. + * + * @param p_j2k the J2k codec. + */ +void j2k_copy_tile_quantization_parameters( + opj_j2k_t *p_j2k + ) +{ + // loop + OPJ_UINT32 i; - cio_write(cio, J2K_MS_QCD, 2); /* QCD */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - j2k_write_qcx(j2k, 0); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lqcd */ - cio_seek(cio, lenp + len); -} + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_ref_tccp = 00; + opj_tccp_t *l_copied_tccp = 00; + OPJ_UINT32 l_size; + // preconditions + assert(p_j2k != 00); -static void j2k_read_qcd(opj_j2k_t *j2k) { - int len, i, pos; - - opj_cio_t *cio = j2k->cio; - opj_image_t *image = j2k->image; + l_cp = &(p_j2k->m_cp); + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + // precondition again + l_ref_tccp = &l_tcp->tccps[0]; + l_copied_tccp = l_ref_tccp + 1; + l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t); - len = cio_read(cio, 2); /* Lqcd */ - pos = cio_tell(cio); - for (i = 0; i < image->numcomps; i++) { - cio_seek(cio, pos); - j2k_read_qcx(j2k, i, len - 2); + for + (i=1;im_image->numcomps;++i) + { + l_copied_tccp->qntsty = l_ref_tccp->qntsty; + l_copied_tccp->numgbits = l_ref_tccp->numgbits; + memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size); + ++l_copied_tccp; } } -static void j2k_write_qcc(opj_j2k_t *j2k, int compno) { - int lenp, len; - opj_cio_t *cio = j2k->cio; + +/** + * Writes the QCD marker (quantization default) + * + * @param p_comp_number the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_qcd( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_qcd_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0); + l_remaining_size = l_qcd_size; - cio_write(cio, J2K_MS_QCC, 2); /* QCC */ - lenp = cio_tell(cio); - cio_skip(cio, 2); - cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */ - j2k_write_qcx(j2k, compno); - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lqcc */ - cio_seek(cio, lenp + len); + if + (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_qcd_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size; + } + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */ + l_current_data += 2; + + opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */ + l_current_data += 2; + + l_remaining_size -= 4; + + if + (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting QCD marker\n"); + return false; + } + if + (l_remaining_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error writting QCD marker\n"); + return false; + } + + if + (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) + { + return false; + } + return true; } -static void j2k_read_qcc(opj_j2k_t *j2k) { - int len, compno; - int numcomp = j2k->image->numcomps; - opj_cio_t *cio = j2k->cio; +/** + * Reads a QCD marker (Quantization defaults) + * @param p_header_data the data contained in the QCD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCD marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_qcd ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); - len = cio_read(cio, 2); /* Lqcc */ - compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */ + if + (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + return false; + } + if + (p_header_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n"); + return false; + } + j2k_copy_tile_quantization_parameters(p_j2k); + return true; +} + + +/** + * Writes the QCC marker (quantization component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_qcc( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_qcc_size,l_remaining_size; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + l_remaining_size = l_qcc_size; + if + (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_qcc_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size; + } + j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager); + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) + { + return false; + } + return true; +} + + +/** + * Writes the QCC marker (quantization component) + * + * @param p_comp_no the index of the component to output. + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +void j2k_write_qcc_in_memory( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_comp_no, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_qcc_size,l_remaining_size; + OPJ_BYTE * l_current_data = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + + l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no); + l_remaining_size = l_qcc_size; + + l_current_data = p_data; + + opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */ + l_current_data += 2; + + if + (p_j2k->m_image->numcomps <= 256) + { + --l_qcc_size; + opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ + l_current_data += 2; + opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */ + ++l_current_data; + // in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available + l_remaining_size -= 6; + } + else + { + opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */ + l_current_data += 2; + opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */ + l_current_data+=2; + l_remaining_size -= 6; + } + j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager); + * p_data_written = l_qcc_size; +} + +/** + * Gets the maximum size taken by a qcc. + */ +OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_t *p_j2k) +{ + return j2k_get_max_coc_size(p_j2k); +} + +/** + * Reads a QCC marker (Quantization component) + * @param p_header_data the data contained in the QCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the QCC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_qcc( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_num_comp,l_comp_no; + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_num_comp = p_j2k->m_image->numcomps; #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { - static int backup_compno = 0; + static OPJ_UINT32 backup_compno = 0; /* compno is negative or larger than the number of components!!! */ if ((compno < 0) || (compno >= numcomp)) { - opj_event_msg(j2k->cinfo, EVT_ERROR, + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: bad component number in QCC (%d out of a maximum of %d)\n", compno, numcomp); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ compno = backup_compno % numcomp; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" "- setting component number to %d\n", compno); } @@ -1014,292 +3724,2175 @@ static void j2k_read_qcc(opj_j2k_t *j2k) { backup_compno++; }; #endif /* USE_JPWL */ - - j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2)); -} - -static void j2k_write_poc(opj_j2k_t *j2k) { - int len, numpchgs, i; - - int numcomps = j2k->image->numcomps; - - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = &cp->tcps[j2k->curtileno]; - opj_tccp_t *tccp = &tcp->tccps[0]; - opj_cio_t *cio = j2k->cio; - - numpchgs = 1 + tcp->numpocs; - cio_write(cio, J2K_MS_POC, 2); /* POC */ - len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs; - cio_write(cio, len, 2); /* Lpoc */ - for (i = 0; i < numpchgs; i++) { - opj_poc_t *poc = &tcp->pocs[i]; - cio_write(cio, poc->resno0, 1); /* RSpoc_i */ - cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */ - cio_write(cio, poc->layno1, 2); /* LYEpoc_i */ - poc->layno1 = int_min(poc->layno1, tcp->numlayers); - cio_write(cio, poc->resno1, 1); /* REpoc_i */ - poc->resno1 = int_min(poc->resno1, tccp->numresolutions); - cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */ - poc->compno1 = int_min(poc->compno1, numcomps); - cio_write(cio, poc->prg, 1); /* Ppoc_i */ + if + (l_num_comp <= 256) + { + if + (p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return false; + } + opj_read_bytes(p_header_data,&l_comp_no,1); + ++p_header_data; + --p_header_size; } + else + { + if + (p_header_size < 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return false; + } + opj_read_bytes(p_header_data,&l_comp_no,2); + p_header_data+=2; + p_header_size-=2; + } + if + (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return false; + } + if + (p_header_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n"); + return false; + } + return true; + } -static void j2k_read_poc(opj_j2k_t *j2k) { - int len, numpchgs, i, old_poc; - int numcomps = j2k->image->numcomps; +/** + * Writes the CBD marker (Component bit depth definition) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_cbd( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_cbd_size; + OPJ_BYTE * l_current_data = 00; + opj_image_t *l_image = 00; + opj_image_comp_t * l_comp = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_image = p_j2k->m_image; + l_cbd_size = 6 + p_j2k->m_image->numcomps; - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; - opj_cio_t *cio = j2k->cio; + if + (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_cbd_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size; + } + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */ + l_current_data+=2; + l_comp = l_image->comps; + for + (i=0;inumcomps;++i) + { + opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */ + ++l_current_data; + ++l_comp; + } + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) + { + return false; + } + return true; +} + +/** + * Reads a CBD marker (Component bit depth definition) + * @param p_header_data the data contained in the CBD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the CBD marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_cbd ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_nb_comp,l_num_comp; + OPJ_UINT32 l_comp_def; + OPJ_UINT32 i; + opj_image_comp_t * l_comp = 00; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_num_comp = p_j2k->m_image->numcomps; - old_poc = tcp->POC ? tcp->numpocs + 1 : 0; - tcp->POC = 1; - len = cio_read(cio, 2); /* Lpoc */ - numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2)); + if + (p_header_size != (p_j2k->m_image->numcomps + 2)) + { + opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + return false; + } + opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */ + p_header_data+=2; + if + (l_nb_comp != l_num_comp) + { + opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n"); + return false; + } + + l_comp = p_j2k->m_image->comps; + for + (i=0;isgnd = (l_comp_def>>7) & 1; + l_comp->prec = (l_comp_def&0x7f) + 1; + ++l_comp; + } + return true; +} + +/** + * Writes the MCC marker (Multiple Component Collection) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_mcc_record( + opj_j2k_t *p_j2k, + struct opj_simple_mcc_decorrelation_data * p_mcc_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_mcc_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_bytes_for_comp; + OPJ_UINT32 l_mask; + OPJ_UINT32 l_tmcc; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); - for (i = old_poc; i < numpchgs + old_poc; i++) { - opj_poc_t *poc; - poc = &tcp->pocs[i]; - poc->resno0 = cio_read(cio, 1); /* RSpoc_i */ - poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */ - poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */ - poc->resno1 = cio_read(cio, 1); /* REpoc_i */ - poc->compno1 = int_min( - cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */ - poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */ + if + (p_mcc_record->m_nb_comps > 255 ) + { + l_nb_bytes_for_comp = 2; + l_mask = 0x8000; + } + else + { + l_nb_bytes_for_comp = 1; + l_mask = 0; + } + + l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19; + if + (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_mcc_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size; + } + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */ + l_current_data += 2; + + /* first marker */ + opj_write_bytes(l_current_data,0,2); /* Zmcc */ + l_current_data += 2; + opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */ + ++l_current_data; + /* only one marker atm */ + opj_write_bytes(l_current_data,0,2); /* Ymcc */ + l_current_data+=2; + opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */ + l_current_data+=2; + opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */ + ++l_current_data; + + opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */ + l_current_data+=2; + + for + (i=0;im_nb_comps;++i) + { + opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/ + l_current_data+=l_nb_bytes_for_comp; + } + + opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */ + l_current_data+=2; + for + (i=0;im_nb_comps;++i) + { + opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/ + l_current_data+=l_nb_bytes_for_comp; + } + l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16; + if + (p_mcc_record->m_decorrelation_array) + { + l_tmcc |= p_mcc_record->m_decorrelation_array->m_index; + } + if + (p_mcc_record->m_offset_array) + { + l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8); + } + opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */ + l_current_data+=3; + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) + { + return false; + } + return true; +} + + +/** + * Reads a MCC marker (Multiple Component Collection) + * + * @param p_header_data the data contained in the MCC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_mcc ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i,j; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_indix; + opj_tcp_t * l_tcp; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_mct_data; + OPJ_UINT32 l_nb_collections; + OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_nb_bytes_by_comp; + + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if + (p_header_size < 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; } - tcp->numpocs = numpchgs + old_poc - 1; -} - -static void j2k_read_crg(opj_j2k_t *j2k) { - int len, i, Xcrg_i, Ycrg_i; - - opj_cio_t *cio = j2k->cio; - int numcomps = j2k->image->numcomps; - - len = cio_read(cio, 2); /* Lcrg */ - for (i = 0; i < numcomps; i++) { - Xcrg_i = cio_read(cio, 2); /* Xcrg_i */ - Ycrg_i = cio_read(cio, 2); /* Ycrg_i */ + /* first marker */ + opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */ + p_header_data += 2; + if + (l_tmp != 0) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); + return true; } -} - -static void j2k_read_tlm(opj_j2k_t *j2k) { - int len, Ztlm, Stlm, ST, SP, tile_tlm, i; - long int Ttlm_i, Ptlm_i; - - opj_cio_t *cio = j2k->cio; - - len = cio_read(cio, 2); /* Ltlm */ - Ztlm = cio_read(cio, 1); /* Ztlm */ - Stlm = cio_read(cio, 1); /* Stlm */ - ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02); - SP = (Stlm >> 6) & 0x01; - tile_tlm = (len - 4) / ((SP + 1) * 2 + ST); - for (i = 0; i < tile_tlm; i++) { - Ttlm_i = cio_read(cio, ST); /* Ttlm_i */ - Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */ + if + (p_header_size < 7) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; } -} - -static void j2k_read_plm(opj_j2k_t *j2k) { - int len, i, Zplm, Nplm, add, packet_len = 0; + opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */ + ++p_header_data; - opj_cio_t *cio = j2k->cio; - - len = cio_read(cio, 2); /* Lplm */ - Zplm = cio_read(cio, 1); /* Zplm */ - len -= 3; - while (len > 0) { - Nplm = cio_read(cio, 4); /* Nplm */ - len -= 4; - for (i = Nplm; i > 0; i--) { - add = cio_read(cio, 1); - len--; - packet_len = (packet_len << 7) + add; /* Iplm_ij */ - if ((add & 0x80) == 0) { - /* New packet */ - packet_len = 0; + l_mcc_record = l_tcp->m_mcc_records; + for + (i=0;im_nb_mcc_records;++i) + { + if + (l_mcc_record->m_index == l_indix) + { + break; + } + ++l_mcc_record; + } + /** NOT FOUND */ + if + (i == l_tcp->m_nb_mcc_records) + { + if + (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) + { + l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS; + l_tcp->m_mcc_records = opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); + if + (! l_tcp->m_mcc_records) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; + } + l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; + memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); + } + l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records; + } + l_mcc_record->m_index = l_indix; + + /* only one marker atm */ + opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */ + p_header_data+=2; + if + (l_tmp != 0) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n"); + return true; + } + opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */ + p_header_data+=2; + if + (l_nb_collections > 1) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n"); + return true; + } + p_header_size -= 7; + for + (i=0;i array based decorrelation */ + ++p_header_data; + if + (l_tmp != 1) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n"); + return true; + } + opj_read_bytes(p_header_data,&l_nb_comps,2); + p_header_data+=2; + p_header_size-=3; + l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); + l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff; + if + (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; + } + p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2); + for + (j=0;jm_nb_comps;++j) + { + opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/ + p_header_data+=l_nb_bytes_by_comp; + if + (l_tmp != j) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); + return true; + } + } + opj_read_bytes(p_header_data,&l_nb_comps,2); + p_header_data+=2; + l_nb_bytes_by_comp = 1 + (l_nb_comps>>15); + l_nb_comps &= 0x7fff; + if + (l_nb_comps != l_mcc_record->m_nb_comps) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n"); + return true; + } + if + (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; + } + p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3); + for + (j=0;jm_nb_comps;++j) + { + opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/ + p_header_data+=l_nb_bytes_by_comp; + if + (l_tmp != j) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n"); + return true; + } + } + opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/ + p_header_data += 3; + l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1); + l_mcc_record->m_decorrelation_array = 00; + l_mcc_record->m_offset_array = 00; + l_indix = l_tmp & 0xff; + if + (l_indix != 0) + { + l_mct_data = l_tcp->m_mct_records; + for + (j=0;jm_nb_mct_records;++j) + { + if + (l_mct_data->m_index == l_indix) + { + l_mcc_record->m_decorrelation_array = l_mct_data; + break; + } + ++l_mct_data; + } + if + (l_mcc_record->m_decorrelation_array == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; + } + } + l_indix = (l_tmp >> 8) & 0xff; + if + (l_indix != 0) + { + l_mct_data = l_tcp->m_mct_records; + for + (j=0;jm_nb_mct_records;++j) + { + if + (l_mct_data->m_index == l_indix) + { + l_mcc_record->m_offset_array = l_mct_data; + break; + } + ++l_mct_data; + } + if + (l_mcc_record->m_offset_array == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; } - if (len <= 0) - break; } } + if + (p_header_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n"); + return false; + } + ++l_tcp->m_nb_mcc_records; + return true; } -static void j2k_read_plt(opj_j2k_t *j2k) { - int len, i, Zplt, packet_len = 0, add; +/** + * Writes the MCT marker (Multiple Component Transform) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_mct_record( + opj_j2k_t *p_j2k, + opj_mct_data_t * p_mct_record, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_mct_size; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tmp; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_mct_size = 10 + p_mct_record->m_data_size; + if + (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_mct_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size; + } - opj_cio_t *cio = j2k->cio; + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; - len = cio_read(cio, 2); /* Lplt */ - Zplt = cio_read(cio, 1); /* Zplt */ - for (i = len - 3; i > 0; i--) { - add = cio_read(cio, 1); - packet_len = (packet_len << 7) + add; /* Iplt_i */ - if ((add & 0x80) == 0) { - /* New packet */ - packet_len = 0; + opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */ + l_current_data += 2; + opj_write_bytes(l_current_data,0,2); /* Zmct */ + l_current_data += 2; + /* only one marker atm */ + l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10); + opj_write_bytes(l_current_data,l_tmp,2); + l_current_data += 2; + opj_write_bytes(l_current_data,0,2); /* Ymct */ + l_current_data+=2; + + memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size); + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) + { + return false; + } + return true; +} + +/** + * Reads a MCT marker (Multiple Component Transform) + * + * @param p_header_data the data contained in the MCT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCT marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_mct ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_tmp; + OPJ_UINT32 l_indix; + opj_mct_data_t * l_mct_data; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + + if + (p_header_size < 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return false; + } + /* first marker */ + opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */ + p_header_data += 2; + if + (l_tmp != 0) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n"); + return true; + } + if + (p_header_size <= 6) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return false; + } + opj_read_bytes(p_header_data,&l_tmp,2); /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/ + p_header_data += 2; + + l_indix = l_tmp & 0xff; + l_mct_data = l_tcp->m_mct_records; + for + (i=0;im_nb_mct_records;++i) + { + if + (l_mct_data->m_index == l_indix) + { + break; + } + ++l_mct_data; + } + /* NOT FOUND */ + if + (i == l_tcp->m_nb_mct_records) + { + if + (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) + { + l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; + l_tcp->m_mct_records = opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if + (! l_tcp->m_mct_records) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return false; + } + l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; + memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + } + l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records; + } + if + (l_mct_data->m_data) + { + opj_free(l_mct_data->m_data); + l_mct_data->m_data = 00; + } + l_mct_data->m_index = l_indix; + l_mct_data->m_array_type = (l_tmp >> 8) & 3; + l_mct_data->m_element_type = (l_tmp >> 10) & 3; + + opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */ + p_header_data+=2; + if + (l_tmp != 0) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n"); + return true; + } + p_header_size -= 6; + l_mct_data->m_data = opj_malloc(p_header_size); + if + (! l_mct_data->m_data) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n"); + return false; + } + memcpy(l_mct_data->m_data,p_header_data,p_header_size); + l_mct_data->m_data_size = p_header_size; + ++l_tcp->m_nb_mct_records; + return true; +} + +bool j2k_setup_mct_encoding (opj_tcp_t * p_tcp,opj_image_t * p_image) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_indix = 1; + opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00; + opj_simple_mcc_decorrelation_data_t * l_mcc_data; + OPJ_UINT32 l_mct_size,l_nb_elem; + OPJ_FLOAT32 * l_data, * l_current_data; + opj_tccp_t * l_tccp; + + // preconditions + assert(p_tcp != 00); + + if + (p_tcp->mct != 2) + { + return true; + } + + if + (p_tcp->m_mct_decoding_matrix) + { + if + (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) + { + p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; + p_tcp->m_mct_records = opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if + (! p_tcp->m_mct_records) + { + return false; + } + l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + } + l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + + if + (l_mct_deco_data->m_data) + { + opj_free(l_mct_deco_data->m_data); + l_mct_deco_data->m_data = 00; + } + l_mct_deco_data->m_index = l_indix++; + l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION; + l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT; + l_nb_elem = p_image->numcomps * p_image->numcomps; + l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type]; + l_mct_deco_data->m_data = opj_malloc(l_mct_size ); + if + (! l_mct_deco_data->m_data) + { + return false; + } + j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem); + l_mct_deco_data->m_data_size = l_mct_size; + ++p_tcp->m_nb_mct_records; + } + + if + (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) + { + p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS; + p_tcp->m_mct_records = opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t)); + if + (! p_tcp->m_mct_records) + { + return false; + } + l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t)); + if + (l_mct_deco_data) + { + l_mct_deco_data = l_mct_offset_data - 1; } } + l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records; + if + (l_mct_offset_data->m_data) + { + opj_free(l_mct_offset_data->m_data); + l_mct_offset_data->m_data = 00; + } + + l_mct_offset_data->m_index = l_indix++; + l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET; + l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT; + l_nb_elem = p_image->numcomps; + l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type]; + l_mct_offset_data->m_data = opj_malloc(l_mct_size ); + if + (! l_mct_offset_data->m_data) + { + return false; + } + l_data = opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32)); + if + (! l_data) + { + opj_free(l_mct_offset_data->m_data); + l_mct_offset_data->m_data = 00; + return false; + } + l_tccp = p_tcp->tccps; + l_current_data = l_data; + for + (i=0;im_dc_level_shift); + ++l_tccp; + } + j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem); + opj_free(l_data); + l_mct_offset_data->m_data_size = l_mct_size; + ++p_tcp->m_nb_mct_records; + + if + (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) + { + p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS; + p_tcp->m_mcc_records = opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t)); + if + (! p_tcp->m_mcc_records) + { + return false; + } + l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; + memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t)); + + } + l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records; + l_mcc_data->m_decorrelation_array = l_mct_deco_data; + l_mcc_data->m_is_irreversible = 1; + l_mcc_data->m_nb_comps = p_image->numcomps; + l_mcc_data->m_index = l_indix++; + l_mcc_data->m_offset_array = l_mct_offset_data; + ++p_tcp->m_nb_mcc_records; + return true; } -static void j2k_read_ppm(opj_j2k_t *j2k) { - int len, Z_ppm, i, j; - int N_ppm; +/** + * Writes the MCO marker (Multiple component transformation ordering) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_mco( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_mco_size; + opj_tcp_t * l_tcp = 00; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + OPJ_UINT32 i; - opj_cp_t *cp = j2k->cp; - opj_cio_t *cio = j2k->cio; - - len = cio_read(cio, 2); - cp->ppm = 1; - - Z_ppm = cio_read(cio, 1); /* Z_ppm */ - len -= 3; - while (len > 0) { - if (cp->ppm_previous == 0) { - N_ppm = cio_read(cio, 4); /* N_ppm */ - len -= 4; - } else { - N_ppm = cp->ppm_previous; + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + l_mco_size = 5 + l_tcp->m_nb_mcc_records; + if + (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_mco_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; } - j = cp->ppm_store; - if (Z_ppm == 0) { /* First PPM marker */ - cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char)); - cp->ppm_data_first = cp->ppm_data; - cp->ppm_len = N_ppm; - } else { /* NON-first PPM marker */ - cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char)); + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size; + } + + opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/ + ++l_current_data; + + l_mcc_record = l_tcp->m_mcc_records; + for + (i=0;im_nb_mcc_records;++i) + { + opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/ + ++l_current_data; + ++l_mcc_record; + } + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) + { + return false; + } + return true; +} +/** + * Reads a MCO marker (Multiple Component Transform Ordering) + * + * @param p_header_data the data contained in the MCO box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the MCO marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_mco ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_tmp, i; + OPJ_UINT32 l_nb_stages; + opj_tcp_t * l_tcp; + opj_tccp_t * l_tccp; + opj_image_t * l_image; + opj_image_comp_t * l_img_comp; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_image; + l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH ? &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + if + (p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n"); + return false; + } + + opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/ + ++p_header_data; + if + (l_nb_stages > 1) + { + opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n"); + return true; + } + if + (p_header_size != l_nb_stages + 1) + { + opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n"); + return false; + } + + l_tccp = l_tcp->tccps; + l_img_comp = l_image->comps; + for + (i=0;inumcomps;++i) + { + l_tccp->m_dc_level_shift = 0; + ++l_tccp; + } + if + (l_tcp->m_mct_decoding_matrix) + { + opj_free(l_tcp->m_mct_decoding_matrix); + l_tcp->m_mct_decoding_matrix = 00; + } + + for + (i=0;im_image,l_tmp)) + { + return false; + } + } + return true; +} + +bool j2k_add_mct(opj_tcp_t * p_tcp,opj_image_t * p_image, OPJ_UINT32 p_index) +{ + OPJ_UINT32 i; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_deco_array, * l_offset_array; + OPJ_UINT32 l_data_size,l_mct_size, l_offset_size; + OPJ_UINT32 l_nb_elem; + OPJ_UINT32 * l_offset_data, * l_current_offset_data; + opj_tccp_t * l_tccp; + + + // preconditions + assert(p_tcp != 00); + + l_mcc_record = p_tcp->m_mcc_records; + for + (i=0;im_nb_mcc_records;++i) + { + if + (l_mcc_record->m_index == p_index) + { + break; + } + } + if + (i==p_tcp->m_nb_mcc_records) + { + /** element discarded **/ + return true; + } + if + (l_mcc_record->m_nb_comps != p_image->numcomps) + { + /** do not support number of comps != image */ + return true; + } + l_deco_array = l_mcc_record->m_decorrelation_array; + if + (l_deco_array) + { + l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps; + if + (l_deco_array->m_data_size != l_data_size) + { + return false; + } + l_nb_elem = p_image->numcomps * p_image->numcomps; + l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32); + p_tcp->m_mct_decoding_matrix = opj_malloc(l_mct_size); + if + (! p_tcp->m_mct_decoding_matrix ) + { + return false; + } + j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem); + } + l_offset_array = l_mcc_record->m_offset_array; + if + (l_offset_array) + { + l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps; + if + (l_offset_array->m_data_size != l_data_size) + { + return false; + } + l_nb_elem = p_image->numcomps; + l_offset_size = l_nb_elem * sizeof(OPJ_UINT32); + l_offset_data = opj_malloc(l_offset_size); + if + (! l_offset_data ) + { + return false; + } + j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem); + l_tccp = p_tcp->tccps; + l_current_offset_data = l_offset_data; + for + (i=0;inumcomps;++i) + { + l_tccp->m_dc_level_shift = *(l_current_offset_data++); + ++l_tccp; + } + opj_free(l_offset_data); + } + return true; +} + +/** + * Writes the MCT marker (Multiple Component Transform) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_mct_data_group( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + opj_simple_mcc_decorrelation_data_t * l_mcc_record; + opj_mct_data_t * l_mct_record; + opj_tcp_t * l_tcp; + + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + if + (! j2k_write_cbd(p_j2k,p_stream,p_manager)) + { + return false; + } + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + l_mct_record = l_tcp->m_mct_records; + for + (i=0;im_nb_mct_records;++i) + { + if + (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) + { + return false; + } + ++l_mct_record; + } + l_mcc_record = l_tcp->m_mcc_records; + for + (i=0;im_nb_mcc_records;++i) + { + if + (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) + { + return false; + } + ++l_mcc_record; + } + if + (! j2k_write_mco(p_j2k,p_stream,p_manager)) + { + return false; + } + return true; +} + + +/** + * Writes the POC marker (Progression Order Change) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_poc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + OPJ_UINT32 l_written_size = 0; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_UINT32 l_poc_room; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + l_tccp = &l_tcp->tccps[0]; + l_nb_comp = p_j2k->m_image->numcomps; + l_nb_poc = 1 + l_tcp->numpocs; + if + (l_nb_comp <= 256) + { + l_poc_room = 1; + } + else + { + l_poc_room = 2; + } + l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; + if + (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_poc_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size; + } + + j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager); + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) + { + return false; + } + return true; +} + + +/** + * Writes EPC ???? + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_epc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + opj_codestream_info_t * l_info = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_info = p_j2k->cstr_info; + if + (l_info) + { + l_info->codestream_size = opj_stream_tell(p_stream); + /* UniPG>> */ + /* The following adjustment is done to adjust the codestream size */ + /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ + /* the first bunch of bytes is not in the codestream */ + l_info->codestream_size -= l_info->main_head_start; + /* <correct) { - if (!cp->ppm_data) { - opj_event_msg(j2k->cinfo, EVT_ERROR, - "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n", - cio_tell(cio)); - if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_free(cp->ppm_data); - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); - return; - } - } + /* + preparation of JPWL marker segments + */ + if(cp->epc_on) { + + /* encode according to JPWL */ + jpwl_encode(p_j2k, p_stream, image); + + } +#endif /* USE_JPWL */ + return true; +} + + +/** + * Gets the maximum size taken by the writting of a POC. + */ +OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_t *p_j2k) +{ + opj_tcp_t * l_tcp = 00; + OPJ_UINT32 l_nb_tiles = 0; + OPJ_UINT32 l_max_poc = 0; + OPJ_UINT32 i; + + l_tcp = p_j2k->m_cp.tcps; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + + for + (i=0;inumpocs); + ++l_tcp; + } + ++l_max_poc; + return 4 + 9 * l_max_poc; +} + + +/** + * Writes the POC marker (Progression Order Change) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +void j2k_write_poc_in_memory( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_nb_poc; + OPJ_UINT32 l_poc_size; + opj_image_t *l_image = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + opj_poc_t *l_current_poc = 00; + OPJ_UINT32 l_poc_room; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + + l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]; + l_tccp = &l_tcp->tccps[0]; + l_image = p_j2k->m_image; + l_nb_comp = l_image->numcomps; + l_nb_poc = 1 + l_tcp->numpocs; + if + (l_nb_comp <= 256) + { + l_poc_room = 1; + } + else + { + l_poc_room = 2; + } + l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc; + + l_current_data = p_data; + + opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */ + l_current_data += 2; + + l_current_poc = l_tcp->pocs; + for + (i = 0; i < l_nb_poc; ++i) + { + opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */ + ++l_current_data; + opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */ + l_current_data+=l_poc_room; + opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */ + l_current_data+=2; + opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */ + ++l_current_data; + opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */ + l_current_data+=l_poc_room; + opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */ + ++l_current_data; + + /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/ + l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers); + l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions); + l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp); + ++l_current_poc; + } + * p_data_written = l_poc_size; +} + + +/** + * Reads a POC marker (Progression Order Change) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_poc ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_nb_comp; + opj_image_t * l_image = 00; + OPJ_UINT32 l_old_poc_nb,l_current_poc_nb,l_current_poc_remaining; + OPJ_UINT32 l_chunk_size; + OPJ_UINT32 l_tmp; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_poc_t *l_current_poc = 00; + OPJ_UINT32 l_comp_room; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_image; + l_nb_comp = l_image->numcomps; + if + (l_nb_comp <= 256) + { + l_comp_room = 1; + } + else + { + l_comp_room = 2; + } + l_chunk_size = 5 + 2 * l_comp_room; + l_current_poc_nb = p_header_size / l_chunk_size; + l_current_poc_remaining = p_header_size % l_chunk_size; + + if + ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n"); + return false; + } + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH) ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0; + l_current_poc_nb += l_old_poc_nb; + assert(l_current_poc_nb < 32); + + /* now poc is in use.*/ + l_tcp->POC = 1; + + l_current_poc = &l_tcp->pocs[l_old_poc_nb]; + for + (i = l_old_poc_nb; i < l_current_poc_nb; ++i) + { + opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */ + ++p_header_data; + opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */ + p_header_data+=l_comp_room; + opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */ + p_header_data+=2; + opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */ + ++p_header_data; + opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */ + p_header_data+=l_comp_room; + opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */ + ++p_header_data; + l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp; + /* make sure comp is in acceptable bounds */ + l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp); + ++l_current_poc; + } + l_tcp->numpocs = l_current_poc_nb - 1; + return true; +} + +/** + * Writes the RGN marker (Region Of Interest) + * + * @param p_tile_no the tile to output + * @param p_comp_no the component to output + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_rgn( + opj_j2k_t *p_j2k, + OPJ_UINT32 p_tile_no, + OPJ_UINT32 p_comp_no, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_nb_comp; + OPJ_UINT32 l_rgn_size; + opj_image_t *l_image = 00; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + opj_tccp_t *l_tccp = 00; + OPJ_UINT32 l_comp_room; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_tile_no]; + l_tccp = &l_tcp->tccps[p_comp_no]; + + l_nb_comp = l_image->numcomps; + + if + (l_nb_comp <= 256) + { + l_comp_room = 1; + } + else + { + l_comp_room = 2; + } + l_rgn_size = 6 + l_comp_room; + + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */ + l_current_data += 2; + opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */ + l_current_data+=l_comp_room; + opj_write_bytes(l_current_data, 0,1); /* Srgn */ + ++l_current_data; + opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */ + ++l_current_data; + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) + { + return false; + } + return true; +} + +/** + * Reads a RGN marker (Region Of Interest) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_rgn ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_nb_comp; + opj_image_t * l_image = 00; + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_comp_room; + OPJ_UINT32 l_comp_no; + OPJ_UINT32 l_roi_sty; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_image; + l_nb_comp = l_image->numcomps; + if + (l_nb_comp <= 256) + { + l_comp_room = 1; + } + else + { + l_comp_room = 2; + } + if + (p_header_size != 2 + l_comp_room) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); + return false; + } + + l_cp = &(p_j2k->m_cp); + l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_TPH) ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp; + + opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */ + p_header_data+=l_comp_room; + opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */ + ++p_header_data; + +#ifdef USE_JPWL + if (p_j2k->m_cp->correct) { + /* totlen is negative or larger than the bytes left!!! */ + if (compno >= numcomps) { + opj_event_msg(p_j2k->cinfo, EVT_ERROR, + "JPWL: bad component number in RGN (%d when there are only %d)\n", + compno, numcomps); + if (!JPWL_ASSUME || JPWL_ASSUME) { + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + return; } -#endif - - cp->ppm_data_first = cp->ppm_data; - cp->ppm_len = N_ppm + cp->ppm_store; } - for (i = N_ppm; i > 0; i--) { /* Read packet header */ - cp->ppm_data[j] = cio_read(cio, 1); - j++; - len--; - if (len == 0) - break; /* Case of non-finished packet header in present marker but finished in next one */ + }; +#endif /* USE_JPWL */ + + opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */ + ++p_header_data; + return true; + +} + +/** + * Writes the TLM marker (Tile Length Marker) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_tlm( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_BYTE * l_current_data = 00; + OPJ_UINT32 l_tlm_size; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + if + (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) + { + p_j2k->m_specific_param.m_encoder.m_header_tile_data + = opj_realloc( + p_j2k->m_specific_param.m_encoder.m_header_tile_data, + l_tlm_size); + if + (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + return false; } - cp->ppm_previous = i - 1; - cp->ppm_store = j; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size; } + l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data; + + /* change the way data is written to avoid seeking if possible */ + // TODO + p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream); + + opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */ + l_current_data += 2; + opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */ + l_current_data += 2; + opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/ + ++l_current_data; + opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ + ++l_current_data; + /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */ + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) + { + return false; + } + return true; } -static void j2k_read_ppt(opj_j2k_t *j2k) { - int len, Z_ppt, i, j = 0; - - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = cp->tcps + j2k->curtileno; - opj_cio_t *cio = j2k->cio; - - len = cio_read(cio, 2); - Z_ppt = cio_read(cio, 1); - tcp->ppt = 1; - if (Z_ppt == 0) { /* First PPT marker */ - tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char)); - tcp->ppt_data_first = tcp->ppt_data; - tcp->ppt_store = 0; - tcp->ppt_len = len - 3; - } else { /* NON-first PPT marker */ - tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char)); - tcp->ppt_data_first = tcp->ppt_data; - tcp->ppt_len = len - 3 + tcp->ppt_store; +/** + * Reads a TLM marker (Tile Length Marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_tlm ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size; + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_header_size < 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + return false; } - j = tcp->ppt_store; - for (i = len - 3; i > 0; i--) { - tcp->ppt_data[j] = cio_read(cio, 1); - j++; + p_header_size -= 2; + + + opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */ + ++p_header_data; + opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */ + ++p_header_data; + + l_ST = ((l_Stlm >> 4) & 0x3); + l_SP = (l_Stlm >> 6) & 0x1; + + l_Ptlm_size = (l_SP + 1) * 2; + l_quotient = l_Ptlm_size + l_ST; + + l_tot_num_tp = p_header_size / l_quotient; + l_tot_num_tp_remaining = p_header_size % l_quotient; + if + (l_tot_num_tp_remaining != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n"); + return false; } - tcp->ppt_store = j; + /* Do not care of this at the moment since only local variables are set here */ + /* + for + (i = 0; i < l_tot_num_tp; ++i) + { + opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i + p_header_data += l_ST; + opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i + p_header_data += l_Ptlm_size; + }*/ + return true; } -static void j2k_write_tlm(opj_j2k_t *j2k){ - int lenp; - opj_cio_t *cio = j2k->cio; - j2k->tlm_start = cio_tell(cio); - cio_write(cio, J2K_MS_TLM, 2);/* TLM */ - lenp = 4 + (5*j2k->totnum_tp); - cio_write(cio,lenp,2); /* Ltlm */ - cio_write(cio, 0,1); /* Ztlm=0*/ - cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */ - cio_skip(cio,5*j2k->totnum_tp); +/** + * Reads a CRG marker (Component registration) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_crg ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_nb_comp; + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + l_nb_comp = p_j2k->m_image->numcomps; + + if + (p_header_size != l_nb_comp *4) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n"); + return false; + } + /* Do not care of this at the moment since only local variables are set here */ + /* + for + (i = 0; i < l_nb_comp; ++i) + { + opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i + p_header_data+=2; + opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i + p_header_data+=2; + } + */ + return true; } -static void j2k_write_sot(opj_j2k_t *j2k) { - int lenp, len; +/** + * Reads a PLM marker (Packet length, main header marker) + * + * @param p_header_data the data contained in the TLM box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the TLM marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_plm ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + /* Do not care of this at the moment since only local variables are set here */ + /* + opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm + ++p_header_data; + --p_header_size; - opj_cio_t *cio = j2k->cio; + while + (p_header_size > 0) + { + opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm + ++p_header_data; + p_header_size -= (1+l_Nplm); + if + (p_header_size < 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + for + (i = 0; i < l_Nplm; ++i) + { + opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij + ++p_header_data; + // take only the last seven bytes + l_packet_len |= (l_tmp & 0x7f); + if + (l_tmp & 0x80) + { + l_packet_len <<= 7; + } + else + { + // store packet length and proceed to next packet + l_packet_len = 0; + } + } + if + (l_packet_len != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + } + */ + return true; +} - j2k->sot_start = cio_tell(cio); - cio_write(cio, J2K_MS_SOT, 2); /* SOT */ - lenp = cio_tell(cio); - cio_skip(cio, 2); /* Lsot (further) */ - cio_write(cio, j2k->curtileno, 2); /* Isot */ - cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */ - cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */ - cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */ - len = cio_tell(cio) - lenp; - cio_seek(cio, lenp); - cio_write(cio, len, 2); /* Lsot */ - cio_seek(cio, lenp + len); +/** + * Reads a PLT marker (Packet length, tile-part header) + * + * @param p_header_data the data contained in the PLT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PLT marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_plt ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i; + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + + opj_read_bytes(p_header_data,&l_Zplt,1); // Zplt + ++p_header_data; + --p_header_size; + for + (i = 0; i < p_header_size; ++i) + { + opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij + ++p_header_data; + // take only the last seven bytes + l_packet_len |= (l_tmp & 0x7f); + if + (l_tmp & 0x80) + { + l_packet_len <<= 7; + } + else + { + // store packet length and proceed to next packet + l_packet_len = 0; + } + } + if + (l_packet_len != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n"); + return false; + } + return true; +} + +/** + * Reads a PPM marker (Packed packet headers, main header) + * + * @param p_header_data the data contained in the POC box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the POC marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_ppm ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + + opj_cp_t *l_cp = 00; + OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n"); + return false; + } + l_cp = &(p_j2k->m_cp); + l_cp->ppm = 1; + + opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */ + ++p_header_data; + --p_header_size; + + // first PPM marker + if + (l_Z_ppm == 0) + { + if + (p_header_size < 4) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n"); + return false; + } + // read a N_ppm + opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ + p_header_data+=4; + p_header_size-=4; + /* First PPM marker */ + l_cp->ppm_len = l_N_ppm; + l_cp->ppm_data_size = 0; + l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len); + l_cp->ppm_data = l_cp->ppm_buffer; + if + (l_cp->ppm_buffer == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); + return false; + } + memset(l_cp->ppm_buffer,0,l_cp->ppm_len); + } + + while + (true) + { + if + (l_cp->ppm_data_size == l_cp->ppm_len) + { + if + (p_header_size >= 4) + { + // read a N_ppm + opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */ + p_header_data+=4; + p_header_size-=4; + l_cp->ppm_len += l_N_ppm ; + l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len); + l_cp->ppm_data = l_cp->ppm_buffer; + if + (l_cp->ppm_buffer == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n"); + return false; + } + memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm); + } + else + { + return false; + } + } + l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size; + if + (l_remaining_data <= p_header_size) + { + /* we must store less information than available in the packet */ + memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data); + l_cp->ppm_data_size = l_cp->ppm_len; + p_header_size -= l_remaining_data; + p_header_data += l_remaining_data; + } + else + { + memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size); + l_cp->ppm_data_size += p_header_size; + p_header_data += p_header_size; + p_header_size = 0; + break; + } + } + return true; +} + +/** + * Reads a PPT marker (Packed packet headers, tile-part header) + * + * @param p_header_data the data contained in the PPT box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the PPT marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_ppt ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_header_data, + OPJ_UINT32 p_header_size, + struct opj_event_mgr * p_manager + ) +{ + + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_Z_ppt; + + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_header_size < 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n"); + return false; + } + + l_cp = &(p_j2k->m_cp); + l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]); + l_tcp->ppt = 1; + + opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */ + ++p_header_data; + --p_header_size; + + // first PPM marker + if + (l_Z_ppt == 0) + { + /* First PPM marker */ + l_tcp->ppt_len = p_header_size; + l_tcp->ppt_data_size = 0; + l_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_tcp->ppt_len); + l_tcp->ppt_data = l_tcp->ppt_buffer; + if + (l_tcp->ppt_buffer == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n"); + return false; + } + memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); + } + else + { + l_tcp->ppt_len += p_header_size; + l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len); + if + (l_tcp->ppt_buffer == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n"); + return false; + } + l_tcp->ppt_data = l_tcp->ppt_buffer; + memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size); + } + memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size); + l_tcp->ppt_data_size += p_header_size; + return true; +} + +/** + * Writes the SOT marker (Start of tile-part) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_sot( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + const struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */ + p_data += 2; + + opj_write_bytes(p_data,10,2); /* Lsot */ + p_data += 2; + + opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */ + p_data += 2; + + /* Psot */ + p_data += 4; + + opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */ + ++p_data; + + opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */ + ++p_data; /* UniPG>> */ #ifdef USE_JPWL /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2); + j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2); #endif /* USE_JPWL */ - /* <cp; - opj_cio_t *cio = j2k->cio; + opj_cp_t *l_cp = 00; + opj_tcp_t *l_tcp = 00; + OPJ_UINT32 l_tot_len, l_num_parts = 0; + OPJ_UINT32 l_current_part; + OPJ_UINT32 l_tile_x,l_tile_y; - len = cio_read(cio, 2); - tileno = cio_read(cio, 2); + // preconditions + assert(p_header_data != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_header_size != 8) + { + opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n"); + return false; + } + l_cp = &(p_j2k->m_cp); + opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */ + p_header_data+=2; + + + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_tile_x = p_j2k->m_current_tile_number % l_cp->tw; + l_tile_y = p_j2k->m_current_tile_number / l_cp->tw; #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { static int backup_tileno = 0; /* tileno is negative or larger than the number of tiles!!! */ if ((tileno < 0) || (tileno > (cp->tw * cp->th))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: bad tile number (%d out of a maximum of %d)\n", tileno, (cp->tw * cp->th)); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ tileno = backup_tileno; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" "- setting tile number to %d\n", tileno); } @@ -1309,38 +5902,28 @@ static void j2k_read_sot(opj_j2k_t *j2k) { }; #endif /* USE_JPWL */ - if (cp->tileno_size == 0) { - cp->tileno[cp->tileno_size] = tileno; - cp->tileno_size++; - } else { - i = 0; - while (i < cp->tileno_size && status == 0) { - status = cp->tileno[i] == tileno ? 1 : 0; - i++; - } - if (status == 0) { - cp->tileno[cp->tileno_size] = tileno; - cp->tileno_size++; - } - } - - totlen = cio_read(cio, 4); + /* look for the tile in the list of already processed tile (in parts). */ + /* Optimization possible here with a more complex data structure and with the removing of tiles */ + /* since the time taken by this function can only grow at the time */ + + opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */ + p_header_data+=4; #ifdef USE_JPWL - if (j2k->cp->correct) { + if (p_j2k->m_cp->correct) { /* totlen is negative or larger than the bytes left!!! */ - if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) { - opj_event_msg(j2k->cinfo, EVT_ERROR, + if ((totlen < 0) || (totlen > (p_stream_numbytesleft(p_stream) + 8))) { + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: bad tile byte size (%d bytes against %d bytes left)\n", - totlen, cio_numbytesleft(cio) + 8); + totlen, p_stream_numbytesleft(p_stream) + 8); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); return; } /* we try to correct */ totlen = 0; - opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" + opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n" "- setting Psot to %d => assuming it is the last tile\n", totlen); } @@ -1348,316 +5931,866 @@ static void j2k_read_sot(opj_j2k_t *j2k) { }; #endif /* USE_JPWL */ - if (!totlen) - totlen = cio_numbytesleft(cio) + 8; - - partno = cio_read(cio, 1); - numparts = cio_read(cio, 1); - - j2k->curtileno = tileno; - j2k->cur_tp_num = partno; - j2k->eot = cio_getbp(cio) - 12 + totlen; - j2k->state = J2K_STATE_TPH; - tcp = &cp->tcps[j2k->curtileno]; + if + (!l_tot_len) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot read data with no size known, giving up\n"); + return false; + } + opj_read_bytes(p_header_data,&l_current_part ,1); /* Psot */ + ++p_header_data; + + opj_read_bytes(p_header_data,&l_num_parts ,1); /* Psot */ + ++p_header_data; + + if + (l_num_parts != 0) + { + l_tcp->m_nb_tile_parts = l_num_parts; + } + if + (l_tcp->m_nb_tile_parts) + { + if + (l_tcp->m_nb_tile_parts == (l_current_part + 1)) + { + p_j2k->m_specific_param.m_decoder.m_can_decode = 1; + } + } + p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_TPH; + p_j2k->m_specific_param.m_decoder.m_skip_data = + (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x) + || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x) + || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y) + || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y); /* Index */ - if (j2k->cstr_info) { - if (tcp->first) { - if (tileno == 0) - j2k->cstr_info->main_head_end = cio_tell(cio) - 13; - j2k->cstr_info->tile[tileno].tileno = tileno; - j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12; - j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1; - j2k->cstr_info->tile[tileno].num_tps = numparts; - if (numparts) - j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); - else - j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) - } - else { - j2k->cstr_info->tile[tileno].end_pos += totlen; - } - j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12; - j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = - j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; - } - if (tcp->first == 1) { - /* Initialization PPT */ - opj_tccp_t *tmp = tcp->tccps; - memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t)); - tcp->ppt = 0; - tcp->ppt_data = NULL; - tcp->ppt_data_first = NULL; - tcp->tccps = tmp; - - for (i = 0; i < j2k->image->numcomps; i++) { - tcp->tccps[i] = j2k->default_tcp->tccps[i]; + /* move this onto a separate method to call before reading any SOT */ + /*if + TODO + (p_j2k->cstr_info) + { + if + (l_tcp->first) + { + if + (tileno == 0) + { + p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13; + } + p_j2k->cstr_info->tile[tileno].tileno = tileno; + p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12; + p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1; + p_j2k->cstr_info->tile[tileno].num_tps = numparts; + if + (numparts) + { + p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t)); + } + else + { + p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10) + } } - cp->tcps[j2k->curtileno].first = 0; - } + else + { + p_j2k->cstr_info->tile[tileno].end_pos += totlen; + } + p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12; + p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = + p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1; + }*/ + return true; } -static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) { - int l, layno; - int totlen; - opj_tcp_t *tcp = NULL; - opj_codestream_info_t *cstr_info = NULL; - - opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */ - opj_cp_t *cp = j2k->cp; - opj_cio_t *cio = j2k->cio; +/** + * Writes the SOD marker (Start of data) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_sod( + opj_j2k_t *p_j2k, + struct opj_tcd * p_tile_coder, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + const struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + opj_tcp_t *l_tcp = 00; + opj_codestream_info_t *l_cstr_info = 00; + opj_cp_t *l_cp = 00; - tcd->tp_num = j2k->tp_num ; - tcd->cur_tp_num = j2k->cur_tp_num; - - cio_write(cio, J2K_MS_SOD, 2); - if (j2k->curtileno == 0) { - j2k->sod_start = cio_tell(cio) + j2k->pos_correction; - } + OPJ_UINT32 l_size_tile; + OPJ_UINT32 l_remaining_data; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */ + p_data += 2; + + /* make room for the EOF marker */ + l_remaining_data = p_total_data_size - 4; + + l_cp = &(p_j2k->m_cp); + l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number]; + l_cstr_info = p_j2k->cstr_info; + + /* update tile coder */ + p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ; + p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + l_size_tile = l_cp->th * l_cp->tw; /* INDEX >> */ - cstr_info = j2k->cstr_info; - if (cstr_info) { - if (!j2k->cur_tp_num ) { - cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1; - j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno; + if + (l_cstr_info) + { + if + (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) + { + //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1; + l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number; } - else{ - if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio)) - cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio); + else + { + /* + TODO + if + (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream)) + { + cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream); + }*/ + } /* UniPG>> */ #ifdef USE_JPWL /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2); + j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2); #endif /* USE_JPWL */ /* <tcps[j2k->curtileno]; - for (layno = 0; layno < tcp->numlayers; layno++) { - if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) { - tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw)); - } else if (tcp->rates[layno]) { - tcp->rates[layno]=1; + if + (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) + { + p_tile_coder->tcd_image->tiles->packno = 0; + if + (l_cstr_info) + { + l_cstr_info->packno = 0; } } - if(j2k->cur_tp_num == 0){ - tcd->tcd_image->tiles->packno = 0; - if(cstr_info) - cstr_info->packno = 0; + *p_data_written = 0; + if + (! tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n"); + return false; } - - l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info); - - /* Writing Psot in SOT marker */ - totlen = cio_tell(cio) + l - j2k->sot_start; - cio_seek(cio, j2k->sot_start + 6); - cio_write(cio, totlen, 4); - cio_seek(cio, j2k->sot_start + totlen); - /* Writing Ttlm and Ptlm in TLM marker */ - if(cp->cinema){ - cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num)); - cio_write(cio, j2k->curtileno, 1); - cio_write(cio, totlen, 4); - } - cio_seek(cio, j2k->sot_start + totlen); + *p_data_written += 2; + return true; } -static void j2k_read_sod(opj_j2k_t *j2k) { - int len, truncate = 0, i; - unsigned char *data = NULL, *data_ptr = NULL; +/** + * Updates the Tile Length Marker. + */ +void j2k_update_tlm ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_part_size + ) +{ + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */ + ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current; + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */ + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4; +} - opj_cio_t *cio = j2k->cio; - int curtileno = j2k->curtileno; + +/** + * Reads a SOD marker (Start Of Data) + * + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_sod ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_current_read_size; + opj_codestream_info_t * l_cstr_info = 00; + OPJ_BYTE ** l_current_data = 00; + opj_tcp_t * l_tcp = 00; + OPJ_UINT32 * l_tile_len = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]); + p_j2k->m_specific_param.m_decoder.m_sot_length -= 2; + l_cstr_info = p_j2k->cstr_info; + + l_current_data = &(l_tcp->m_data); + l_tile_len = &l_tcp->m_data_size; + + if + (! *l_current_data) + { + *l_current_data = (OPJ_BYTE*) my_opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length); + } + else + { + *l_current_data = (OPJ_BYTE*) my_opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length); + } + if + (*l_current_data == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile\n"); + return false; + } /* Index */ - if (j2k->cstr_info) { - j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header = - cio_tell(cio) + j2k->pos_correction - 1; - if (j2k->cur_tp_num == 0) - j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1; - j2k->cstr_info->packno = 0; - } - - len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1); - - if (len == cio_numbytesleft(cio) + 1) { - truncate = 1; /* Case of a truncate codestream */ - } - - data = j2k->tile_data[curtileno]; - data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char)); - - data_ptr = data + j2k->tile_len[curtileno]; - for (i = 0; i < len; i++) { - data_ptr[i] = cio_read(cio, 1); - } - - j2k->tile_len[curtileno] += len; - j2k->tile_data[curtileno] = data; - - if (!truncate) { - j2k->state = J2K_STATE_TPHSOT; - } else { - j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */ - } - j2k->cur_tp_num++; -} - -static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) { - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = &cp->tcps[tileno]; - opj_cio_t *cio = j2k->cio; - int numcomps = j2k->image->numcomps; - - cio_write(cio, J2K_MS_RGN, 2); /* RGN */ - cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */ - cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */ - cio_write(cio, 0, 1); /* Srgn */ - cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */ -} - -static void j2k_read_rgn(opj_j2k_t *j2k) { - int len, compno, roisty; - - opj_cp_t *cp = j2k->cp; - opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp; - opj_cio_t *cio = j2k->cio; - int numcomps = j2k->image->numcomps; - - len = cio_read(cio, 2); /* Lrgn */ - compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */ - roisty = cio_read(cio, 1); /* Srgn */ - -#ifdef USE_JPWL - if (j2k->cp->correct) { - /* totlen is negative or larger than the bytes left!!! */ - if (compno >= numcomps) { - opj_event_msg(j2k->cinfo, EVT_ERROR, - "JPWL: bad component number in RGN (%d when there are only %d)\n", - compno, numcomps); - if (!JPWL_ASSUME || JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n"); - return; - } + if + (l_cstr_info) + { + OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1; + l_cstr_info->tile[p_j2k->m_current_tile_number].tp[p_j2k->m_specific_param.m_encoder.m_current_tile_part_number].tp_end_header = l_current_pos; + if + (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) + { + l_cstr_info->tile[p_j2k->m_current_tile_number].end_header = l_current_pos; } - }; -#endif /* USE_JPWL */ - - tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */ + l_cstr_info->packno = 0; + } + l_current_read_size = opj_stream_read_data(p_stream, *l_current_data + *l_tile_len , p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager); + if + (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) + { + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_NEOC; + } + else + { + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_TPHSOT; + } + *l_tile_len += l_current_read_size; + return true; } -static void j2k_write_eoc(opj_j2k_t *j2k) { - opj_cio_t *cio = j2k->cio; - /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */ - cio_write(cio, J2K_MS_EOC, 2); +/** + * Writes the EOC marker (End of Codestream) + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_eoc( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */ + /* UniPG>> */ #ifdef USE_JPWL /* update markers struct */ - j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2); + j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2); #endif /* USE_JPWL */ -/* <m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) + { + return false; + } + if + (! opj_stream_flush(p_stream,p_manager)) + { + return false; + } + return true; } -static void j2k_read_eoc(opj_j2k_t *j2k) { - int i, tileno; - bool success; - /* if packets should be decoded */ - if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) { - opj_tcd_t *tcd = tcd_create(j2k->cinfo); - tcd_malloc_decode(tcd, j2k->image, j2k->cp); - for (i = 0; i < j2k->cp->tileno_size; i++) { - tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info); - tileno = j2k->cp->tileno[i]; - success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info); - opj_free(j2k->tile_data[tileno]); - j2k->tile_data[tileno] = NULL; - tcd_free_decode_tile(tcd, i); - if (success == false) { - j2k->state |= J2K_STATE_ERR; +/** + * Inits the Info + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_init_info( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + opj_codestream_info_t * l_cstr_info = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + l_cstr_info = p_j2k->cstr_info; + + if + (l_cstr_info) + { + OPJ_UINT32 compno; + l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t)); + l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0; + l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0; + l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg; + l_cstr_info->tw = p_j2k->m_cp.tw; + l_cstr_info->th = p_j2k->m_cp.th; + l_cstr_info->tile_x = p_j2k->m_cp.tdx; /* new version parser */ + l_cstr_info->tile_y = p_j2k->m_cp.tdy; /* new version parser */ + l_cstr_info->tile_Ox = p_j2k->m_cp.tx0; /* new version parser */ + l_cstr_info->tile_Oy = p_j2k->m_cp.ty0; /* new version parser */ + l_cstr_info->numcomps = p_j2k->m_image->numcomps; + l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers; + l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32)); + for (compno=0; compno < p_j2k->m_image->numcomps; compno++) { + l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1; + } + l_cstr_info->D_max = 0.0; /* ADD Marcela */ + l_cstr_info->main_head_start = opj_stream_tell(p_stream); /* position of SOC */ + l_cstr_info->maxmarknum = 100; + l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t)); + l_cstr_info->marknum = 0; + } + return j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_image,p_manager); +} + +/** + * Creates a tile-coder decoder. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_create_tcd( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + p_j2k->m_tcd = tcd_create(false); + if + (! p_j2k->m_tcd) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n"); + return false; + } + if + (! tcd_init(p_j2k->m_tcd,p_j2k->m_image,&p_j2k->m_cp)) + { + tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + return false; + } + return true; +} + +OPJ_FLOAT32 get_tp_stride (opj_tcp_t * p_tcp) +{ + return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14); +} + +OPJ_FLOAT32 get_default_stride (opj_tcp_t * p_tcp) +{ + return 0; +} + +/** + * Updates the rates of the tcp. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_update_rates( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + opj_cp_t * l_cp = 00; + opj_image_t * l_image = 00; + opj_tcp_t * l_tcp = 00; + opj_image_comp_t * l_img_comp = 00; + + OPJ_UINT32 i,j,k; + OPJ_INT32 l_x0,l_y0,l_x1,l_y1; + OPJ_FLOAT32 * l_rates = 0; + OPJ_FLOAT32 l_sot_remove; + OPJ_UINT32 l_bits_empty, l_size_pixel; + OPJ_UINT32 l_tile_size = 0; + OPJ_UINT32 l_last_res; + OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_t *) = 00; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + + l_cp = &(p_j2k->m_cp); + l_image = p_j2k->m_image; + l_tcp = l_cp->tcps; + + l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy; + l_size_pixel = l_image->numcomps * l_image->comps->prec; + l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw); + + if + (l_cp->m_specific_param.m_enc.m_tp_on) + { + l_tp_stride_func = get_tp_stride; + } + else + { + l_tp_stride_func = get_default_stride; + } + + for + (i=0;ith;++i) + { + for + (j=0;jtw;++j) + { + OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers; + /* 4 borders of the tile rescale on the image if necessary */ + l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0); + l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0); + l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1); + l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1); + l_rates = l_tcp->rates; + + /* Modification of the RATE >> */ + if + (*l_rates) + { + *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0))) + / + ((*l_rates) * l_bits_empty) + ) + - + l_offset; + } + ++l_rates; + for + (k = 1; k < l_tcp->numlayers; ++k) + { + if + (*l_rates) + { + *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0))) + / + ((*l_rates) * l_bits_empty) + ) + - + l_offset; + } + ++l_rates; + } + ++l_tcp; + } + } + + l_tcp = l_cp->tcps; + for + (i=0;ith;++i) + { + for + (j=0;jtw;++j) + { + l_rates = l_tcp->rates; + if + (*l_rates) + { + *l_rates -= l_sot_remove; + if + (*l_rates < 30) + { + *l_rates = 30; + } + } + ++l_rates; + l_last_res = l_tcp->numlayers - 1; + for + (k = 1; k < l_last_res; ++k) + { + if + (*l_rates) + { + *l_rates -= l_sot_remove; + if + (*l_rates < *(l_rates - 1) + 10) + { + *l_rates = (*(l_rates - 1)) + 20; + } + } + ++l_rates; + } + if + (*l_rates) + { + *l_rates -= (l_sot_remove + 2.f); + if + (*l_rates < *(l_rates - 1) + 10) + { + *l_rates = (*(l_rates - 1)) + 20; + } + } + ++l_tcp; + } + } + + l_img_comp = l_image->comps; + l_tile_size = 0; + for + (i=0;inumcomps;++i) + { + l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx) + * + uint_ceildiv(l_cp->tdy,l_img_comp->dy) + * + l_img_comp->prec + ); + ++l_img_comp; + } + + l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */ + l_tile_size += j2k_get_specific_header_sizes(p_j2k); + + p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size; + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = (OPJ_BYTE *) my_opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size); + if + (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) + { + return false; + } + if + (l_cp->m_specific_param.m_enc.m_cinema) + { + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts); + if + (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) + { + return false; + } + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer; + } + return true; +} + +/** + * Reads a EOC marker (End Of Codestream) + * + * @param p_header_data the data contained in the SOD box. + * @param p_j2k the jpeg2000 codec. + * @param p_header_size the size of the data contained in the SOD marker. + * @param p_manager the user event manager. +*/ +bool j2k_read_eoc ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 i; + opj_tcd_t * l_tcd = 00; + OPJ_UINT32 l_nb_tiles; + opj_tcp_t * l_tcp = 00; + bool l_success; + + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + + l_tcd = tcd_create(true); + if + (l_tcd == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return false; + } + + + + for + (i = 0; i < l_nb_tiles; ++i) + { + if + (l_tcp->m_data) + { + if + (! tcd_init_decode_tile(l_tcd, i)) + { + tcd_destroy(l_tcd); + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return false; + } + l_success = tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_info); + /* cleanup */ + if + (! l_success) + { + p_j2k->m_specific_param.m_decoder.m_state |= J2K_DEC_STATE_ERR; break; } } - tcd_free_decode(tcd); - tcd_destroy(tcd); + j2k_tcp_destroy(l_tcp); + ++l_tcp; } - /* if packets should not be decoded */ - else { - for (i = 0; i < j2k->cp->tileno_size; i++) { - tileno = j2k->cp->tileno[i]; - opj_free(j2k->tile_data[tileno]); - j2k->tile_data[tileno] = NULL; - } - } - if (j2k->state & J2K_STATE_ERR) - j2k->state = J2K_STATE_MT + J2K_STATE_ERR; - else - j2k->state = J2K_STATE_MT; + tcd_destroy(l_tcd); + return true; } -typedef struct opj_dec_mstabent { - /** marker value */ - int id; - /** value of the state when the marker can appear */ - int states; - /** action linked to the marker */ - void (*handler) (opj_j2k_t *j2k); -} opj_dec_mstabent_t; +/** + * Writes the image components. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_image_components( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 compno; + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + for + (compno = 1; compno < p_j2k->m_image->numcomps; ++compno) + { + if + (! j2k_write_coc(p_j2k,compno,p_stream, p_manager)) + { + return false; + } + if + (! j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) + { + return false; + } + } + return true; +} -opj_dec_mstabent_t j2k_dec_mstab[] = { - {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc}, - {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot}, - {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod}, - {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc}, - {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz}, - {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod}, - {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc}, - {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn}, - {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd}, - {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc}, - {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc}, - {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm}, - {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm}, - {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt}, - {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm}, - {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt}, - {J2K_MS_SOP, 0, 0}, - {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg}, - {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com}, +/** + * Writes regions of interests. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_regions( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 compno; + const opj_tccp_t *l_tccp = 00; + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tccp = p_j2k->m_cp.tcps->tccps; + for + (compno = 0; compno < p_j2k->m_image->numcomps; ++compno) + { + if + (l_tccp->roishift) + { + if + (! j2k_write_rgn(p_j2k,0,compno,p_stream,p_manager)) + { + return false; + } + } + ++l_tccp; + } + return true; +} +/** + * Writes the updated tlm. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_write_updated_tlm( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 l_tlm_size; + OPJ_SIZE_T l_tlm_position, l_current_position; + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts; + l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start; + l_current_position = opj_stream_tell(p_stream); + + if + (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) + { + return false; + } + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) + { + return false; + } + if + (! opj_stream_seek(p_stream,l_current_position,p_manager)) + { + return false; + } + return true; +} + +/** + * Ends the encoding, i.e. frees memory. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_end_encoding( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + + if + (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) + { + opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0; + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0; + } + if + (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) + { + opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0; + } + p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0; + + return true; +} + +/** + * Gets the offset of the header. + * + * @param p_stream the stream to write data to. + * @param p_j2k J2K codec. + * @param p_manager the user event manager. +*/ +bool j2k_get_end_header( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + p_j2k->cstr_info->main_head_end = opj_stream_tell(p_stream); + return true; +} + + + + +/** + * Reads an unknown marker + * + * @param p_stream the stream object to read from. + * @param p_j2k the jpeg2000 codec. + * @param p_manager the user event manager. + * + * @return true if the marker could be deduced. +*/ +bool j2k_read_unk ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_BYTE l_data [2]; + OPJ_UINT32 l_unknown_size; + // preconditions + assert(p_j2k != 00); + assert(p_manager != 00); + assert(p_stream != 00); + + opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); + #ifdef USE_JPWL - {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc}, - {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb}, - {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd}, - {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red}, -#endif /* USE_JPWL */ -#ifdef USE_JPSEC - {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec}, - {J2K_MS_INSEC, 0, j2k_read_insec}, -#endif /* USE_JPSEC */ - - {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk} -}; - -static void j2k_read_unk(opj_j2k_t *j2k) { - opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n"); - -#ifdef USE_JPWL - if (j2k->cp->correct) { - int m = 0, id, i; - int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id; - cio_seek(j2k->cio, cio_tell(j2k->cio) - 2); - id = cio_read(j2k->cio, 2); - opj_event_msg(j2k->cinfo, EVT_ERROR, + if (p_j2k->m_cp->correct) { + OPJ_INT32 m = 0, id, i; + OPJ_INT32 min_id = 0, min_dist = 17, cur_dist = 0, tmp_id; + p_stream_seek(p_j2k->p_stream, p_stream_tell(p_j2k->p_stream) - 2); + id = p_stream_read(p_j2k->p_stream, 2); + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: really don't know this marker %x\n", id); if (!JPWL_ASSUME) { - opj_event_msg(j2k->cinfo, EVT_ERROR, + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "- possible synch loss due to uncorrectable codestream errors => giving up\n"); return; } @@ -1688,358 +6821,940 @@ static void j2k_read_unk(opj_j2k_t *j2k) { /* do we substitute the marker? */ if (min_dist < JPWL_MAXIMUM_HAMMING) { - opj_event_msg(j2k->cinfo, EVT_ERROR, + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "- marker %x is at distance %d from the read %x\n", min_id, min_dist, id); - opj_event_msg(j2k->cinfo, EVT_ERROR, + opj_event_msg(p_j2k->cinfo, EVT_ERROR, "- trying to substitute in place and crossing fingers!\n"); - cio_seek(j2k->cio, cio_tell(j2k->cio) - 2); - cio_write(j2k->cio, min_id, 2); + p_stream_seek(p_j2k->p_stream, p_stream_tell(p_j2k->p_stream) - 2); + p_stream_write(p_j2k->p_stream, min_id, 2); /* rewind */ - cio_seek(j2k->cio, cio_tell(j2k->cio) - 2); + p_stream_seek(p_j2k->p_stream, p_stream_tell(p_j2k->p_stream) - 2); } }; #endif /* USE_JPWL */ + if + (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n"); + return false; + } + opj_read_bytes(l_data,&l_unknown_size,2); + if + (l_unknown_size < 2) + { + return false; + } + l_unknown_size-=2; + if + (opj_stream_skip(p_stream,l_unknown_size,p_manager) != l_unknown_size) + { + return false; + } + return true; } /** -Read the lookup table containing all the marker, status and action -@param id Marker value + * Reads the lookup table containing all the marker, status and action, and returns the handler associated + * with the marker value. + * @param p_id Marker value to look up + * + * @return the handler associated with the id. */ -static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) { - opj_dec_mstabent_t *e; - for (e = j2k_dec_mstab; e->id != 0; e++) { - if (e->id == id) { +const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id) +{ + const opj_dec_memory_marker_handler_t *e; + for + (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) + { + if + (e->id == p_id) + { break; } } return e; } +/** + * Destroys a tile coding parameter structure. + * + * @param p_tcp the tile coding parameter to destroy. + */ +void j2k_tcp_destroy (opj_tcp_t *p_tcp) +{ + if + (p_tcp == 00) + { + return; + } + if + (p_tcp->ppt_buffer != 00) + { + opj_free(p_tcp->ppt_buffer); + p_tcp->ppt_buffer = 00; + } + if + (p_tcp->tccps != 00) + { + opj_free(p_tcp->tccps); + p_tcp->tccps = 00; + } + if + (p_tcp->m_mct_coding_matrix != 00) + { + opj_free(p_tcp->m_mct_coding_matrix); + p_tcp->m_mct_coding_matrix = 00; + } + if + (p_tcp->m_mct_decoding_matrix != 00) + { + opj_free(p_tcp->m_mct_decoding_matrix); + p_tcp->m_mct_decoding_matrix = 00; + } + if + (p_tcp->m_mcc_records) + { + opj_free(p_tcp->m_mcc_records); + p_tcp->m_mcc_records = 00; + p_tcp->m_nb_max_mcc_records = 0; + p_tcp->m_nb_mcc_records = 0; + } + if + (p_tcp->m_mct_records) + { + opj_mct_data_t * l_mct_data = p_tcp->m_mct_records; + OPJ_UINT32 i; + for + (i=0;im_nb_mct_records;++i) + { + if + (l_mct_data->m_data) + { + opj_free(l_mct_data->m_data); + l_mct_data->m_data = 00; + } + ++l_mct_data; + } + opj_free(p_tcp->m_mct_records); + p_tcp->m_mct_records = 00; + } + + if + (p_tcp->mct_norms != 00) + { + opj_free(p_tcp->mct_norms); + p_tcp->mct_norms = 00; + } + if + (p_tcp->m_data) + { + opj_free(p_tcp->m_data); + p_tcp->m_data = 00; + } +} + +/** + * Destroys a coding parameter structure. + * + * @param p_cp the coding parameter to destroy. + */ +void j2k_cp_destroy (opj_cp_t *p_cp) +{ + OPJ_UINT32 l_nb_tiles; + opj_tcp_t * l_current_tile = 00; + OPJ_UINT32 i; + + if + (p_cp == 00) + { + return; + } + if + (p_cp->tcps != 00) + { + l_current_tile = p_cp->tcps; + l_nb_tiles = p_cp->th * p_cp->tw; + + for + (i = 0; i < l_nb_tiles; ++i) + { + j2k_tcp_destroy(l_current_tile); + ++l_current_tile; + } + opj_free(p_cp->tcps); + p_cp->tcps = 00; + } + if + (p_cp->ppm_buffer != 00) + { + opj_free(p_cp->ppm_buffer); + p_cp->ppm_buffer = 00; + } + if + (p_cp->comment != 00) + { + opj_free(p_cp->comment); + p_cp->comment = 00; + } + if + (! p_cp->m_is_decoder) + { + if + (p_cp->m_specific_param.m_enc.m_matrice) + { + opj_free(p_cp->m_specific_param.m_enc.m_matrice); + p_cp->m_specific_param.m_enc.m_matrice = 00; + } + } +} + /* ----------------------------------------------------------------------- */ /* J2K / JPT decoder interface */ /* ----------------------------------------------------------------------- */ +/** + * Creates a J2K decompression structure. + * + * @return a handle to a J2K decompressor if successful, NULL otherwise. +*/ +opj_j2k_t* j2k_create_decompress() +{ + opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t)); + if + (!l_j2k) + { + return 00; + } + memset(l_j2k,0,sizeof(opj_j2k_t)); + l_j2k->m_is_decoder = 1; + l_j2k->m_cp.m_is_decoder = 1; + l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_malloc(sizeof(opj_tcp_t)); + if + (!l_j2k->m_specific_param.m_decoder.m_default_tcp) + { + opj_free(l_j2k); + return 00; + } + memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_t)); + + l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE); + if + (! l_j2k->m_specific_param.m_decoder.m_header_data) + { + j2k_destroy(l_j2k); + return 00; + } + l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE; -opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) { - opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); - if(!j2k) - return NULL; - - j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t)); - if(!j2k->default_tcp) { - opj_free(j2k); - return NULL; + // validation list creation + l_j2k->m_validation_list = opj_procedure_list_create(); + if + (! l_j2k->m_validation_list) + { + j2k_destroy(l_j2k); + return 00; } - j2k->cinfo = cinfo; - j2k->tile_data = NULL; - - return j2k; + // execution list creation + l_j2k->m_procedure_list = opj_procedure_list_create(); + if + (! l_j2k->m_procedure_list) + { + j2k_destroy(l_j2k); + return 00; + } + return l_j2k; } -void j2k_destroy_decompress(opj_j2k_t *j2k) { - int i = 0; +opj_j2k_t* j2k_create_compress() +{ + opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t)); + if + (!l_j2k) + { + return 00; + } + memset(l_j2k,0,sizeof(opj_j2k_t)); + l_j2k->m_is_decoder = 0; + l_j2k->m_cp.m_is_decoder = 0; + + l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE); + if + (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + j2k_destroy(l_j2k); + return 00; + } + l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE; - if(j2k->tile_len != NULL) { - opj_free(j2k->tile_len); + // validation list creation + l_j2k->m_validation_list = opj_procedure_list_create(); + if + (! l_j2k->m_validation_list) + { + j2k_destroy(l_j2k); + return 00; } - if(j2k->tile_data != NULL) { - opj_free(j2k->tile_data); + + // execution list creation + l_j2k->m_procedure_list = opj_procedure_list_create(); + if + (! l_j2k->m_procedure_list) + { + j2k_destroy(l_j2k); + return 00; } - if(j2k->default_tcp != NULL) { - opj_tcp_t *default_tcp = j2k->default_tcp; - if(default_tcp->ppt_data_first != NULL) { - opj_free(default_tcp->ppt_data_first); + return l_j2k; +} + + +/** + * Destroys a jpeg2000 codec. + * + * @param p_j2k the jpeg20000 structure to destroy. + */ +void j2k_destroy (opj_j2k_t *p_j2k) +{ + if + (p_j2k == 00) + { + return; + } + + if + (p_j2k->m_is_decoder) + { + if + (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) + { + j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp); + opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp); + p_j2k->m_specific_param.m_decoder.m_default_tcp = 00; } - if(j2k->default_tcp->tccps != NULL) { - opj_free(j2k->default_tcp->tccps); + if + (p_j2k->m_specific_param.m_decoder.m_header_data != 00) + { + opj_free(p_j2k->m_specific_param.m_decoder.m_header_data); + p_j2k->m_specific_param.m_decoder.m_header_data = 00; + p_j2k->m_specific_param.m_decoder.m_header_data_size = 0; } - opj_free(j2k->default_tcp); + } - if(j2k->cp != NULL) { - opj_cp_t *cp = j2k->cp; - if(cp->tcps != NULL) { - for(i = 0; i < cp->tw * cp->th; i++) { - if(cp->tcps[i].ppt_data_first != NULL) { - opj_free(cp->tcps[i].ppt_data_first); - } - if(cp->tcps[i].tccps != NULL) { - opj_free(cp->tcps[i].tccps); + else + { + if + (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) + { + opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data); + p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00; + } + if + (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) + { + opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer); + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00; + p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00; + } + if + (p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00; + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + } + } + tcd_destroy(p_j2k->m_tcd); + + j2k_cp_destroy(&(p_j2k->m_cp)); + memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t)); + + opj_procedure_list_destroy(p_j2k->m_procedure_list); + p_j2k->m_procedure_list = 00; + + opj_procedure_list_destroy(p_j2k->m_validation_list); + p_j2k->m_procedure_list = 00; + + opj_free(p_j2k); +} + +/** + * Starts a compression scheme, i.e. validates the codec parameters, writes the header. + * + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream object. + * @param p_manager the user event manager. + * + * @return true if the codec is valid. + */ +bool j2k_start_compress( + opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_image_t * p_image, + opj_event_mgr_t * p_manager) +{ + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + p_j2k->m_image = p_image; + + + /* customization of the validation */ + j2k_setup_encoding_validation (p_j2k); + + /* validation of the parameters codec */ + if + (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) + { + return false; + } + + /* customization of the encoding */ + j2k_setup_header_writting(p_j2k); + + /* write header */ + if + (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) + { + return false; + } + return true; +} +/** + * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures. + */ +void j2k_setup_header_reading (opj_j2k_t *p_j2k) +{ + // preconditions + assert(p_j2k != 00); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_read_header_procedure); + + /* DEVELOPER CORNER, add your custom procedures */ + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_copy_default_tcp_and_create_tcd); + +} + +/** + * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures. + */ +void j2k_setup_decoding (opj_j2k_t *p_j2k) +{ + // preconditions + assert(p_j2k != 00); + + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_decode_tiles); + /* DEVELOPER CORNER, add your custom procedures */ + +} + +/** + * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures. + */ +void j2k_setup_header_writting (opj_j2k_t *p_j2k) +{ + // preconditions + assert(p_j2k != 00); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_init_info ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_soc ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_siz ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_cod ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_qcd ); + + + if + (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) + { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_image_components ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_tlm ); + if + (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) + { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_poc ); + } + } + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_regions); + + if + (p_j2k->m_cp.comment != 00) + { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_com); + } + + /* DEVELOPER CORNER, insert your custom procedures */ + if + (p_j2k->m_cp.rsiz & MCT) + { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_mct_data_group ); + } + /* End of Developer Corner */ + + if + (p_j2k->cstr_info) + { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_get_end_header ); + } + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_create_tcd); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_update_rates); +} + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +void j2k_setup_end_compress (opj_j2k_t *p_j2k) +{ + // preconditions + assert(p_j2k != 00); + + /* DEVELOPER CORNER, insert your custom procedures */ + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_eoc ); + if + (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) + { + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_updated_tlm); + } + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_write_epc ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_end_encoding ); + opj_procedure_list_add_procedure(p_j2k->m_procedure_list,j2k_destroy_header_memory); +} + + + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +void j2k_setup_encoding_validation (opj_j2k_t *p_j2k) +{ + // preconditions + assert(p_j2k != 00); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, j2k_build_encoder); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, j2k_encoding_validation); + + + /* DEVELOPER CORNER, add your custom validation procedure */ + opj_procedure_list_add_procedure(p_j2k->m_validation_list, j2k_mct_validation); +} + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +void j2k_setup_decoding_validation (opj_j2k_t *p_j2k) +{ + // preconditions + assert(p_j2k != 00); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, j2k_build_decoder); + opj_procedure_list_add_procedure(p_j2k->m_validation_list, j2k_decoding_validation); + /* DEVELOPER CORNER, add your custom validation procedure */ + +} + + +/** + * Excutes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param p_j2k the jpeg2000 codec to execute the procedures on. + * @param p_stream the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +bool j2k_exec ( + opj_j2k_t * p_j2k, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + bool (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00; + bool l_result = true; + OPJ_UINT32 l_nb_proc, i; + + // preconditions + assert(p_procedure_list != 00); + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); + l_procedure = (bool (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + for + (i=0;im_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); + + /* POINTER validation */ + /* make sure a p_j2k codec is present */ + l_is_valid &= (p_j2k->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (p_j2k->m_validation_list != 00); + + if + ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) + { + opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + return false; + } + if + ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) + { + opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n"); + return false; + } + + /* PARAMETER VALIDATION */ + return l_is_valid; +} + +/** + * The default decoding validation procedure without any extension. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool j2k_decoding_validation ( + opj_j2k_t *p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + bool l_is_valid = true; + + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + /* STATE checking */ + /* make sure the state is at 0 */ + l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE); + + /* POINTER validation */ + /* make sure a p_j2k codec is present */ + /* make sure a procedure list is present */ + l_is_valid &= (p_j2k->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (p_j2k->m_validation_list != 00); + + /* PARAMETER VALIDATION */ + return l_is_valid; +} + +/** + * The mct encoding validation procedure. + * + * @param p_j2k the jpeg2000 codec to validate. + * @param p_stream the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool j2k_mct_validation ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + bool l_is_valid = true; + OPJ_UINT32 i,j; + + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + if + ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) + { + OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + opj_tcp_t * l_tcp = p_j2k->m_cp.tcps; + for + (i=0;imct == 2) + { + opj_tccp_t * l_tccp = l_tcp->tccps; + l_is_valid &= (l_tcp->m_mct_coding_matrix != 00); + for + (j=0;jm_image->numcomps;++j) + { + l_is_valid &= ! (l_tccp->qmfbid & 1); + ++l_tccp; } } - opj_free(cp->tcps); + ++l_tcp; } - if(cp->ppm_data_first != NULL) { - opj_free(cp->ppm_data_first); - } - if(cp->tileno != NULL) { - opj_free(cp->tileno); - } - if(cp->comment != NULL) { - opj_free(cp->comment); - } - - opj_free(cp); } - opj_free(j2k); + return l_is_valid; } -void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) { - if(j2k && parameters) { +/** + * Builds the cp decoder parameters to use to decode tile. + */ +bool j2k_build_decoder ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + // add here initialization of cp + // copy paste of setup_decoder + return true; +} + +/** + * Builds the cp encoder parameters to use to encode tile. + */ +bool j2k_build_encoder ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + // add here initialization of cp + // copy paste of setup_encoder + return true; +} + +bool j2k_copy_default_tcp_and_create_tcd + ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + opj_tcp_t * l_tcp = 00; + opj_tcp_t * l_default_tcp = 00; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 i,j; + opj_tccp_t *l_current_tccp = 00; + OPJ_UINT32 l_tccp_size; + OPJ_UINT32 l_mct_size; + opj_image_t * l_image; + OPJ_UINT32 l_mcc_records_size,l_mct_records_size; + opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec; + opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec; + OPJ_UINT32 l_offset; + + // preconditions in debug + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_image = p_j2k->m_image; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + l_tcp = p_j2k->m_cp.tcps; + l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t); + l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp; + l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32); + for + (i=0;itccps; + memcpy(l_tcp,l_default_tcp, sizeof(opj_tcp_t)); + l_tcp->ppt = 0; + l_tcp->ppt_data = 00; + l_tcp->tccps = l_current_tccp; + if + (l_default_tcp->m_mct_decoding_matrix) + { + l_tcp->m_mct_decoding_matrix = opj_malloc(l_mct_size); + if + (! l_tcp->m_mct_decoding_matrix ) + { + return false; + } + memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size); + } + l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t); + l_tcp->m_mct_records = opj_malloc(l_mct_records_size); + if + (! l_tcp->m_mct_records) + { + return false; + } + memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size); + l_src_mct_rec = l_default_tcp->m_mct_records; + l_dest_mct_rec = l_tcp->m_mct_records; + for + (j=0;jm_nb_mct_records;++j) + { + if + (l_src_mct_rec->m_data) + { + l_dest_mct_rec->m_data = opj_malloc(l_src_mct_rec->m_data_size); + if + (! l_dest_mct_rec->m_data) + { + return false; + } + memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size); + } + ++l_src_mct_rec; + ++l_dest_mct_rec; + } + l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t); + l_tcp->m_mcc_records = opj_malloc(l_mcc_records_size); + if + (! l_tcp->m_mcc_records) + { + return false; + } + memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size); + l_src_mcc_rec = l_default_tcp->m_mcc_records; + l_dest_mcc_rec = l_tcp->m_mcc_records; + for + (j=0;jm_nb_max_mcc_records;++j) + { + if + (l_src_mcc_rec->m_decorrelation_array) + { + l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records; + l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset; + } + if + (l_src_mcc_rec->m_offset_array) + { + l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records; + l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset; + } + ++l_src_mcc_rec; + ++l_dest_mcc_rec; + } + memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size); + ++l_tcp; + } + p_j2k->m_tcd = tcd_create(true); + if + (! p_j2k->m_tcd ) + { + return false; + } + if + (! tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp))) + { + tcd_destroy(p_j2k->m_tcd); + p_j2k->m_tcd = 00; + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return false; + } + return true; +} + +/** + * Destroys the memory associated with the decoding of headers. + */ +bool j2k_destroy_header_memory ( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + // preconditions in debug + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + if + (p_j2k->m_specific_param.m_encoder.m_header_tile_data) + { + opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data); + p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0; + } + p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0; + return true; +} + +/** + * Sets up the decoder decoding parameters using user parameters. + * Decoding parameters are stored in p_j2k->m_cp. + * + * @param p_j2k J2K codec + * @param p_parameters decompression parameters + * @deprecated +*/ +void j2k_setup_decoder( + opj_j2k_t *p_j2k, + opj_dparameters_t *p_parameters + ) +{ + if + (p_j2k && p_parameters) + { /* create and initialize the coding parameters structure */ - opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t)); - cp->reduce = parameters->cp_reduce; - cp->layer = parameters->cp_layer; - cp->limit_decoding = parameters->cp_limit_decoding; + p_j2k->m_cp.m_specific_param.m_dec.m_reduce = p_parameters->cp_reduce; + p_j2k->m_cp.m_specific_param.m_dec.m_layer = p_parameters->cp_layer; + p_j2k->m_specific_param.m_decoder.m_discard_tiles = p_parameters->m_use_restrict_decode; + if + (p_parameters->m_use_restrict_decode) + { + p_j2k->m_specific_param.m_decoder.m_start_tile_x = p_parameters->m_decode_start_x; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = p_parameters->m_decode_start_y; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = p_parameters->m_decode_end_x; + p_j2k->m_specific_param.m_decoder.m_end_tile_y = p_parameters->m_decode_end_y; + } #ifdef USE_JPWL cp->correct = parameters->jpwl_correct; cp->exp_comps = parameters->jpwl_exp_comps; cp->max_tiles = parameters->jpwl_max_tiles; #endif /* USE_JPWL */ - - - /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */ - j2k->cp = cp; } } -opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) { - opj_image_t *image = NULL; - - opj_common_ptr cinfo = j2k->cinfo; - - j2k->cio = cio; - j2k->cstr_info = cstr_info; - if (cstr_info) - memset(cstr_info, 0, sizeof(opj_codestream_info_t)); - - /* create an empty image */ - image = opj_image_create0(); - j2k->image = image; - - j2k->state = J2K_STATE_MHSOC; - - for (;;) { - opj_dec_mstabent_t *e; - int id = cio_read(cio, 2); - -#ifdef USE_JPWL - /* we try to honor JPWL correction power */ - if (j2k->cp->correct) { - - int orig_pos = cio_tell(cio); - bool status; - - /* call the corrector */ - status = jpwl_correct(j2k); - - /* go back to where you were */ - cio_seek(cio, orig_pos - 2); - - /* re-read the marker */ - id = cio_read(cio, 2); - - /* check whether it begins with ff */ - if (id >> 8 != 0xff) { - opj_event_msg(cinfo, EVT_ERROR, - "JPWL: possible bad marker %x at %d\n", - id, cio_tell(cio) - 2); - if (!JPWL_ASSUME) { - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n"); - return 0; - } - /* we try to correct */ - id = id | 0xff00; - cio_seek(cio, cio_tell(cio) - 2); - cio_write(cio, id, 2); - opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n" - "- setting marker to %x\n", - id); - } - - } -#endif /* USE_JPWL */ - - if (id >> 8 != 0xff) { - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup(id); - // Check if the marker is known - if (!(j2k->state & e->states)) { - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id); - return 0; - } - // Check if the decoding is limited to the main header - if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) { - opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n"); - return image; - } - - if (e->handler) { - (*e->handler)(j2k); - } - if (j2k->state & J2K_STATE_ERR) - return NULL; - - if (j2k->state == J2K_STATE_MT) { - break; - } - if (j2k->state == J2K_STATE_NEOC) { - break; - } - } - if (j2k->state == J2K_STATE_NEOC) { - j2k_read_eoc(j2k); - } - - if (j2k->state != J2K_STATE_MT) { - opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n"); - } - - return image; -} - -/* -* Read a JPT-stream and decode file -* -*/ -opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) { - opj_image_t *image = NULL; - opj_jpt_msg_header_t header; - int position; - - opj_common_ptr cinfo = j2k->cinfo; - - j2k->cio = cio; - - /* create an empty image */ - image = opj_image_create0(); - j2k->image = image; - - j2k->state = J2K_STATE_MHSOC; - - /* Initialize the header */ - jpt_init_msg_header(&header); - /* Read the first header of the message */ - jpt_read_msg_header(cinfo, cio, &header); - - position = cio_tell(cio); - if (header.Class_Id != 6) { /* 6 : Main header data-bin message */ - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id); - return 0; - } - - for (;;) { - opj_dec_mstabent_t *e = NULL; - int id; - - if (!cio_numbytesleft(cio)) { - j2k_read_eoc(j2k); - return image; - } - /* data-bin read -> need to read a new header */ - if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) { - jpt_read_msg_header(cinfo, cio, &header); - position = cio_tell(cio); - if (header.Class_Id != 4) { /* 4 : Tile data-bin message */ - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n"); - return 0; - } - } - - id = cio_read(cio, 2); - if (id >> 8 != 0xff) { - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id); - return 0; - } - e = j2k_dec_mstab_lookup(id); - if (!(j2k->state & e->states)) { - opj_image_destroy(image); - opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id); - return 0; - } - if (e->handler) { - (*e->handler)(j2k); - } - if (j2k->state == J2K_STATE_MT) { - break; - } - if (j2k->state == J2K_STATE_NEOC) { - break; - } - } - if (j2k->state == J2K_STATE_NEOC) { - j2k_read_eoc(j2k); - } - - if (j2k->state != J2K_STATE_MT) { - opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n"); - } - - return image; -} - -/* ----------------------------------------------------------------------- */ -/* J2K encoder interface */ -/* ----------------------------------------------------------------------- */ - -opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) { - opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t)); - if(j2k) { - j2k->cinfo = cinfo; - } - return j2k; -} - -void j2k_destroy_compress(opj_j2k_t *j2k) { - int tileno; - - if(!j2k) return; - if(j2k->cp != NULL) { - opj_cp_t *cp = j2k->cp; - - if(cp->comment) { - opj_free(cp->comment); - } - if(cp->matrice) { - opj_free(cp->matrice); - } - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - opj_free(cp->tcps[tileno].tccps); - } - opj_free(cp->tcps); - opj_free(cp); - } - - opj_free(j2k); -} - -void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) { - int i, j, tileno, numpocs_tile; - opj_cp_t *cp = NULL; - - if(!j2k || !parameters || ! image) { +void j2k_setup_encoder(opj_j2k_t *p_j2k, opj_cparameters_t *parameters, opj_image_t *image, struct opj_event_mgr * p_manager) { + OPJ_UINT32 i, j, tileno, numpocs_tile; + opj_cp_t *cp = 00; + bool l_res; + if(!p_j2k || !parameters || ! image) { return; } - - /* create and initialize the coding parameters structure */ - cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t)); - + /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */ - j2k->cp = cp; - + cp = &(p_j2k->m_cp); + /* set default values for cp */ cp->tw = 1; cp->th = 1; @@ -2047,18 +7762,20 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ /* copy user encoding parameters */ - cp->cinema = parameters->cp_cinema; - cp->max_comp_size = parameters->max_comp_size; + cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema; + cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size; cp->rsiz = parameters->cp_rsiz; - cp->disto_alloc = parameters->cp_disto_alloc; - cp->fixed_alloc = parameters->cp_fixed_alloc; - cp->fixed_quality = parameters->cp_fixed_quality; + cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc; + cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc; + cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality; /* mod fixed_quality */ - if(parameters->cp_matrice) { - size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int); - cp->matrice = (int *) opj_malloc(array_size); - memcpy(cp->matrice, parameters->cp_matrice, array_size); + if + (parameters->cp_matrice) + { + size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32); + cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size); + memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size); } /* tiles */ @@ -2089,24 +7806,20 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ cp->tdy = image->y1 - cp->ty0; } - if(parameters->tp_on){ - cp->tp_flag = parameters->tp_flag; - cp->tp_on = 1; + if + (parameters->tp_on) + { + cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag; + cp->m_specific_param.m_enc.m_tp_on = 1; } - cp->img_size = 0; - for(i=0;inumcomps ;i++){ - cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec); - } - - #ifdef USE_JPWL /* calculate JPWL encoding parameters */ if (parameters->jpwl_epc_on) { - int i; + OPJ_INT32 i; /* set JPWL on */ cp->epc_on = true; @@ -2160,18 +7873,24 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ /* initialize the mutiple tiles */ /* ---------------------------- */ cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t)); - + if + (parameters->numpocs) + { + /* initialisation of POC */ + l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager); + // TODO + } for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { opj_tcp_t *tcp = &cp->tcps[tileno]; tcp->numlayers = parameters->tcp_numlayers; for (j = 0; j < tcp->numlayers; j++) { - if(cp->cinema){ - if (cp->fixed_quality) { + if(cp->m_specific_param.m_enc.m_cinema){ + if (cp->m_specific_param.m_enc.m_fixed_quality) { tcp->distoratio[j] = parameters->tcp_distoratio[j]; } tcp->rates[j] = parameters->tcp_rates[j]; }else{ - if (cp->fixed_quality) { /* add fixed_quality */ + if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */ tcp->distoratio[j] = parameters->tcp_distoratio[j]; } else { tcp->rates[j] = parameters->tcp_rates[j]; @@ -2182,12 +7901,17 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ tcp->prg = parameters->prog_order; tcp->mct = parameters->tcp_mct; + + numpocs_tile = 0; tcp->POC = 0; - if (parameters->numpocs) { + if + (parameters->numpocs) + { /* initialisation of POC */ tcp->POC = 1; - for (i = 0; i < parameters->numpocs; i++) { + // TODO + for (i = 0; i < (unsigned int) parameters->numpocs; i++) { if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) { opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile]; tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0; @@ -2206,6 +7930,44 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ } tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t)); + if + (parameters->mct_data) + { + OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32); + OPJ_FLOAT32 * lTmpBuf = opj_malloc(lMctSize); + OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize); + tcp->mct = 2; + tcp->m_mct_coding_matrix = opj_malloc(lMctSize); + memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize); + memcpy(lTmpBuf,parameters->mct_data,lMctSize); + tcp->m_mct_decoding_matrix = opj_malloc(lMctSize); + assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps)); + tcp->mct_norms = opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64)); + opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix); + opj_free(lTmpBuf); + for + (i = 0; i < image->numcomps; i++) + { + opj_tccp_t *tccp = &tcp->tccps[i]; + tccp->m_dc_level_shift = l_dc_shift[i]; + } + j2k_setup_mct_encoding(tcp,image); + } + else + { + for + (i = 0; i < image->numcomps; i++) + { + opj_tccp_t *tccp = &tcp->tccps[i]; + opj_image_comp_t * l_comp = &(image->comps[i]); + if + (! l_comp->sgnd) + { + tccp->m_dc_level_shift = 1 << (l_comp->prec - 1); + } + } + } + for (i = 0; i < image->numcomps; i++) { opj_tccp_t *tccp = &tcp->tccps[i]; @@ -2278,227 +8040,856 @@ void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_ } } } - + dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec); } } + if + (parameters->mct_data) + { + opj_free(parameters->mct_data); + parameters->mct_data = 00; + } } -bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { - int tileno, compno; - opj_cp_t *cp = NULL; +bool j2k_write_first_tile_part ( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 compno; + OPJ_UINT32 l_nb_bytes_written = 0; + OPJ_UINT32 l_current_nb_bytes_written; + OPJ_BYTE * l_begin_data = 00; - opj_tcd_t *tcd = NULL; /* TCD component */ - - j2k->cio = cio; - j2k->image = image; - - cp = j2k->cp; - - /* j2k_dump_cp(stdout, image, cp); */ + opj_tcp_t *l_tcp = 00; + opj_tcd_t * l_tcd = 00; + opj_cp_t * l_cp = 00; + + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + l_tcd->cur_pino = 0; + /*Get number of tile parts*/ + + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; /* INDEX >> */ - j2k->cstr_info = cstr_info; - if (cstr_info) { - int compno; - cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t)); - cstr_info->image_w = image->x1 - image->x0; - cstr_info->image_h = image->y1 - image->y0; - cstr_info->prog = (&cp->tcps[0])->prg; - cstr_info->tw = cp->tw; - cstr_info->th = cp->th; - cstr_info->tile_x = cp->tdx; /* new version parser */ - cstr_info->tile_y = cp->tdy; /* new version parser */ - cstr_info->tile_Ox = cp->tx0; /* new version parser */ - cstr_info->tile_Oy = cp->ty0; /* new version parser */ - cstr_info->numcomps = image->numcomps; - cstr_info->numlayers = (&cp->tcps[0])->numlayers; - cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int)); - for (compno=0; compno < image->numcomps; compno++) { - cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1; - } - cstr_info->D_max = 0.0; /* ADD Marcela */ - cstr_info->main_head_start = cio_tell(cio); /* position of SOC */ - cstr_info->maxmarknum = 100; - cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t)); - cstr_info->marknum = 0; - } + /* << INDEX */ + l_current_nb_bytes_written = 0; + l_begin_data = p_data; + if + (! j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) + { + return false; + } + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; - j2k_write_soc(j2k); - j2k_write_siz(j2k); - j2k_write_cod(j2k); - j2k_write_qcd(j2k); - - if(cp->cinema){ - for (compno = 1; compno < image->numcomps; compno++) { - j2k_write_coc(j2k, compno); - j2k_write_qcc(j2k, compno); + if + (l_cp->m_specific_param.m_enc.m_cinema == 0) + { + for + (compno = 1; compno < p_j2k->m_image->numcomps; compno++) + { + l_current_nb_bytes_written = 0; + j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + + l_current_nb_bytes_written = 0; + j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + } + if + (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) + { + l_current_nb_bytes_written = 0; + j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager); + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; } } - - for (compno = 0; compno < image->numcomps; compno++) { - opj_tcp_t *tcp = &cp->tcps[0]; - if (tcp->tccps[compno].roishift) - j2k_write_rgn(j2k, compno, 0); + l_current_nb_bytes_written = 0; + if + (! j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) + { + return false; } - if (cp->comment != NULL) { - j2k_write_com(j2k); + l_nb_bytes_written += l_current_nb_bytes_written; + * p_data_written = l_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */ + if + (l_cp->m_specific_param.m_enc.m_cinema) + { + j2k_update_tlm(p_j2k,l_nb_bytes_written); } + return true; +} - j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k); - /* TLM Marker*/ - if(cp->cinema){ - j2k_write_tlm(j2k); - if (cp->cinema == CINEMA4K_24) { - j2k_write_poc(j2k); +bool j2k_write_all_tile_parts( + opj_j2k_t *p_j2k, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_total_data_size, + opj_stream_private_t *p_stream, + struct opj_event_mgr * p_manager + ) +{ + OPJ_UINT32 tilepartno=0; + OPJ_UINT32 l_nb_bytes_written = 0; + OPJ_UINT32 l_current_nb_bytes_written; + OPJ_UINT32 l_part_tile_size; + OPJ_UINT32 tot_num_tp; + OPJ_UINT32 pino; + + OPJ_BYTE * l_begin_data; + opj_tcp_t *l_tcp = 00; + opj_tcd_t * l_tcd = 00; + opj_cp_t * l_cp = 00; + + + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + + /*Get number of tile parts*/ + tot_num_tp = j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number); + for + (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) + { + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; + l_current_nb_bytes_written = 0; + l_part_tile_size = 0; + l_begin_data = p_data; + if + (! j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) + { + return false; } + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_nb_bytes_written; + + l_current_nb_bytes_written = 0; + if + (! j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) + { + return false; + } + p_data += l_current_nb_bytes_written; + l_nb_bytes_written += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ + + if + (l_cp->m_specific_param.m_enc.m_cinema) + { + j2k_update_tlm(p_j2k,l_part_tile_size); + } + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + } + for + (pino = 1; pino <= l_tcp->numpocs; ++pino) + { + l_tcd->cur_pino = pino; + /*Get number of tile parts*/ + tot_num_tp = j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number); + for + (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) + { + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno; + l_current_nb_bytes_written = 0; + l_part_tile_size = 0; + l_begin_data = p_data; + if + (! j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) + { + return false; + } + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; + + l_current_nb_bytes_written = 0; + if + (! j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) + { + return false; + } + l_nb_bytes_written += l_current_nb_bytes_written; + p_data += l_current_nb_bytes_written; + p_total_data_size -= l_current_nb_bytes_written; + l_part_tile_size += l_current_nb_bytes_written; + + /* Writing Psot in SOT marker */ + opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */ + + if + (l_cp->m_specific_param.m_enc.m_cinema) + { + j2k_update_tlm(p_j2k,l_part_tile_size); + } + ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number; + } + } + *p_data_written = l_nb_bytes_written; + return true; +} + + +bool j2k_pre_write_tile ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + if + (p_tile_index != p_j2k->m_current_tile_number) + { + opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." ); + return false; } - /* uncomment only for testing JPSEC marker writing */ - /* j2k_write_sec(j2k); */ + opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th); - /* INDEX >> */ - if(cstr_info) { - cstr_info->main_head_end = cio_tell(cio) - 1; + p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0; + p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts; + p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0; + /* initialisation before tile encoding */ + if + (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) + { + return false; } - /* << INDEX */ - /**** Main Header ENDS here ***/ + return true; +} - /* create the tile encoder */ - tcd = tcd_create(j2k->cinfo); +/** + * Writes a tile. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool j2k_write_tile ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + if + (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) + { + return false; + } + return j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager); +} - /* encode each tile */ - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - int pino; - int tilepartno=0; - /* UniPG>> */ - int acc_pack_num = 0; - /* <m_specific_param.m_encoder.m_encoded_tile_data); + l_tcd = p_j2k->m_tcd; + l_cp = &(p_j2k->m_cp); + l_tcp = l_cp->tcps + p_j2k->m_current_tile_number; + + l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size; + l_available_data = l_tile_size; + l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data; + if + (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) + { + opj_event_msg(p_manager, EVT_ERROR, "Size mismtach between tile data and sent data." ); + return false; + } + + l_nb_bytes_written = 0; + if + (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) + { + return false; + } + l_current_data += l_nb_bytes_written; + l_available_data -= l_nb_bytes_written; - opj_tcp_t *tcp = &cp->tcps[tileno]; - opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th); + l_nb_bytes_written = 0; + if + (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) + { + return false; + } + + l_available_data -= l_nb_bytes_written; + l_nb_bytes_written = l_tile_size - l_available_data; + + if + (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,l_nb_bytes_written,p_manager) != l_nb_bytes_written) + { + return false; + } + ++p_j2k->m_current_tile_number; + return true; +} - j2k->curtileno = tileno; - j2k->cur_tp_num = 0; - tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno]; - /* initialisation before tile encoding */ - if (tileno == 0) { - tcd_malloc_encode(tcd, image, cp, j2k->curtileno); - } else { - tcd_init_encode(tcd, image, cp, j2k->curtileno); - } +/** + * Reads a tile header. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool j2k_read_tile_header ( + opj_j2k_t * p_j2k, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_current_marker = J2K_MS_SOT; + OPJ_UINT32 l_marker_size; + const opj_dec_memory_marker_handler_t * l_marker_handler = 00; + opj_tcp_t * l_tcp = 00; + OPJ_UINT32 l_nb_tiles; - /* INDEX >> */ - if(cstr_info) { - cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction; - } - /* << INDEX */ - - for(pino = 0; pino <= tcp->numpocs; pino++) { - int tot_num_tp; - tcd->cur_pino=pino; - - /*Get number of tile parts*/ - tot_num_tp = j2k_get_num_tp(cp,pino,tileno); - tcd->tp_pos = cp->tp_pos; - - for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){ - j2k->tp_num = tilepartno; - /* INDEX >> */ - if(cstr_info) - cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos = - cio_tell(cio) + j2k->pos_correction; - /* << INDEX */ - j2k_write_sot(j2k); - - if(j2k->cur_tp_num == 0 && cp->cinema == 0){ - for (compno = 1; compno < image->numcomps; compno++) { - j2k_write_coc(j2k, compno); - j2k_write_qcc(j2k, compno); - } - if (cp->tcps[tileno].numpocs) { - j2k_write_poc(j2k); - } + // preconditions + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_EOC) + { + l_current_marker = J2K_MS_EOC; + } + else if + (p_j2k->m_specific_param.m_decoder.m_state != J2K_DEC_STATE_TPHSOT) + { + return false; + } + + while + (! p_j2k->m_specific_param.m_decoder.m_can_decode && l_current_marker != J2K_MS_EOC) + { + while + (l_current_marker != J2K_MS_SOD) + { + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); + if + (p_j2k->m_specific_param.m_decoder.m_state & J2K_DEC_STATE_TPH) + { + p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2); + } + l_marker_size -= 2; + + l_marker_handler = j2k_get_marker_handler(l_current_marker); + // Check if the marker is known + if + (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) + { + opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return false; + } + if + (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) + { + p_j2k->m_specific_param.m_decoder.m_header_data = opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size); + if + (p_j2k->m_specific_param.m_decoder.m_header_data == 00) + { + return false; } + p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; - /* INDEX >> */ - if(cstr_info) - cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header = - cio_tell(cio) + j2k->pos_correction + 1; - /* << INDEX */ - - j2k_write_sod(j2k, tcd); - - /* INDEX >> */ - if(cstr_info) { - cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos = - cio_tell(cio) + j2k->pos_correction - 1; - cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack = - acc_pack_num; - cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks = - cstr_info->packno - acc_pack_num; - acc_pack_num = cstr_info->packno; + } + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + if + (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return false; + } + if + (p_j2k->m_specific_param.m_decoder.m_skip_data) + { + if + (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; } - /* << INDEX */ - - j2k->cur_tp_num++; - } - } - if(cstr_info) { - cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1; + l_current_marker = J2K_MS_SOD; + } + else + { + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } } - - /* - if (tile->PPT) { // BAD PPT !!! - FILE *PPT_file; - int i; - PPT_file=fopen("PPT","rb"); - fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256); - for (i=0;ilen_ppt;i++) { - unsigned char elmt; - fread(&elmt, 1, 1, PPT_file); - fwrite(&elmt,1,1,f); + if + (! p_j2k->m_specific_param.m_decoder.m_skip_data) + { + if + (! j2k_read_sod(p_j2k,p_stream,p_manager)) + { + return false; + } } - fclose(PPT_file); - unlink("PPT"); + else + { + p_j2k->m_specific_param.m_decoder.m_skip_data = 0; + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_TPHSOT; + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } + } + + if + (l_current_marker == J2K_MS_EOC) + { + if + (p_j2k->m_specific_param.m_decoder.m_state != J2K_DEC_STATE_EOC) + { + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_EOC; + } + } + if + ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) + { + l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number; + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + while + ( + (p_j2k->m_current_tile_number < l_nb_tiles) + && (l_tcp->m_data == 00) + ) + { + ++p_j2k->m_current_tile_number; + ++l_tcp; + } + if + (p_j2k->m_current_tile_number == l_nb_tiles) + { + *p_go_on = false; + return true; + } + } + if + (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + return false; + } + *p_tile_index = p_j2k->m_current_tile_number; + *p_go_on = true; + *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd); + * p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0; + * p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0; + * p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1; + * p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1; + * p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps; + p_j2k->m_specific_param.m_decoder.m_state |= J2K_DEC_STATE_DATA; + return true; +} + +bool j2k_decode_tile ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 l_current_marker; + OPJ_BYTE l_data [2]; + opj_tcp_t * l_tcp; + + // preconditions + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + if + (! (p_j2k->m_specific_param.m_decoder.m_state & J2K_DEC_STATE_DATA) || p_tile_index != p_j2k->m_current_tile_number) + { + return false; + } + l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]); + if + (! l_tcp->m_data) + { + j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index])); + return false; + } + if + (! tcd_decode_tile(p_j2k->m_tcd, l_tcp->m_data, l_tcp->m_data_size, p_tile_index, p_j2k->cstr_info)) + { + j2k_tcp_destroy(l_tcp); + p_j2k->m_specific_param.m_decoder.m_state |= J2K_DEC_STATE_ERR; + return false; + } + if + (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) + { + return false; + } + j2k_tcp_destroy(l_tcp); + p_j2k->m_tcd->tcp = 0; + + p_j2k->m_specific_param.m_decoder.m_can_decode = 0; + p_j2k->m_specific_param.m_decoder.m_state &= (~J2K_DEC_STATE_DATA); + if + (p_j2k->m_specific_param.m_decoder.m_state != J2K_DEC_STATE_EOC) + { + if + (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(l_data,&l_current_marker,2); + if + (l_current_marker == J2K_MS_EOC) + { + p_j2k->m_current_tile_number = 0; + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_EOC; + } + else if + (l_current_marker != J2K_MS_SOT) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n"); + return false; + } + } + return true; +} + + +/** + * Ends the compression procedures and possibiliy add data to be read after the + * codestream. + */ +bool j2k_end_compress(opj_j2k_t *p_j2k, struct opj_stream_private *p_stream, struct opj_event_mgr * p_manager) +{ + /* customization of the encoding */ + j2k_setup_end_compress(p_j2k); + + if + (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) + { + return false; + } + return true; +} + +/** + * Reads a jpeg2000 codestream header structure. + * + * @param p_stream the stream to read data from. + * @param p_j2k the jpeg2000 codec. + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +bool j2k_read_header( + opj_j2k_t *p_j2k, + struct opj_image ** p_image, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + *p_image = 00; + /* create an empty image */ + p_j2k->m_image = opj_image_create0(); + if + (! p_j2k->m_image) + { + return false; + } + + /* customization of the validation */ + j2k_setup_decoding_validation (p_j2k); + + /* validation of the parameters codec */ + if + (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) + { + opj_image_destroy(p_j2k->m_image); + p_j2k->m_image = 00; + return false; + } + + /* customization of the encoding */ + j2k_setup_header_reading(p_j2k); + + /* read header */ + if + (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) + { + opj_image_destroy(p_j2k->m_image); + p_j2k->m_image = 00; + return false; + } + *p_image = p_j2k->m_image; + * p_tile_x0 = p_j2k->m_cp.tx0; + * p_tile_y0 = p_j2k->m_cp.ty0; + * p_tile_width = p_j2k->m_cp.tdx; + * p_tile_height = p_j2k->m_cp.tdy; + * p_nb_tiles_x = p_j2k->m_cp.tw; + * p_nb_tiles_y = p_j2k->m_cp.th; + return true; +} + +/** + * The read header procedure. + */ +bool j2k_read_header_procedure( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + OPJ_UINT32 l_current_marker; + OPJ_UINT32 l_marker_size; + const opj_dec_memory_marker_handler_t * l_marker_handler = 00; + + // preconditions + assert(p_stream != 00); + assert(p_j2k != 00); + assert(p_manager != 00); + + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_MHSOC; + + if + (! j2k_read_soc(p_j2k,p_stream,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n"); + return false; + } + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + + while + (l_current_marker != J2K_MS_SOT) + { + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2); + l_marker_size -= 2; + /*if + (l_current_marker < 0xff00) + { + opj_event_msg(p_manager, EVT_ERROR, "%.8x: expected a marker instead of %x\n", opj_stream_tell(p_stream) - 2, l_current_marker); + return 0; } */ + l_marker_handler = j2k_get_marker_handler(l_current_marker); + // Check if the marker is known + if + (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) + { + opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return false; + } + if + (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) + { + p_j2k->m_specific_param.m_decoder.m_header_data = opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size); + if + (p_j2k->m_specific_param.m_decoder.m_header_data == 00) + { + return false; + } + p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size; + } + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + if + (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n"); + return false; + } + if + (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n"); + return false; + } + opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2); + } + p_j2k->m_specific_param.m_decoder.m_state = J2K_DEC_STATE_TPHSOT; + return true; +} + + + +/** + * Reads the tiles. + */ +bool j2k_decode_tiles ( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + bool l_go_on = true; + OPJ_UINT32 l_current_tile_no; + OPJ_UINT32 l_data_size,l_max_data_size; + OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; + OPJ_UINT32 l_nb_comps; + OPJ_BYTE * l_current_data; + + l_current_data = opj_malloc(1000); + if + (! l_current_data) + { + return false; + } + l_max_data_size = 1000; + + while + (true) + { + if + (! j2k_read_tile_header( + p_j2k,&l_current_tile_no, + &l_data_size, + &l_tile_x0, + &l_tile_y0, + &l_tile_x1, + &l_tile_y1, + &l_nb_comps, + &l_go_on, + p_stream, + p_manager)) + { + return false; + } + if + (! l_go_on) + { + break; + } + if + (l_data_size > l_max_data_size) + { + l_current_data = opj_realloc(l_current_data,l_data_size); + if + (! l_current_data) + { + return false; + } + l_max_data_size = l_data_size; + } + if + (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) + { + opj_free(l_current_data); + return false; + } + if + (! j2k_update_image_data(p_j2k->m_tcd,l_current_data)) + { + opj_free(l_current_data); + return false; + } } - - /* destroy the tile encoder */ - tcd_free_encode(tcd); - tcd_destroy(tcd); - - opj_free(j2k->cur_totnum_tp); - - j2k_write_eoc(j2k); - - if(cstr_info) { - cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction; - /* UniPG>> */ - /* The following adjustment is done to adjust the codestream size */ - /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */ - /* the first bunch of bytes is not in the codestream */ - cstr_info->codestream_size -= cstr_info->main_head_start; - /* <epc_on) { - - /* encode according to JPWL */ - jpwl_encode(j2k, cio, image); - - } -#endif /* USE_JPWL */ - + opj_free(l_current_data); return true; } @@ -2507,3 +8898,410 @@ bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestre +/** + * Decodes the tiles of the stream. + */ +opj_image_t * j2k_decode( + opj_j2k_t * p_j2k, + opj_stream_private_t * p_stream, + opj_event_mgr_t * p_manager) +{ + /* customization of the encoding */ + j2k_setup_decoding(p_j2k); + + /* write header */ + if + (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) + { + opj_image_destroy(p_j2k->m_image); + p_j2k->m_image = 00; + } + return p_j2k->m_image; +} + +/** + * Encodes all the tiles in a row. + */ +bool j2k_encode( + opj_j2k_t * p_j2k, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + OPJ_UINT32 i; + OPJ_UINT32 l_nb_tiles; + OPJ_UINT32 l_max_tile_size, l_current_tile_size; + OPJ_BYTE * l_current_data; + + // preconditions + assert(p_j2k != 00); + assert(p_stream != 00); + assert(p_manager != 00); + + l_current_data = opj_malloc(1000); + if + (! l_current_data) + { + return false; + } + l_max_tile_size = 1000; + + l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw; + for + (i=0;im_tcd); + if + (l_current_tile_size > l_max_tile_size) + { + l_current_data = opj_realloc(l_current_data,l_current_tile_size); + if + (! l_current_data) + { + return false; + } + l_max_tile_size = l_current_tile_size; + } + j2k_get_tile_data(p_j2k->m_tcd,l_current_data); + if + (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) + { + return false; + } + } + opj_free(l_current_data); + return true; +} + + + +/** + * Ends the decompression procedures and possibiliy add data to be read after the + * codestream. + */ +bool j2k_end_decompress( + opj_j2k_t *p_j2k, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager) +{ + return true; +} + + + +void j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data) +{ + OPJ_UINT32 i,j,k = 0; + OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + opj_image_t * l_image = 00; + OPJ_UINT32 l_size_comp, l_remaining; + OPJ_INT32 * l_src_ptr; + l_tilec = p_tcd->tcd_image->tiles->comps; + l_image = p_tcd->image; + l_img_comp = l_image->comps; + for + (i=0;iimage->numcomps;++i) + { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + if + (l_remaining) + { + ++l_size_comp; + } + if + (l_size_comp == 3) + { + l_size_comp = 4; + } + l_width = (l_tilec->x1 - l_tilec->x0); + l_height = (l_tilec->y1 - l_tilec->y0); + l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx); + l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy); + l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx); + l_stride = l_image_width - l_width; + l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width; + + switch + (l_size_comp) + { + case 1: + { + OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data; + if + (l_img_comp->sgnd) + { + for + (j=0;jsgnd) + { + for + (j=0;jtcd_image->tiles->comps; + l_image = p_tcd->image; + l_img_comp = l_image->comps; + for + (i=0;iimage->numcomps;++i) + { + if + (!l_img_comp->data) + { + l_img_comp->data = (OPJ_INT32*) opj_malloc(l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32)); + if + (! l_img_comp->data) + { + return false; + } + memset(l_img_comp->data,0,l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32)); + } + + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_res = l_tilec->resolutions + l_img_comp->resno_decoded; + + if + (l_remaining) + { + ++l_size_comp; + } + if + (l_size_comp == 3) + { + l_size_comp = 4; + } + l_width = (l_res->x1 - l_res->x0); + l_height = (l_res->y1 - l_res->y0); + l_dest_stride = (l_img_comp->w) - l_width; + l_offset_x = int_ceildivpow2(l_img_comp->x0, l_img_comp->factor); + l_offset_y = int_ceildivpow2(l_img_comp->y0, l_img_comp->factor); + l_dest_ptr = l_img_comp->data + (l_res->x0 - l_offset_x) + (l_res->y0 - l_offset_y) * l_img_comp->w; + + switch + (l_size_comp) + { + case 1: + { + OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data; + if + (l_img_comp->sgnd) + { + for + (j=0;jsgnd) + { + for + (j=0;jm_cp); + + if + (p_j2k->m_specific_param.m_decoder.m_state != J2K_DEC_STATE_TPHSOT) + { + return false; + } + p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx; + p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy; + p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx); + p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy); + p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1; + return true; +} diff --git a/libopenjpeg/j2k.h b/libopenjpeg/j2k.h index 5599be47..c28123a5 100644 --- a/libopenjpeg/j2k.h +++ b/libopenjpeg/j2k.h @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,6 +39,11 @@ The functions in J2K.C have for goal to read/write the several parts of the codestream: markers and data. */ +#include "openjpeg.h" + +struct opj_dparameters; +struct opj_stream_private; +struct opj_event_mgr; /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */ /*@{*/ @@ -77,6 +83,11 @@ The functions in J2K.C have for goal to read/write the several parts of the code #define J2K_MS_EPH 0xff92 /**< EPH marker value */ #define J2K_MS_CRG 0xff63 /**< CRG marker value */ #define J2K_MS_COM 0xff64 /**< COM marker value */ +#define J2K_MS_CBD 0xff78 /**< CBD marker value */ +#define J2K_MS_MCC 0xff75 /**< MCC marker value */ +#define J2K_MS_MCT 0xff74 /**< MCT marker value */ +#define J2K_MS_MCO 0xff77 /**< MCO marker value */ + /* UniPG>> */ #ifdef USE_JPWL #define J2K_MS_EPC 0xff68 /**< EPC marker value (Part 11: JPEG 2000 for Wireless) */ @@ -97,171 +108,267 @@ The functions in J2K.C have for goal to read/write the several parts of the code Values that specify the status of the decoding process when decoding the main header. These values may be combined with a | operator. */ -typedef enum J2K_STATUS { - J2K_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */ - J2K_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */ - J2K_STATE_MH = 0x0004, /**< the decoding process is in the main header */ - J2K_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */ - J2K_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */ - J2K_STATE_MT = 0x0020, /**< the EOC marker has just been read */ - J2K_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */ - J2K_STATE_ERR = 0x0080 /**< the decoding process has encountered an error */ -} J2K_STATUS; +typedef enum +{ + J2K_DEC_STATE_NONE = 0x0000, /**< a SOC marker is expected */ + J2K_DEC_STATE_MHSOC = 0x0001, /**< a SOC marker is expected */ + J2K_DEC_STATE_MHSIZ = 0x0002, /**< a SIZ marker is expected */ + J2K_DEC_STATE_MH = 0x0004, /**< the decoding process is in the main header */ + J2K_DEC_STATE_TPHSOT = 0x0008, /**< the decoding process is in a tile part header and expects a SOT marker */ + J2K_DEC_STATE_TPH = 0x0010, /**< the decoding process is in a tile part header */ + J2K_DEC_STATE_MT = 0x0020, /**< the EOC marker has just been read */ + J2K_DEC_STATE_NEOC = 0x0040, /**< the decoding process must not expect a EOC marker because the codestream is truncated */ + J2K_DEC_STATE_DATA = 0x0080, /**< the decoding process must not expect a EOC marker because the codestream is truncated */ + J2K_DEC_STATE_ERR = 0x8000, /**< the decoding process has encountered an error */ + J2K_DEC_STATE_EOC = 0x0100 +} +J2K_DECODING_STATUS; + +/** +Values that specify the status of the decoding process when decoding the main header. +These values may be combined with a | operator. +*/ +typedef enum +{ + J2K_ENC_STATE_NONE = 0x0000, /**< a SOC marker is expected */ + J2K_ENC_STATE_ENCODE = 0x0001, /**< a SOC marker is expected */ +} +J2K_ENCODING_STATUS; + +/** + * Type of data for storing the MCT data + */ +typedef enum MCT_ELEMENT_TYPE +{ + MCT_TYPE_INT16 = 0, /** MCT data is stored as signed shorts*/ + MCT_TYPE_INT32 = 1, /** MCT data is stored as signed integers*/ + MCT_TYPE_FLOAT = 2, /** MCT data is stored as floats*/ + MCT_TYPE_DOUBLE = 3 /** MCT data is stored as doubles*/ +} J2K_MCT_ELEMENT_TYPE; + +/** + * Type of data for storing the MCT data + */ +typedef enum MCT_ARRAY_TYPE +{ + MCT_TYPE_DEPENDENCY = 0, + MCT_TYPE_DECORRELATION = 1, + MCT_TYPE_OFFSET = 2 +} J2K_MCT_ARRAY_TYPE; + /* ----------------------------------------------------------------------- */ -/** -T2 encoding mode -*/ -typedef enum T2_MODE { - THRESH_CALC = 0, /** Function called in Rate allocation process*/ - FINAL_PASS = 1 /** Function called in Tier 2 process*/ -}J2K_T2_MODE; + /** Quantization stepsize */ -typedef struct opj_stepsize { +typedef struct opj_stepsize +{ /** exponent */ - int expn; + OPJ_UINT32 expn; /** mantissa */ - int mant; -} opj_stepsize_t; + OPJ_UINT32 mant; +} +opj_stepsize_t; + +typedef struct opj_mct_data +{ + J2K_MCT_ELEMENT_TYPE m_element_type; + J2K_MCT_ARRAY_TYPE m_array_type; + OPJ_UINT32 m_index; + OPJ_BYTE * m_data; + OPJ_UINT32 m_data_size; +} +opj_mct_data_t; + +typedef struct opj_simple_mcc_decorrelation_data +{ + OPJ_UINT32 m_index; + OPJ_UINT32 m_nb_comps; + opj_mct_data_t * m_decorrelation_array; + opj_mct_data_t * m_offset_array; + OPJ_UINT32 m_is_irreversible : 1; +} +opj_simple_mcc_decorrelation_data_t; + /** Tile-component coding parameters */ -typedef struct opj_tccp { +typedef struct opj_tccp +{ /** coding style */ - int csty; + OPJ_UINT32 csty; /** number of resolutions */ - int numresolutions; + OPJ_UINT32 numresolutions; /** code-blocks width */ - int cblkw; + OPJ_UINT32 cblkw; /** code-blocks height */ - int cblkh; + OPJ_UINT32 cblkh; /** code-block coding style */ - int cblksty; + OPJ_UINT32 cblksty; /** discrete wavelet transform identifier */ - int qmfbid; + OPJ_UINT32 qmfbid; /** quantisation style */ - int qntsty; + OPJ_UINT32 qntsty; /** stepsizes used for quantization */ opj_stepsize_t stepsizes[J2K_MAXBANDS]; /** number of guard bits */ - int numgbits; + OPJ_UINT32 numgbits; /** Region Of Interest shift */ - int roishift; + OPJ_INT32 roishift; /** precinct width */ - int prcw[J2K_MAXRLVLS]; + OPJ_UINT32 prcw[J2K_MAXRLVLS]; /** precinct height */ - int prch[J2K_MAXRLVLS]; -} opj_tccp_t; + OPJ_UINT32 prch[J2K_MAXRLVLS]; + /** the dc_level_shift **/ + OPJ_INT32 m_dc_level_shift; +} +opj_tccp_t; /** Tile coding parameters : this structure is used to store coding/decoding parameters common to all tiles (information like COD, COC in main header) */ -typedef struct opj_tcp { - /** 1 : first part-tile of a tile */ - int first; +typedef struct opj_tcp +{ /** coding style */ - int csty; + OPJ_UINT32 csty; /** progression order */ OPJ_PROG_ORDER prg; /** number of layers */ - int numlayers; + OPJ_UINT32 numlayers; + OPJ_UINT32 num_layers_to_decode; /** multi-component transform identifier */ - int mct; + OPJ_UINT32 mct; /** rates of layers */ - float rates[100]; + OPJ_FLOAT32 rates[100]; /** number of progression order changes */ - int numpocs; - /** indicates if a POC marker has been used O:NO, 1:YES */ - int POC; + OPJ_UINT32 numpocs; /** progression order changes */ opj_poc_t pocs[32]; /** packet header store there for futur use in t2_decode_packet */ - unsigned char *ppt_data; - /** pointer remaining on the first byte of the first header if ppt is used */ - unsigned char *ppt_data_first; - /** If ppt == 1 --> there was a PPT marker for the present tile */ - int ppt; - /** used in case of multiple marker PPT (number of info already stored) */ - int ppt_store; - /** ppmbug1 */ - int ppt_len; + OPJ_BYTE *ppt_data; + /** used to keep a track of the allocated memory */ + OPJ_BYTE *ppt_buffer; + /** Number of bytes stored inside ppt_data*/ + OPJ_UINT32 ppt_data_size; + /** size of ppt_data*/ + OPJ_UINT32 ppt_len; /** add fixed_quality */ - float distoratio[100]; + OPJ_FLOAT32 distoratio[100]; /** tile-component coding parameters */ opj_tccp_t *tccps; + /** number of tile parts for the tile. */ + OPJ_UINT32 m_nb_tile_parts; + /** data for the tile */ + OPJ_BYTE * m_data; + /** size of data */ + OPJ_UINT32 m_data_size; + /** encoding norms */ + OPJ_FLOAT64 * mct_norms; + /** the mct decoding matrix */ + OPJ_FLOAT32 * m_mct_decoding_matrix; + /** the mct coding matrix */ + OPJ_FLOAT32 * m_mct_coding_matrix; + /** mct records */ + opj_mct_data_t * m_mct_records; + /** the number of mct records. */ + OPJ_UINT32 m_nb_mct_records; + /** the max number of mct records. */ + OPJ_UINT32 m_nb_max_mct_records; + /** mcc records */ + opj_simple_mcc_decorrelation_data_t * m_mcc_records; + /** the number of mct records. */ + OPJ_UINT32 m_nb_mcc_records; + /** the max number of mct records. */ + OPJ_UINT32 m_nb_max_mcc_records; + + + + /***** FLAGS *******/ + /** If ppt == 1 --> there was a PPT marker for the present tile */ + OPJ_UINT32 ppt : 1; + /** indicates if a POC marker has been used O:NO, 1:YES */ + OPJ_UINT32 POC : 1; } opj_tcp_t; +typedef struct opj_encoding_param +{ + /** Digital cinema profile*/ + OPJ_CINEMA_MODE m_cinema; + /** Maximum rate for each component. If == 0, component size limitation is not considered */ + OPJ_UINT32 m_max_comp_size; + /** Position of tile part flag in progression order*/ + OPJ_INT32 m_tp_pos; + /** fixed layer */ + OPJ_INT32 *m_matrice; + /** Flag determining tile part generation*/ + OPJ_BYTE m_tp_flag; + /** allocation by rate/distortion */ + OPJ_UINT32 m_disto_alloc : 1; + /** allocation by fixed layer */ + OPJ_UINT32 m_fixed_alloc : 1; + /** add fixed_quality */ + OPJ_UINT32 m_fixed_quality : 1; + /** Enabling Tile part generation*/ + OPJ_UINT32 m_tp_on : 1; +} +opj_encoding_param_t; + +typedef struct opj_decoding_param +{ + /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */ + OPJ_UINT32 m_reduce; + /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */ + OPJ_UINT32 m_layer; +} +opj_decoding_param_t; + /** Coding parameters */ -typedef struct opj_cp { - /** Digital cinema profile*/ - OPJ_CINEMA_MODE cinema; - /** Maximum rate for each component. If == 0, component size limitation is not considered */ - int max_comp_size; +typedef struct opj_cp +{ /** Size of the image in bits*/ - int img_size; + /*int img_size;*/ /** Rsiz*/ OPJ_RSIZ_CAPABILITIES rsiz; - /** Enabling Tile part generation*/ - char tp_on; - /** Flag determining tile part generation*/ - char tp_flag; - /** Position of tile part flag in progression order*/ - int tp_pos; - /** allocation by rate/distortion */ - int disto_alloc; - /** allocation by fixed layer */ - int fixed_alloc; - /** add fixed_quality */ - int fixed_quality; - /** if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */ - int reduce; - /** if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */ - int layer; - /** if == NO_LIMITATION, decode entire codestream; if == LIMIT_TO_MAIN_HEADER then only decode the main header */ - OPJ_LIMIT_DECODING limit_decoding; /** XTOsiz */ - int tx0; + OPJ_INT32 tx0; /** YTOsiz */ - int ty0; + OPJ_INT32 ty0; /** XTsiz */ - int tdx; + OPJ_UINT32 tdx; /** YTsiz */ - int tdy; - /** comment for coding */ - char *comment; + OPJ_UINT32 tdy; + /** comment */ + OPJ_CHAR *comment; /** number of tiles in width */ - int tw; + OPJ_UINT32 tw; /** number of tiles in heigth */ - int th; - /** ID number of the tiles present in the codestream */ - int *tileno; - /** size of the vector tileno */ - int tileno_size; + OPJ_UINT32 th; + /** packet header storage original buffer */ + OPJ_BYTE *ppm_buffer; /** packet header store there for futur use in t2_decode_packet */ - unsigned char *ppm_data; - /** pointer remaining on the first byte of the first header if ppm is used */ - unsigned char *ppm_data_first; - /** if ppm == 1 --> there was a PPM marker for the present tile */ - int ppm; - /** use in case of multiple marker PPM (number of info already store) */ - int ppm_store; - /** use in case of multiple marker PPM (case on non-finished previous info) */ - int ppm_previous; - /** ppmbug1 */ - int ppm_len; + OPJ_BYTE *ppm_data; + /** size of the ppm_data*/ + OPJ_UINT32 ppm_len; + /** Number of bytes actually stored inside the ppm_data */ + OPJ_UINT32 ppm_data_size; /** tile coding parameters */ opj_tcp_t *tcps; - /** fixed layer */ - int *matrice; + union + { + opj_decoding_param_t m_dec; + opj_encoding_param_t m_enc; + } + m_specific_param; + + /* UniPG>> */ #ifdef USE_JPWL /** enables writing of EPC in MH, thus activating JPWL */ @@ -305,90 +412,192 @@ typedef struct opj_cp { /** maximum number of tiles at the decoder */ int max_tiles; #endif /* USE_JPWL */ + + /******** FLAGS *********/ + /** if ppm == 1 --> there was a PPM marker*/ + OPJ_UINT32 ppm : 1; + /** tells if the parameter is a coding or decoding one */ + OPJ_UINT32 m_is_decoder : 1; /* <cp. -@param j2k J2K decompressor handle -@param parameters decompression parameters + * Starts a compression scheme, i.e. validates the codec parameters, writes the header. + * + * @param p_j2k the jpeg2000 codec. + * @param cio the stream object. + * @param p_manager the user event manager. + * + * @return true if the codec is valid. + */ +bool j2k_start_compress( + opj_j2k_t *p_j2k, + struct opj_stream_private *cio, + struct opj_image * p_image, + struct opj_event_mgr * p_manager + ); +/** + * Ends the compression procedures and possibiliy add data to be read after the + * codestream. + */ +bool j2k_end_compress(opj_j2k_t *p_j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); + +/** + * Sets up the decoder decoding parameters using user parameters. + * Decoding parameters are stored in j2k->cp. + * + * @param p_j2k J2K codec + * @param p_parameters decompression parameters + * @deprecated */ -void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters); +void j2k_setup_decoder( + opj_j2k_t *p_j2k, + struct opj_dparameters *p_parameters + ); +/** + * Reads a jpeg2000 codestream header structure. + * + * @param cio the stream to read data from. + * @param p_j2k the jpeg2000 codec. + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +bool j2k_read_header( + opj_j2k_t *p_j2k, + struct opj_image ** p_image, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); /** Decode an image from a JPEG-2000 codestream @param j2k J2K decompressor handle @@ -396,7 +605,14 @@ Decode an image from a JPEG-2000 codestream @param cstr_info Codestream information structure if required, NULL otherwise @return Returns a decoded image if successful, returns NULL otherwise */ -opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info); +opj_image_t* j2k_decode(opj_j2k_t *j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); + +/** + * Ends the decompression procedures and possibiliy add data to be read after the + * codestream. + */ +bool j2k_end_decompress(opj_j2k_t *j2k, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); + /** Decode an image form a JPT-stream (JPEG 2000, JPIP) @param j2k J2K decompressor handle @@ -404,18 +620,15 @@ Decode an image form a JPT-stream (JPEG 2000, JPIP) @param cstr_info Codestream information structure if required, NULL otherwise @return Returns a decoded image if successful, returns NULL otherwise */ -opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info); +opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, struct opj_stream_private *cio, struct opj_codestream_info *cstr_info); + /** Creates a J2K compression structure @param cinfo Codec context info @return Returns a handle to a J2K compressor if successful, returns NULL otherwise */ -opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo); -/** -Destroy a J2K compressor handle -@param j2k J2K compressor handle to destroy -*/ -void j2k_destroy_compress(opj_j2k_t *j2k); +opj_j2k_t* j2k_create_compress(); + /** Setup the encoder parameters using the current image and using user parameters. Coding parameters are returned in j2k->cp. @@ -423,20 +636,97 @@ Coding parameters are returned in j2k->cp. @param parameters compression parameters @param image input filled image */ -void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image); +void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image, struct opj_event_mgr * p_manager); + /** -Converts an enum type progression order to string type -*/ -char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order); + * Writes a tile. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool j2k_write_tile ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); + /** -Encode an image into a JPEG-2000 codestream -@param j2k J2K compressor handle -@param cio Output buffer stream -@param image Image to encode -@param cstr_info Codestream information structure if required, NULL otherwise -@return Returns true if successful, returns false otherwise -*/ -bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info); + * Converts an enum type progression order to string type. + * + * @param prg_order the progression order to get. + * + * @return the string representation of the gicen progression order. + */ +const char * j2k_convert_progression_order(OPJ_PROG_ORDER prg_order); + + +/** + * Encodes an image into a JPEG-2000 codestream + */ +bool j2k_encode( + opj_j2k_t * p_j2k, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); +bool j2k_setup_mct_encoding (opj_tcp_t * p_tcp,opj_image_t * p_image); + +/** + * Decode tile data. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool j2k_decode_tile ( + opj_j2k_t * p_j2k, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Reads a tile header. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool j2k_read_tile_header ( + opj_j2k_t * p_j2k, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_go_on, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_j2k the jpeg2000 codec. + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * @param p_manager the user event manager + * + * @return true if the area could be set. + */ +bool j2k_set_decode_area( + opj_j2k_t *p_j2k, + OPJ_INT32 p_start_x, + OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, + OPJ_INT32 p_end_y, + struct opj_event_mgr * p_manager + ); + /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/j2k_lib.c b/libopenjpeg/j2k_lib.c index 91aee007..3b86c728 100644 --- a/libopenjpeg/j2k_lib.c +++ b/libopenjpeg/j2k_lib.c @@ -27,13 +27,16 @@ #ifdef WIN32 #include #else -#include +/*#include #include #include +*/ #endif /* WIN32 */ -#include "opj_includes.h" +#include "j2k_lib.h" -double opj_clock(void) { + +OPJ_FLOAT64 opj_clock(void) { +#if 0 #ifdef WIN32 /* WIN32: use QueryPerformance (very accurate) */ LARGE_INTEGER freq , t ; @@ -55,5 +58,8 @@ double opj_clock(void) { /* (2b) More precisely! Get the microseconds part ! */ return ( procTime + (t.ru_utime.tv_usec + t.ru_stime.tv_usec) * 1e-6 ) ; #endif + +#endif + return 0; } diff --git a/libopenjpeg/j2k_lib.h b/libopenjpeg/j2k_lib.h index 7df4d367..c78c4f21 100644 --- a/libopenjpeg/j2k_lib.h +++ b/libopenjpeg/j2k_lib.h @@ -31,7 +31,7 @@ The functions in J2K_LIB.C are internal utilities mainly used for timing. */ - +#include "openjpeg.h" /** @defgroup MISC MISC - Miscellaneous internal functions */ /*@{*/ @@ -43,7 +43,7 @@ The functions in J2K_LIB.C are internal utilities mainly used for timing. Difference in successive opj_clock() calls tells you the elapsed time @return Returns time in seconds */ -double opj_clock(void); +OPJ_FLOAT64 opj_clock(void); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/jp2.c b/libopenjpeg/jp2.c index b2831cfb..1bd7b4a9 100644 --- a/libopenjpeg/jp2.c +++ b/libopenjpeg/jp2.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,540 +29,2052 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - -#include "opj_includes.h" +#include "jp2.h" +#include "cio.h" +#include "opj_malloc.h" +#include "event.h" +#include "j2k.h" +#include "function_list.h" +#include "assert.h" /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */ /*@{*/ +#define BOX_SIZE 1024 + + + /** @name Local static functions */ /*@{*/ -/** -Read box headers -@param cinfo Codec context info -@param cio Input stream -@param box -@return Returns true if successful, returns false otherwise -*/ -static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box); -/*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/ -/** -Read the IHDR box - Image Header box -@param jp2 JP2 handle -@param cio Input buffer stream -@return Returns true if successful, returns false otherwise -*/ -static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio); -static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio); -static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio); -static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio); -static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio); -static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio); -/** -Write the FTYP box - File type box -@param jp2 JP2 handle -@param cio Output buffer stream -*/ -static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio); -/** -Read the FTYP box - File type box -@param jp2 JP2 handle -@param cio Input buffer stream -@return Returns true if successful, returns false otherwise -*/ -static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio); -static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info); -static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset); -static void jp2_write_jp(opj_cio_t *cio); -/** -Read the JP box - JPEG 2000 signature -@param jp2 JP2 handle -@param cio Input buffer stream -@return Returns true if successful, returns false otherwise -*/ -static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio); -/** -Decode the structure of a JP2 file -@param jp2 JP2 handle -@param cio Input buffer stream -@return Returns true if successful, returns false otherwise -*/ -static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio); + +/** + * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writting was successful. +*/ +bool jp2_write_jp2h( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Skips the Jpeg2000 Codestream Header box - JP2C Header box. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writting was successful. +*/ +bool jp2_skip_jp2c( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param p_header_data the data contained in the file header box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the file header box. + * @param p_manager the user event manager. + * + * @return true if the JP2 Header box was successfully reconized. +*/ +bool jp2_read_jp2h( + opj_jp2_t *jp2, + unsigned char * p_header_data, + unsigned int p_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writting was successful. +*/ +static bool jp2_write_jp2c( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. + * + * @param cio the input stream to read data from. + * @param box the box structure to fill. + * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). + * @param p_manager user event manager. + * + * @return true if the box is reconized, false otherwise +*/ +static bool jp2_read_boxhdr( + opj_jp2_box_t *box, + OPJ_UINT32 * p_number_bytes_read, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string + * + * @param p_data the character string to read data from. + * @param box the box structure to fill. + * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). + * @param p_box_max_size the maximum number of bytes in the box. + * + * @return true if the box is reconized, false otherwise +*/ +static bool jp2_read_boxhdr_char( + opj_jp2_box_t *box, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_number_bytes_read, + OPJ_UINT32 p_box_max_size, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a jpeg2000 file signature box. + * + * @param p_header_data the data contained in the signature box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the signature box. + * @param p_manager the user event manager. + * + * @return true if the file signature box is valid. + */ +static bool jp2_read_jp( + opj_jp2_t *jp2, + unsigned char * p_header_data, + unsigned int p_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Writes a jpeg2000 file signature box. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager the user event manager. + * + * @return true if writting was successful. + */ +static bool jp2_write_jp( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Writes a FTYP box - File type box + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager the user event manager. + * + * @return true if writting was successful. + */ +static bool jp2_write_ftyp( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a a FTYP box - File type box + * + * @param p_header_data the data contained in the FTYP box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the FTYP box. + * @param p_manager the user event manager. + * + * @return true if the FTYP box is valid. + */ +static bool jp2_read_ftyp( + opj_jp2_t *jp2, + unsigned char * p_header_data, + unsigned int p_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Reads a IHDR box - Image Header box + * + * @param p_image_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_image_header_size the size of the image header + * @param p_manager the user event manager. + * + * @return true if the image header is valid, fale else. + */ +static bool jp2_read_ihdr( + opj_jp2_t *jp2, + unsigned char * p_image_header_data, + unsigned int p_image_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Writes the Image Header box - Image Header box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static unsigned char * jp2_write_ihdr( + opj_jp2_t *jp2, + unsigned int * p_nb_bytes_written + ); + +/** + * Reads a Bit per Component box. + * + * @param p_bpc_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_bpc_header_size the size of the bpc header + * @param p_manager the user event manager. + * + * @return true if the bpc header is valid, fale else. + */ +static bool jp2_read_bpcc( + opj_jp2_t *jp2, + unsigned char * p_bpc_header_data, + unsigned int p_bpc_header_size, + struct opj_event_mgr * p_manager + ); + + +/** + * Writes the Bit per Component box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static unsigned char * jp2_write_bpcc( + opj_jp2_t *jp2, + unsigned int * p_nb_bytes_written + ); + +/** + * Reads the Colour Specification box. + * + * @param p_colr_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_colr_header_size the size of the color header + * @param p_manager the user event manager. + * + * @return true if the bpc header is valid, fale else. +*/ +static bool jp2_read_colr( + opj_jp2_t *jp2, + unsigned char * p_colr_header_data, + unsigned int p_colr_header_size, + struct opj_event_mgr * p_manager + ); + +/** + * Writes the Colour Specification box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static unsigned char *jp2_write_colr( + opj_jp2_t *jp2, + unsigned int * p_nb_bytes_written + ); + + +/** + * Reads a jpeg2000 file header structure. + * + * @param cio the stream to read data from. + * @param jp2 the jpeg2000 file header structure. + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +bool jp2_read_header_procedure( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); +/** + * Excutes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param jp2 the jpeg2000 file codec to execute the procedures on. + * @param cio the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +static bool jp2_exec ( + opj_jp2_t * jp2, + struct opj_procedure_list * p_procedure_list, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); +/** + * Finds the execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or NULL if it could not be found. + */ +static const opj_jp2_header_handler_t * jp2_find_handler (int p_id); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static void jp2_setup_encoding_validation (opj_jp2_t *jp2); + +/** + * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures. + */ +static void jp2_setup_header_writting (opj_jp2_t *jp2); + +/** + * The default validation procedure without any extension. + * + * @param jp2 the jpeg2000 codec to validate. + * @param cio the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool jp2_default_validation ( + opj_jp2_t * jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); + +/** + * Finds the execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or NULL if it could not be found. + */ +static const opj_jp2_header_handler_t * jp2_find_handler ( + int p_id + ); + +/** + * Finds the image execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or NULL if it could not be found. + */ +static const opj_jp2_header_handler_t * jp2_img_find_handler ( + int p_id + ); + +/** + * Sets up the procedures to do on writting header after the codestream. + * Developpers wanting to extend the library can add their own writting procedures. + */ +static void jp2_setup_end_header_writting (opj_jp2_t *jp2); + +/** + * Sets up the procedures to do on reading header after the codestream. + * Developpers wanting to extend the library can add their own writting procedures. + */ +static void jp2_setup_end_header_reading (opj_jp2_t *jp2); + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +static void jp2_setup_decoding_validation (opj_jp2_t *jp2); + +/** + * Sets up the procedures to do on reading header. + * Developpers wanting to extend the library can add their own writting procedures. + */ +static void jp2_setup_header_reading (opj_jp2_t *jp2); /*@}*/ /*@}*/ /* ----------------------------------------------------------------------- */ +const opj_jp2_header_handler_t jp2_header [] = +{ + {JP2_JP,jp2_read_jp}, + {JP2_FTYP,jp2_read_ftyp}, + {JP2_JP2H,jp2_read_jp2h} +}; -static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) { - box->init_pos = cio_tell(cio); - box->length = cio_read(cio, 4); - box->type = cio_read(cio, 4); - if (box->length == 1) { - if (cio_read(cio, 4) != 0) { - opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n"); - return false; +const opj_jp2_header_handler_t jp2_img_header [] = +{ + {JP2_IHDR,jp2_read_ihdr}, + {JP2_COLR,jp2_read_colr}, + {JP2_BPCC,jp2_read_bpcc} +}; +/** + * Finds the execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or 00 if it could not be found. + */ +const opj_jp2_header_handler_t * jp2_find_handler ( + int p_id + ) +{ + unsigned int i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t); + for + (i=0;ilength = cio_read(cio, 4); - if (box->length == 0) - box->length = cio_numbytesleft(cio) + 12; } - else if (box->length == 0) { - box->length = cio_numbytesleft(cio) + 8; + return 00; +} + +/** + * Finds the image execution function related to the given box id. + * + * @param p_id the id of the handler to fetch. + * + * @return the given handler or 00 if it could not be found. + */ +static const opj_jp2_header_handler_t * jp2_img_find_handler ( + int p_id + ) +{ + unsigned int i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t); + for + (i=0;icinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - if (JP2_IHDR != box.type) { - opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n"); + if + (l_current_data == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n"); return false; } - - jp2->h = cio_read(cio, 4); /* HEIGHT */ - jp2->w = cio_read(cio, 4); /* WIDTH */ - jp2->numcomps = cio_read(cio, 2); /* NC */ - jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t)); - - jp2->bpc = cio_read(cio, 1); /* BPC */ - - jp2->C = cio_read(cio, 1); /* C */ - jp2->UnkC = cio_read(cio, 1); /* UnkC */ - jp2->IPR = cio_read(cio, 1); /* IPR */ - - if (cio_tell(cio) - box.init_pos != box.length) { - opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n"); - return false; - } - - return true; -} - -static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) { - opj_jp2_box_t box; - - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_IHDR, 4); /* IHDR */ - - cio_write(cio, jp2->h, 4); /* HEIGHT */ - cio_write(cio, jp2->w, 4); /* WIDTH */ - cio_write(cio, jp2->numcomps, 2); /* NC */ - - cio_write(cio, jp2->bpc, 1); /* BPC */ - - cio_write(cio, jp2->C, 1); /* C : Always 7 */ - cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */ - cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */ - - box.length = cio_tell(cio) - box.init_pos; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); -} - -static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) { - unsigned int i; - opj_jp2_box_t box; - - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_BPCC, 4); /* BPCC */ - - for (i = 0; i < jp2->numcomps; i++) { - cio_write(cio, jp2->comps[i].bpcc, 1); - } - - box.length = cio_tell(cio) - box.init_pos; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); -} - - -static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) { - unsigned int i; - opj_jp2_box_t box; - - opj_common_ptr cinfo = jp2->cinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - if (JP2_BPCC != box.type) { - opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n"); - return false; - } - - for (i = 0; i < jp2->numcomps; i++) { - jp2->comps[i].bpcc = cio_read(cio, 1); - } - - if (cio_tell(cio) - box.init_pos != box.length) { - opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n"); - return false; - } - - return true; -} - -static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) { - opj_jp2_box_t box; - - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_COLR, 4); /* COLR */ - - cio_write(cio, jp2->meth, 1); /* METH */ - cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */ - cio_write(cio, jp2->approx, 1); /* APPROX */ - - if (jp2->meth == 1) { - cio_write(cio, jp2->enumcs, 4); /* EnumCS */ - } else { - cio_write(cio, 0, 1); /* PROFILE (??) */ - } - - box.length = cio_tell(cio) - box.init_pos; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); -} - -static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) { - opj_jp2_box_t box; - int skip_len; - - opj_common_ptr cinfo = jp2->cinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - do { - if (JP2_COLR != box.type) { - cio_skip(cio, box.length - 8); - jp2_read_boxhdr(cinfo, cio, &box); - } - } while(JP2_COLR != box.type); - - jp2->meth = cio_read(cio, 1); /* METH */ - jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */ - jp2->approx = cio_read(cio, 1); /* APPROX */ - - if (jp2->meth == 1) { - jp2->enumcs = cio_read(cio, 4); /* EnumCS */ - } else { - /* skip PROFILE */ - skip_len = box.init_pos + box.length - cio_tell(cio); - if (skip_len < 0) { - opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n"); - return false; - } - cio_skip(cio, box.init_pos + box.length - cio_tell(cio)); - } - - if (cio_tell(cio) - box.init_pos != box.length) { - opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n"); - return false; - } - return true; -} - -void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) { - opj_jp2_box_t box; - - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_JP2H, 4); /* JP2H */ - - jp2_write_ihdr(jp2, cio); - - if (jp2->bpc == 255) { - jp2_write_bpcc(jp2, cio); - } - jp2_write_colr(jp2, cio); - - box.length = cio_tell(cio) - box.init_pos; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); -} - -bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) { - opj_jp2_box_t box; - int skip_len; - - opj_common_ptr cinfo = jp2->cinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - do { - if (JP2_JP2H != box.type) { - if (box.type == JP2_JP2C) { - opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n"); + memset(l_current_data, 0 , l_last_data_size); + while + (jp2_read_boxhdr(&box,&l_nb_bytes_read,cio,p_manager)) + { + // is it the codestream box ? + if + (box.type == JP2_JP2C) + { + if + (jp2->jp2_state & JP2_STATE_HEADER) + { + jp2->jp2_state |= JP2_STATE_CODESTREAM; + return true; + } + else + { + opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n"); return false; } - cio_skip(cio, box.length - 8); - jp2_read_boxhdr(cinfo, cio, &box); } - } while(JP2_JP2H != box.type); - - if (!jp2_read_ihdr(jp2, cio)) - return false; - - if (jp2->bpc == 255) { - if (!jp2_read_bpcc(jp2, cio)) + else if + (box.length == 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); return false; - } - if (!jp2_read_colr(jp2, cio)) - return false; - - skip_len = box.init_pos + box.length - cio_tell(cio); - if (skip_len < 0) { - opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n"); - return false; - } - cio_skip(cio, box.init_pos + box.length - cio_tell(cio)); - - return true; -} - -static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) { - unsigned int i; - opj_jp2_box_t box; - - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_FTYP, 4); /* FTYP */ - - cio_write(cio, jp2->brand, 4); /* BR */ - cio_write(cio, jp2->minversion, 4); /* MinV */ - - for (i = 0; i < jp2->numcl; i++) { - cio_write(cio, jp2->cl[i], 4); /* CL */ - } - - box.length = cio_tell(cio) - box.init_pos; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); -} - -static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) { - int i; - opj_jp2_box_t box; - - opj_common_ptr cinfo = jp2->cinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - - if (JP2_FTYP != box.type) { - opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n"); - return false; - } - - jp2->brand = cio_read(cio, 4); /* BR */ - jp2->minversion = cio_read(cio, 4); /* MinV */ - jp2->numcl = (box.length - 16) / 4; - jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int)); - - for (i = 0; i < (int)jp2->numcl; i++) { - jp2->cl[i] = cio_read(cio, 4); /* CLi */ - } - - if (cio_tell(cio) - box.init_pos != box.length) { - opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n"); - return false; - } - - return true; -} - -static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { - unsigned int j2k_codestream_offset, j2k_codestream_length; - opj_jp2_box_t box; - - opj_j2k_t *j2k = jp2->j2k; - - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_JP2C, 4); /* JP2C */ - - /* J2K encoding */ - j2k_codestream_offset = cio_tell(cio); - if(!j2k_encode(j2k, cio, image, cstr_info)) { - opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n"); - return 0; - } - j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset; - - jp2->j2k_codestream_offset = j2k_codestream_offset; - jp2->j2k_codestream_length = j2k_codestream_length; - - box.length = 8 + jp2->j2k_codestream_length; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); - - return box.length; -} - -static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) { - opj_jp2_box_t box; - - opj_common_ptr cinfo = jp2->cinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - do { - if(JP2_JP2C != box.type) { - cio_skip(cio, box.length - 8); - jp2_read_boxhdr(cinfo, cio, &box); } - } while(JP2_JP2C != box.type); - *j2k_codestream_offset = cio_tell(cio); - *j2k_codestream_length = box.length - 8; + l_current_handler = jp2_find_handler(box.type); + l_current_data_size = box.length - l_nb_bytes_read; + if + (l_current_handler != 00) + { + if + (l_current_data_size > l_last_data_size) + { + l_current_data = opj_realloc(l_current_data,l_current_data_size); + l_last_data_size = l_current_data_size; + } + l_nb_bytes_read = opj_stream_read_data(cio,l_current_data,l_current_data_size,p_manager); + if + (l_nb_bytes_read != l_current_data_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n"); + return false; + } + if + (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) + { + return false; + } + } + else + { + jp2->jp2_state |= JP2_STATE_UNKNOWN; + if + (opj_stream_skip(cio,l_current_data_size,p_manager) != l_current_data_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n"); + return false; + } + } + } return true; } -static void jp2_write_jp(opj_cio_t *cio) { - opj_jp2_box_t box; +/** + * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. + * + * @param cio the input stream to read data from. + * @param box the box structure to fill. + * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (should usually be 8). + * @param p_manager user event manager. + * + * @return true if the box is reconized, false otherwise +*/ +bool jp2_read_boxhdr(opj_jp2_box_t *box, OPJ_UINT32 * p_number_bytes_read,opj_stream_private_t *cio, opj_event_mgr_t * p_manager) +{ + /* read header from file */ + unsigned char l_data_header [8]; - box.init_pos = cio_tell(cio); - cio_skip(cio, 4); - cio_write(cio, JP2_JP, 4); /* JP2 signature */ - cio_write(cio, 0x0d0a870a, 4); + // preconditions + assert(cio != 00); + assert(box != 00); + assert(p_number_bytes_read != 00); + assert(p_manager != 00); - box.length = cio_tell(cio) - box.init_pos; - cio_seek(cio, box.init_pos); - cio_write(cio, box.length, 4); /* L */ - cio_seek(cio, box.init_pos + box.length); -} - -static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) { - opj_jp2_box_t box; - - opj_common_ptr cinfo = jp2->cinfo; - - jp2_read_boxhdr(cinfo, cio, &box); - if (JP2_JP != box.type) { - opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n"); + *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager); + if + (*p_number_bytes_read != 8) + { return false; } - if (0x0d0a870a != cio_read(cio, 4)) { - opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n"); - return false; - } - if (cio_tell(cio) - box.init_pos != box.length) { - opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n"); - return false; - } - - return true; -} - - -static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) { - if (!jp2_read_jp(jp2, cio)) - return false; - if (!jp2_read_ftyp(jp2, cio)) - return false; - if (!jp2_read_jp2h(jp2, cio)) - return false; - if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset)) - return false; + /* process read data */ + opj_read_bytes(l_data_header,&(box->length), 4); + opj_read_bytes(l_data_header+4,&(box->type), 4); + // do we have a "special very large box ?" + // read then the XLBox + if + (box->length == 1) + { + OPJ_UINT32 l_xl_part_size; + OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager); + if + (l_nb_bytes_read != 8) + { + if + (l_nb_bytes_read > 0) + { + *p_number_bytes_read += l_nb_bytes_read; + } + return false; + } + opj_read_bytes(l_data_header,&l_xl_part_size, 4); + if + (l_xl_part_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n"); + return false; + } + opj_read_bytes(l_data_header,&(box->length), 4); + } return true; } -/* ----------------------------------------------------------------------- */ -/* JP2 decoder interface */ -/* ----------------------------------------------------------------------- */ +/** + * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string + * + * @param p_data the character string to read data from. + * @param box the box structure to fill. + * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2). + * @param p_box_max_size the maximum number of bytes in the box. + * + * @return true if the box is reconized, false otherwise +*/ +static bool jp2_read_boxhdr_char( + opj_jp2_box_t *box, + OPJ_BYTE * p_data, + OPJ_UINT32 * p_number_bytes_read, + OPJ_UINT32 p_box_max_size, + opj_event_mgr_t * p_manager + ) +{ + // preconditions + assert(p_data != 00); + assert(box != 00); + assert(p_number_bytes_read != 00); + assert(p_manager != 00); -opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) { - opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t)); - if(jp2) { - jp2->cinfo = cinfo; - /* create the J2K codec */ - jp2->j2k = j2k_create_decompress(cinfo); - if(jp2->j2k == NULL) { - jp2_destroy_decompress(jp2); - return NULL; - } + if + (p_box_max_size < 8) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n"); + return false; } - return jp2; + /* process read data */ + opj_read_bytes(p_data,&(box->length), 4); + p_data += 4; + opj_read_bytes(p_data,&(box->type), 4); + p_data += 4; + *p_number_bytes_read = 8; + + // do we have a "special very large box ?" + // read then the XLBox + if + (box->length == 1) + { + unsigned int l_xl_part_size; + if + (p_box_max_size < 16) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n"); + return false; + } + + opj_read_bytes(p_data,&l_xl_part_size, 4); + p_data += 4; + *p_number_bytes_read += 4; + if + (l_xl_part_size != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n"); + return false; + } + opj_read_bytes(p_data,&(box->length), 4); + *p_number_bytes_read += 4; + if + (box->length == 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); + return false; + } + + } + else if + (box->length == 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n"); + return false; + } + return true; } -void jp2_destroy_decompress(opj_jp2_t *jp2) { - if(jp2) { - /* destroy the J2K codec */ - j2k_destroy_decompress(jp2->j2k); - if(jp2->comps) { - opj_free(jp2->comps); +/** + * Reads a jpeg2000 file signature box. + * + * @param p_header_data the data contained in the signature box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the signature box. + * @param p_manager the user event manager. + * + * @return true if the file signature box is valid. + */ +bool jp2_read_jp( + opj_jp2_t *jp2, + unsigned char * p_header_data, + unsigned int p_header_size, + opj_event_mgr_t * p_manager + ) +{ + unsigned int l_magic_number; + + // preconditions + assert(p_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + if + (jp2->jp2_state != JP2_STATE_NONE) + { + opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n"); + return false; + } + + + /* assure length of data is correct (4 -> magic number) */ + if + (p_header_size != 4) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n"); + return false; + } + + // rearrange data + opj_read_bytes(p_header_data,&l_magic_number,4); + if + (l_magic_number != 0x0d0a870a ) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n"); + return false; + } + jp2->jp2_state |= JP2_STATE_SIGNATURE; + return true; +} + +/** + * Reads a a FTYP box - File type box + * + * @param p_header_data the data contained in the FTYP box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the FTYP box. + * @param p_manager the user event manager. + * + * @return true if the FTYP box is valid. + */ +bool jp2_read_ftyp( + opj_jp2_t *jp2, + unsigned char * p_header_data, + unsigned int p_header_size, + opj_event_mgr_t * p_manager + ) +{ + unsigned int i; + unsigned int l_remaining_bytes; + + // preconditions + assert(p_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + if + (jp2->jp2_state != JP2_STATE_SIGNATURE) + { + opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n"); + return false; + } + + /* assure length of data is correct */ + if + (p_header_size < 8) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n"); + return false; + } + + opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */ + p_header_data += 4; + + opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */ + p_header_data += 4; + + l_remaining_bytes = p_header_size - 8; + + /* the number of remaining bytes should be a multiple of 4 */ + if + ((l_remaining_bytes & 0x3) != 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n"); + return false; + } + /* div by 4 */ + jp2->numcl = l_remaining_bytes >> 2; + if + (jp2->numcl) + { + jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int)); + if + (jp2->cl == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n"); + return false; } - if(jp2->cl) { + memset(jp2->cl,0,jp2->numcl * sizeof(unsigned int)); + } + + + for + (i = 0; i < jp2->numcl; ++i) + { + opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */ + p_header_data += 4; + + } + jp2->jp2_state |= JP2_STATE_FILE_TYPE; + return true; +} + +/** + * Writes a jpeg2000 file signature box. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager the user event manager. + * + * @return true if writting was successful. + */ +bool jp2_write_jp ( + opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + /* 12 bytes will be read */ + unsigned char l_signature_data [12]; + + // preconditions + assert(cio != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + + /* write box length */ + opj_write_bytes(l_signature_data,12,4); + /* writes box type */ + opj_write_bytes(l_signature_data+4,JP2_JP,4); + /* writes magic number*/ + opj_write_bytes(l_signature_data+8,0x0d0a870a,4); + if + (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) + { + return false; + } + return true; +} + + +/** + * Writes a FTYP box - File type box + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager the user event manager. + * + * @return true if writting was successful. + */ +bool jp2_write_ftyp( + opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + unsigned int i; + unsigned int l_ftyp_size = 16 + 4 * jp2->numcl; + unsigned char * l_ftyp_data, * l_current_data_ptr; + bool l_result; + + // preconditions + assert(cio != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + l_ftyp_data = (unsigned char *) opj_malloc(l_ftyp_size); + + if + (l_ftyp_data == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n"); + return false; + } + memset(l_ftyp_data,0,l_ftyp_size); + + l_current_data_ptr = l_ftyp_data; + + opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */ + l_current_data_ptr += 4; + + opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */ + l_current_data_ptr += 4; + + opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */ + l_current_data_ptr += 4; + + opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */ + l_current_data_ptr += 4; + + for + (i = 0; i < jp2->numcl; i++) + { + opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */ + } + + l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size); + if + (! l_result) + { + opj_event_msg(p_manager, EVT_ERROR, "Error while writting ftyp data to stream\n"); + } + opj_free(l_ftyp_data); + return l_result; +} + +/** + * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writting was successful. +*/ +bool jp2_write_jp2h( + opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + opj_jp2_img_header_writer_handler_t l_writers [3]; + opj_jp2_img_header_writer_handler_t * l_current_writer; + + int i, l_nb_pass; + /* size of data for super box*/ + int l_jp2h_size = 8; + bool l_result = true; + + /* to store the data of the super box */ + unsigned char l_jp2h_data [8]; + + // preconditions + assert(cio != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + memset(l_writers,0,sizeof(l_writers)); + + if + (jp2->bpc == 255) + { + l_nb_pass = 3; + l_writers[0].handler = jp2_write_ihdr; + l_writers[1].handler = jp2_write_bpcc; + l_writers[2].handler = jp2_write_colr; + } + else + { + l_nb_pass = 2; + l_writers[0].handler = jp2_write_ihdr; + l_writers[1].handler = jp2_write_colr; + } + + /* write box header */ + /* write JP2H type */ + opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4); + + l_current_writer = l_writers; + for + (i=0;im_data = l_current_writer->handler(jp2,&(l_current_writer->m_size)); + if + (l_current_writer->m_data == 00) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n"); + l_result = false; + break; + } + l_jp2h_size += l_current_writer->m_size; + ++l_current_writer; + } + + if + (! l_result) + { + l_current_writer = l_writers; + for + (i=0;im_data != 00) + { + opj_free(l_current_writer->m_data ); + } + ++l_current_writer; + } + return false; + } + + /* write super box size */ + opj_write_bytes(l_jp2h_data,l_jp2h_size,4); + + /* write super box data on stream */ + if + (opj_stream_write_data(cio,l_jp2h_data,8,p_manager) != 8) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n"); + l_result = false; + } + + if + (l_result) + { + l_current_writer = l_writers; + for + (i=0;im_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream error while writting JP2 Header box\n"); + l_result = false; + break; + } + ++l_current_writer; + } + } + l_current_writer = l_writers; + /* cleanup */ + for + (i=0;im_data != 00) + { + opj_free(l_current_writer->m_data ); + } + ++l_current_writer; + } + return l_result; +} + +/** + * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box). + * + * @param p_header_data the data contained in the file header box. + * @param jp2 the jpeg2000 file codec. + * @param p_header_size the size of the data contained in the file header box. + * @param p_manager the user event manager. + * + * @return true if the JP2 Header box was successfully reconized. +*/ +bool jp2_read_jp2h( + opj_jp2_t *jp2, + unsigned char * p_header_data, + unsigned int p_header_size, + opj_event_mgr_t * p_manager + ) +{ + unsigned int l_box_size=0, l_current_data_size = 0; + opj_jp2_box_t box; + const opj_jp2_header_handler_t * l_current_handler; + + // preconditions + assert(p_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + /* make sure the box is well placed */ + if + ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) + { + opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n"); + return false; + } + jp2->jp2_img_state = JP2_IMG_STATE_NONE; + + /* iterate while remaining data */ + while + (p_header_size > 0) + { + if + (! jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n"); + return false; + } + if + (box.length > p_header_size) + { + opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n"); + return false; + } + l_current_handler = jp2_img_find_handler(box.type); + + l_current_data_size = box.length - l_box_size; + p_header_data += l_box_size; + + if + (l_current_handler != 00) + { + if + (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) + { + return false; + } + } + else + { + jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN; + } + p_header_data += l_current_data_size; + p_header_size -= box.length; + } + jp2->jp2_state |= JP2_STATE_HEADER; + return true; +} + +/** + * Reads a IHDR box - Image Header box + * + * @param p_image_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_image_header_size the size of the image header + * @param p_image_header_max_size maximum size of the header, any size bigger than this value should result the function to output false. + * @param p_manager the user event manager. + * + * @return true if the image header is valid, fale else. + */ +bool jp2_read_ihdr( + opj_jp2_t *jp2, + unsigned char * p_image_header_data, + unsigned int p_image_header_size, + opj_event_mgr_t * p_manager + ) +{ + // preconditions + assert(p_image_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + if + (p_image_header_size != 14) + { + opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n"); + return false; + } + opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */ + p_image_header_data += 4; + opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */ + p_image_header_data += 4; + opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */ + p_image_header_data += 2; + + /* allocate memory for components */ + jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t)); + if + (jp2->comps == 0) + { + opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n"); + return false; + } + memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t)); + + opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */ + ++ p_image_header_data; + opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */ + ++ p_image_header_data; + opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */ + ++ p_image_header_data; + opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */ + ++ p_image_header_data; + return true; +} + +/** + * Writes the Image Header box - Image Header box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +static unsigned char * jp2_write_ihdr( + opj_jp2_t *jp2, + unsigned int * p_nb_bytes_written + ) +{ + unsigned char * l_ihdr_data,* l_current_ihdr_ptr; + + // preconditions + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + + /* default image header is 22 bytes wide */ + l_ihdr_data = (unsigned char *) opj_malloc(22); + if + (l_ihdr_data == 00) + { + return 00; + } + memset(l_ihdr_data,0,22); + + l_current_ihdr_ptr = l_ihdr_data; + + opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */ + l_current_ihdr_ptr+=4; + opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */ + l_current_ihdr_ptr+=4; + opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */ + l_current_ihdr_ptr+=4; + opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */ + l_current_ihdr_ptr+=4; + opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */ + l_current_ihdr_ptr+=2; + opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */ + ++l_current_ihdr_ptr; + opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */ + ++l_current_ihdr_ptr; + opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */ + ++l_current_ihdr_ptr; + opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */ + ++l_current_ihdr_ptr; + *p_nb_bytes_written = 22; + return l_ihdr_data; +} + +/** + * Writes the Bit per Component box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +unsigned char * jp2_write_bpcc( + opj_jp2_t *jp2, + unsigned int * p_nb_bytes_written + ) +{ + unsigned int i; + /* room for 8 bytes for box and 1 byte for each component */ + int l_bpcc_size = 8 + jp2->numcomps; + unsigned char * l_bpcc_data,* l_current_bpcc_ptr; + + // preconditions + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + + l_bpcc_data = (unsigned char *) opj_malloc(l_bpcc_size); + if + (l_bpcc_data == 00) + { + return 00; + } + memset(l_bpcc_data,0,l_bpcc_size); + + l_current_bpcc_ptr = l_bpcc_data; + + opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */ + l_current_bpcc_ptr += 4; + opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */ + l_current_bpcc_ptr += 4; + + for + (i = 0; i < jp2->numcomps; ++i) + { + opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */ + ++l_current_bpcc_ptr; + } + *p_nb_bytes_written = l_bpcc_size; + return l_bpcc_data; +} + +/** + * Reads a Bit per Component box. + * + * @param p_bpc_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_bpc_header_size pointer that will hold the size of the bpc header + * @param p_bpc_header_max_size maximum size of the header, any size bigger than this value should result the function to output false. + * @param p_manager the user event manager. + * + * @return true if the bpc header is valid, fale else. + */ +bool jp2_read_bpcc( + opj_jp2_t *jp2, + unsigned char * p_bpc_header_data, + unsigned int p_bpc_header_size, + opj_event_mgr_t * p_manager + ) +{ + unsigned int i; + + // preconditions + assert(p_bpc_header_data != 00); + assert(jp2 != 00); + assert(p_manager != 00); + + // and length is relevant + if + (p_bpc_header_size != jp2->numcomps) + { + opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n"); + return false; + } + + // read info for each component + for + (i = 0; i < jp2->numcomps; ++i) + { + opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */ + ++p_bpc_header_data; + } + return true; +} + +/** + * Writes the Colour Specification box. + * + * @param jp2 jpeg2000 file codec. + * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. + * + * @return the data being copied. +*/ +unsigned char *jp2_write_colr( + opj_jp2_t *jp2, + unsigned int * p_nb_bytes_written + ) +{ + /* room for 8 bytes for box 3 for common data and variable upon profile*/ + unsigned int l_colr_size = 11; + unsigned char * l_colr_data,* l_current_colr_ptr; + + // preconditions + assert(jp2 != 00); + assert(p_nb_bytes_written != 00); + + switch + (jp2->meth) + { + case 1 : + l_colr_size += 4; + break; + case 2 : + ++l_colr_size; + break; + default : + return 00; + } + + l_colr_data = (unsigned char *) opj_malloc(l_colr_size); + if + (l_colr_data == 00) + { + return 00; + } + memset(l_colr_data,0,l_colr_size); + l_current_colr_ptr = l_colr_data; + + opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */ + l_current_colr_ptr += 4; + opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */ + l_current_colr_ptr += 4; + + opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */ + ++l_current_colr_ptr; + opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */ + ++l_current_colr_ptr; + opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */ + ++l_current_colr_ptr; + + if + (jp2->meth == 1) + { + opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */ + } + else + { + opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */ + } + *p_nb_bytes_written = l_colr_size; + return l_colr_data; +} + +/** + * Reads the Colour Specification box. + * + * @param p_colr_header_data pointer to actual data (already read from file) + * @param jp2 the jpeg2000 file codec. + * @param p_colr_header_size pointer that will hold the size of the color header + * @param p_colr_header_max_size maximum size of the header, any size bigger than this value should result the function to output false. + * @param p_manager the user event manager. + * + * @return true if the bpc header is valid, fale else. +*/ +bool jp2_read_colr( + opj_jp2_t * jp2, + unsigned char * p_colr_header_data, + unsigned int p_colr_header_size, + opj_event_mgr_t * p_manager + ) +{ + // preconditions + assert(jp2 != 00); + assert(p_colr_header_data != 00); + assert(p_manager != 00); + + if + (p_colr_header_size < 3) + { + opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n"); + return false; + } + + opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */ + ++p_colr_header_data; + + opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */ + ++p_colr_header_data; + + opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */ + ++p_colr_header_data; + + + if + (jp2->meth == 1) + { + if + (p_colr_header_size != 7) + { + opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n"); + return false; + } + opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */ + } + /*else + { + // do not care with profiles. + }*/ + return true; +} + +/** + * Writes the Jpeg2000 codestream Header box - JP2C Header box. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writting was successful. +*/ +bool jp2_write_jp2c( + opj_jp2_t *jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + unsigned int j2k_codestream_exit; + unsigned char l_data_header [8]; + + // preconditions + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + assert(opj_stream_has_seek(cio)); + + j2k_codestream_exit = opj_stream_tell(cio); + opj_write_bytes(l_data_header,j2k_codestream_exit - jp2->j2k_codestream_offset,4); /* size of codestream */ + opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */ + + if + (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return false; + } + + if + (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) + { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return false; + } + + if + (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n"); + return false; + } + return true; +} + +/** + * Destroys a jpeg2000 file decompressor. + * + * @param jp2 a jpeg2000 file decompressor. + */ +void jp2_destroy(opj_jp2_t *jp2) +{ + if + (jp2) + { + /* destroy the J2K codec */ + j2k_destroy(jp2->j2k); + jp2->j2k = 00; + if + (jp2->comps) + { + opj_free(jp2->comps); + jp2->comps = 00; + } + if + (jp2->cl) + { opj_free(jp2->cl); + jp2->cl = 00; + } + if + (jp2->m_validation_list) + { + opj_procedure_list_destroy(jp2->m_validation_list); + jp2->m_validation_list = 00; + } + if + (jp2->m_procedure_list) + { + opj_procedure_list_destroy(jp2->m_procedure_list); + jp2->m_procedure_list = 00; } opj_free(jp2); } } -void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) { - /* setup the J2K codec */ - j2k_setup_decoder(jp2->j2k, parameters); - /* further JP2 initializations go here */ + + + + +/* ----------------------------------------------------------------------- */ +/* JP2 encoder interface */ +/* ----------------------------------------------------------------------- */ + +opj_jp2_t* jp2_create(bool p_is_decoder) +{ + opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t)); + if + (jp2) + { + memset(jp2,0,sizeof(opj_jp2_t)); + /* create the J2K codec */ + if + (! p_is_decoder) + { + jp2->j2k = j2k_create_compress(); + } + else + { + jp2->j2k = j2k_create_decompress(); + } + if + (jp2->j2k == 00) + { + jp2_destroy(jp2); + return 00; + } + // validation list creation + jp2->m_validation_list = opj_procedure_list_create(); + if + (! jp2->m_validation_list) + { + jp2_destroy(jp2); + return 00; + } + + // execution list creation + jp2->m_procedure_list = opj_procedure_list_create(); + if + (! jp2->m_procedure_list) + { + jp2_destroy(jp2); + return 00; + } + } + return jp2; } -opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *cstr_info) { - opj_common_ptr cinfo; - opj_image_t *image = NULL; +/** + * Excutes the given procedures on the given codec. + * + * @param p_procedure_list the list of procedures to execute + * @param jp2 the jpeg2000 file codec to execute the procedures on. + * @param cio the stream to execute the procedures on. + * @param p_manager the user manager. + * + * @return true if all the procedures were successfully executed. + */ +bool jp2_exec ( + opj_jp2_t * jp2, + opj_procedure_list_t * p_procedure_list, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + bool (** l_procedure) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *) = 00; + bool l_result = true; + unsigned int l_nb_proc, i; - if(!jp2 || !cio) { - return NULL; + // preconditions + assert(p_procedure_list != 00); + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list); + l_procedure = (bool (**) (opj_jp2_t * jp2,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list); + for + (i=0;im_validation_list,cio,p_manager)) + { + return false; } - cinfo = jp2->cinfo; + /* customization of the encoding */ + jp2_setup_header_writting(jp2); - /* JP2 decoding */ - if(!jp2_read_struct(jp2, cio)) { - opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n"); - return NULL; + /* write header */ + if + (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) + { + return false; + } + return j2k_start_compress(jp2->j2k,cio,p_image,p_manager); +} + +/** + * Reads a jpeg2000 file header structure. + * + * @param cio the stream to read data from. + * @param jp2 the jpeg2000 file header structure. + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +bool jp2_read_header( + opj_jp2_t *jp2, + opj_image_t ** p_image, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + /* customization of the validation */ + jp2_setup_decoding_validation (jp2); + + /* customization of the encoding */ + jp2_setup_header_reading(jp2); + + /* validation of the parameters codec */ + if + (! jp2_exec(jp2,jp2->m_validation_list,cio,p_manager)) + { + return false; } + /* read header */ + if + (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) + { + return false; + } + return j2k_read_header( + jp2->j2k, + p_image, + p_tile_x0, + p_tile_y0, + p_tile_width, + p_tile_height, + p_nb_tiles_x, + p_nb_tiles_y, + cio, + p_manager); +} + +/** + * Ends the decompression procedures and possibiliy add data to be read after the + * codestream. + */ +bool jp2_end_decompress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager) +{ + // preconditions + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + /* customization of the end encoding */ + jp2_setup_end_header_reading(jp2); + + /* write header */ + if + (! jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) + { + return false; + } + return j2k_end_decompress(jp2->j2k, cio, p_manager); +} + + +/** + * Ends the compression procedures and possibiliy add data to be read after the + * codestream. + */ +bool jp2_end_compress(opj_jp2_t *jp2, opj_stream_private_t *cio, opj_event_mgr_t * p_manager) +{ + // preconditions + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + /* customization of the end encoding */ + jp2_setup_end_header_writting(jp2); + + if + (! j2k_end_compress(jp2->j2k,cio,p_manager)) + { + return false; + } + /* write header */ + return jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager); +} + +/** +Encode an image into a JPEG-2000 file stream +@param jp2 JP2 compressor handle +@param cio Output buffer stream +@param image Image to encode +@param cstr_info Codestream information structure if required, NULL otherwise +@return Returns true if successful, returns false otherwise +*/ +bool jp2_encode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager) +{ + return j2k_encode(jp2->j2k,cio,p_manager); +} +/** + * Writes a tile. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool jp2_write_tile ( + opj_jp2_t *p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ) +{ + return j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager); +} + +/** + * Decode tile data. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool jp2_decode_tile ( + opj_jp2_t * p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + return j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager); +} +/** + * Reads a tile header. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool jp2_read_tile_header ( + opj_jp2_t * p_jp2, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_go_on, + opj_stream_private_t *p_stream, + opj_event_mgr_t * p_manager + ) +{ + return j2k_read_tile_header (p_jp2->j2k, + p_tile_index, + p_data_size, + p_tile_x0, + p_tile_y0, + p_tile_x1, + p_tile_y1, + p_nb_comps, + p_go_on, + p_stream, + p_manager); +} + +/** + * Sets up the procedures to do on writting header after the codestream. + * Developpers wanting to extend the library can add their own writting procedures. + */ +void jp2_setup_end_header_writting (opj_jp2_t *jp2) +{ + // preconditions + assert(jp2 != 00); + + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp2c ); + /* DEVELOPER CORNER, add your custom procedures */ +} + +/** + * Sets up the procedures to do on reading header. + * Developpers wanting to extend the library can add their own writting procedures. + */ +void jp2_setup_header_reading (opj_jp2_t *jp2) +{ + // preconditions + assert(jp2 != 00); + + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_read_header_procedure ); + /* DEVELOPER CORNER, add your custom procedures */ +} + +/** + * Sets up the procedures to do on reading header after the codestream. + * Developpers wanting to extend the library can add their own writting procedures. + */ +void jp2_setup_end_header_reading (opj_jp2_t *jp2) +{ + // preconditions + assert(jp2 != 00); + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_read_header_procedure ); + /* DEVELOPER CORNER, add your custom procedures */ +} + + +/** + * The default validation procedure without any extension. + * + * @param jp2 the jpeg2000 codec to validate. + * @param cio the input stream to validate. + * @param p_manager the user event manager. + * + * @return true if the parameters are correct. + */ +bool jp2_default_validation ( + opj_jp2_t * jp2, + opj_stream_private_t *cio, + opj_event_mgr_t * p_manager + ) +{ + bool l_is_valid = true; + unsigned int i; + + // preconditions + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + /* JPEG2000 codec validation */ + /*TODO*/ + + /* STATE checking */ + /* make sure the state is at 0 */ + l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE); + /* make sure not reading a jp2h ???? WEIRD */ + l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE); + + /* POINTER validation */ + /* make sure a j2k codec is present */ + l_is_valid &= (jp2->j2k != 00); + /* make sure a procedure list is present */ + l_is_valid &= (jp2->m_procedure_list != 00); + /* make sure a validation list is present */ + l_is_valid &= (jp2->m_validation_list != 00); + + /* PARAMETER VALIDATION */ + /* number of components */ + l_is_valid &= (jp2->numcl > 0); + /* width */ + l_is_valid &= (jp2->h > 0); + /* height */ + l_is_valid &= (jp2->w > 0); + /* precision */ + for + (i = 0; i < jp2->numcomps; ++i) + { + l_is_valid &= (jp2->comps[i].bpcc > 0); + } + /* METH */ + l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3)); + + + + /* stream validation */ + /* back and forth is needed */ + l_is_valid &= opj_stream_has_seek(cio); + + return l_is_valid; + +} + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +void jp2_setup_encoding_validation (opj_jp2_t *jp2) +{ + // preconditions + assert(jp2 != 00); + opj_procedure_list_add_procedure(jp2->m_validation_list, jp2_default_validation); + /* DEVELOPER CORNER, add your custom validation procedure */ +} + +/** + * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters + * are valid. Developpers wanting to extend the library can add their own validation procedures. + */ +void jp2_setup_decoding_validation (opj_jp2_t *jp2) +{ + // preconditions + assert(jp2 != 00); + /* DEVELOPER CORNER, add your custom validation procedure */ +} + +/** + * Sets up the procedures to do on writting header. Developpers wanting to extend the library can add their own writting procedures. + */ +void jp2_setup_header_writting (opj_jp2_t *jp2) +{ + // preconditions + assert(jp2 != 00); + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp ); + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_ftyp ); + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_write_jp2h ); + opj_procedure_list_add_procedure(jp2->m_procedure_list,jp2_skip_jp2c ); + + /* DEVELOPER CORNER, insert your custom procedures */ + +} + + +/** + * Skips the Jpeg2000 Codestream Header box - JP2C Header box. + * + * @param cio the stream to write data to. + * @param jp2 the jpeg2000 file codec. + * @param p_manager user event manager. + * + * @return true if writting was successful. +*/ +bool jp2_skip_jp2c( + opj_jp2_t *jp2, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ) +{ + // preconditions + assert(jp2 != 00); + assert(cio != 00); + assert(p_manager != 00); + + jp2->j2k_codestream_offset = opj_stream_tell(cio); + if + (opj_stream_skip(cio,8,p_manager) != 8) + { + return false; + } + return true; +} + +struct opj_image * jp2_decode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager) +{ /* J2K decoding */ - image = j2k_decode(jp2->j2k, cio, cstr_info); - if(!image) { - opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n"); - return NULL; + struct opj_image * image = j2k_decode(jp2->j2k, cio, p_manager); + if + (!image) + { + opj_event_msg(p_manager, EVT_ERROR, "Failed to decode J2K image\n"); + return false; } /* Set Image Color Space */ @@ -573,45 +2086,12 @@ opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *c image->color_space = CLRSPC_SYCC; else image->color_space = CLRSPC_UNKNOWN; - return image; } -/* ----------------------------------------------------------------------- */ -/* JP2 encoder interface */ -/* ----------------------------------------------------------------------- */ - -opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) { - opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t)); - if(jp2) { - jp2->cinfo = cinfo; - /* create the J2K codec */ - jp2->j2k = j2k_create_compress(cinfo); - if(jp2->j2k == NULL) { - jp2_destroy_compress(jp2); - return NULL; - } - } - return jp2; -} - -void jp2_destroy_compress(opj_jp2_t *jp2) { - if(jp2) { - /* destroy the J2K codec */ - j2k_destroy_compress(jp2->j2k); - - if(jp2->comps) { - opj_free(jp2->comps); - } - if(jp2->cl) { - opj_free(jp2->cl); - } - opj_free(jp2); - } -} - -void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) { - int i; +void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image,opj_event_mgr_t * p_manager) +{ + unsigned int i; int depth_0, sign; if(!jp2 || !parameters || !image) @@ -622,11 +2102,11 @@ void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_ /* Check if number of components respects standard */ if (image->numcomps < 1 || image->numcomps > 16384) { - opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n"); + opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n"); return; } - j2k_setup_encoder(jp2->j2k, parameters, image); + j2k_setup_encoder(jp2->j2k, parameters, image,p_manager); /* setup the JP2 codec */ /* ------------------- */ @@ -687,25 +2167,63 @@ void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_ } -bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { +void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) +{ + if(!jp2 || !parameters) + return; - /* JP2 encoding */ - - /* JPEG 2000 Signature box */ - jp2_write_jp(cio); - /* File Type box */ - jp2_write_ftyp(jp2, cio); - /* JP2 Header box */ - jp2_write_jp2h(jp2, cio); - - /* J2K encoding */ - - if(!jp2_write_jp2c(jp2, cio, image, cstr_info)) { - opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n"); - return false; - } - - return true; + /* setup the J2K codec */ + /* ------------------- */ + j2k_setup_decoder(jp2->j2k, parameters); +} +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_jp2 the jpeg2000 codec. + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * @param p_manager the user event manager + * + * @return true if the area could be set. + */ +bool jp2_set_decode_area( + opj_jp2_t *p_jp2, + OPJ_INT32 p_start_x, + OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, + OPJ_INT32 p_end_y, + struct opj_event_mgr * p_manager + ) +{ + return j2k_set_decode_area(p_jp2->j2k,p_start_x,p_start_y,p_end_x,p_end_y,p_manager); } +#if 0 + + + + +static void jp2_write_url(opj_cio_t *cio, char *Idx_file) { + unsigned int i; + opj_jp2_box_t box; + + box.init_pos = cio_tell(cio); + cio_skip(cio, 4); + cio_write(cio, JP2_URL, 4); /* DBTL */ + cio_write(cio, 0, 1); /* VERS */ + cio_write(cio, 0, 3); /* FLAG */ + + if(Idx_file) { + for (i = 0; i < strlen(Idx_file); i++) { + cio_write(cio, Idx_file[i], 1); + } + } + + box.length = cio_tell(cio) - box.init_pos; + cio_seek(cio, box.init_pos); + cio_write(cio, box.length, 4); /* L */ + cio_seek(cio, box.init_pos + box.length); +} +#endif diff --git a/libopenjpeg/jp2.h b/libopenjpeg/jp2.h index 7e363be2..e98cde39 100644 --- a/libopenjpeg/jp2.h +++ b/libopenjpeg/jp2.h @@ -3,6 +3,7 @@ * Copyright (c) 2002-2007, Professor Benoit Macq * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,6 +34,20 @@ @brief The JPEG-2000 file format Reader/Writer (JP2) */ +#include "openjpeg.h" + + + + +/********************************************************************************** + ********************************* FORWARD DECLARATIONS *************************** + **********************************************************************************/ +struct opj_j2k; +struct opj_procedure_list; +struct opj_event_mgr; +struct opj_stream_private; +struct opj_dparameters; +struct opj_cparameters; /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */ /*@{*/ @@ -52,25 +67,54 @@ /* ----------------------------------------------------------------------- */ + +typedef enum +{ + JP2_STATE_NONE = 0x0, + JP2_STATE_SIGNATURE = 0x1, + JP2_STATE_FILE_TYPE = 0x2, + JP2_STATE_HEADER = 0x4, + JP2_STATE_CODESTREAM = 0x8, + JP2_STATE_END_CODESTREAM = 0x10, + JP2_STATE_UNKNOWN = 0x80000000 +} +JP2_STATE; + +typedef enum +{ + JP2_IMG_STATE_NONE = 0x0, + JP2_IMG_STATE_UNKNOWN = 0x80000000 +} +JP2_IMG_STATE; + /** JP2 component */ -typedef struct opj_jp2_comps { - int depth; +typedef struct opj_jp2_comps +{ + unsigned int depth; int sgnd; - int bpcc; -} opj_jp2_comps_t; + unsigned int bpcc; +} +opj_jp2_comps_t; /** JPEG-2000 file format reader/writer */ -typedef struct opj_jp2 { - /** codec context */ - opj_common_ptr cinfo; +typedef struct opj_jp2 +{ /** handle to the J2K codec */ - opj_j2k_t *j2k; + struct opj_j2k *j2k; + /** list of validation procedures */ + struct opj_procedure_list * m_validation_list; + /** list of execution procedures */ + struct opj_procedure_list * m_procedure_list; + + /* width of image */ unsigned int w; + /* height of image */ unsigned int h; + /* number of components in the image */ unsigned int numcomps; unsigned int bpc; unsigned int C; @@ -86,71 +130,80 @@ typedef struct opj_jp2 { unsigned int *cl; opj_jp2_comps_t *comps; unsigned int j2k_codestream_offset; - unsigned int j2k_codestream_length; -} opj_jp2_t; + unsigned int jp2_state; + unsigned int jp2_img_state; + +} +opj_jp2_t; /** JP2 Box */ -typedef struct opj_jp2_box { - int length; - int type; - int init_pos; -} opj_jp2_box_t; +typedef struct opj_jp2_box +{ + unsigned int length; + unsigned int type; +} +opj_jp2_box_t; + +typedef struct opj_jp2_header_handler +{ + /* marker value */ + int id; + /* action linked to the marker */ + bool (*handler) (opj_jp2_t *jp2,unsigned char * p_header_data, unsigned int p_header_size,struct opj_event_mgr * p_manager); +} +opj_jp2_header_handler_t; + + +typedef struct opj_jp2_img_header_writer_handler +{ + /* action to perform */ + unsigned char* (*handler) (opj_jp2_t *jp2, unsigned int * p_data_size); + /* result of the action : data */ + unsigned char * m_data; + /* size of data */ + unsigned int m_size; +} +opj_jp2_img_header_writer_handler_t; + + + + /** @name Exported functions */ /*@{*/ /* ----------------------------------------------------------------------- */ + /** -Write the JP2H box - JP2 Header box (used in MJ2) -@param jp2 JP2 handle -@param cio Output buffer stream -*/ -void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio); -/** -Read the JP2H box - JP2 Header box (used in MJ2) -@param jp2 JP2 handle -@param cio Input buffer stream -@return Returns true if successful, returns false otherwise -*/ -bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio); -/** -Creates a JP2 decompression structure -@param cinfo Codec context info -@return Returns a handle to a JP2 decompressor if successful, returns NULL otherwise -*/ -opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo); + * Creates a jpeg2000 file decompressor. + * + * @return an empty jpeg2000 file codec. + */ +opj_jp2_t* jp2_create (bool p_is_decoder); + /** Destroy a JP2 decompressor handle @param jp2 JP2 decompressor handle to destroy */ -void jp2_destroy_decompress(opj_jp2_t *jp2); +void jp2_destroy(opj_jp2_t *jp2); + /** Setup the decoder decoding parameters using user parameters. Decoding parameters are returned in jp2->j2k->cp. @param jp2 JP2 decompressor handle @param parameters decompression parameters */ -void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters); +void jp2_setup_decoder(opj_jp2_t *jp2, struct opj_dparameters *parameters); + /** -Decode an image from a JPEG-2000 file stream -@param jp2 JP2 decompressor handle -@param cio Input buffer stream -@param cstr_info Codestream information structure if required, NULL otherwise -@return Returns a decoded image if successful, returns NULL otherwise + * Decode an image from a JPEG-2000 file stream + * @param jp2 JP2 decompressor handle + * @param cio Input buffer stream + * @param cstr_info Codestream information structure if required, NULL otherwise + * @return Returns a decoded image if successful, returns NULL otherwise */ -opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio, opj_codestream_info_t *cstr_info); -/** -Creates a JP2 compression structure -@param cinfo Codec context info -@return Returns a handle to a JP2 compressor if successful, returns NULL otherwise -*/ -opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo); -/** -Destroy a JP2 compressor handle -@param jp2 JP2 compressor handle to destroy -*/ -void jp2_destroy_compress(opj_jp2_t *jp2); +struct opj_image* jp2_decode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); /** Setup the encoder parameters using the current image and using user parameters. Coding parameters are returned in jp2->j2k->cp. @@ -158,7 +211,24 @@ Coding parameters are returned in jp2->j2k->cp. @param parameters compression parameters @param image input filled image */ -void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image); +void jp2_setup_encoder(opj_jp2_t *jp2, struct opj_cparameters *parameters, struct opj_image *image,struct opj_event_mgr * p_manager); + +/** + * Starts a compression scheme, i.e. validates the codec parameters, writes the header. + * + * @param jp2 the jpeg2000 file codec. + * @param cio the stream object. + * + * @return true if the codec is valid. + */ +bool jp2_start_compress(opj_jp2_t *jp2, struct opj_stream_private *cio,struct opj_image * p_image,struct opj_event_mgr * p_manager); + +/** + * Ends the compression procedures and possibiliy add data to be read after the + * codestream. + */ +bool jp2_end_compress(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); + /** Encode an image into a JPEG-2000 file stream @param jp2 JP2 compressor handle @@ -167,7 +237,102 @@ Encode an image into a JPEG-2000 file stream @param cstr_info Codestream information structure if required, NULL otherwise @return Returns true if successful, returns false otherwise */ -bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info); +bool jp2_encode(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); + +/** + * Reads a jpeg2000 file header structure. + * + * @param cio the stream to read data from. + * @param jp2 the jpeg2000 file header structure. + * @param p_manager the user event manager. + * + * @return true if the box is valid. + */ +bool jp2_read_header( + opj_jp2_t *jp2, + struct opj_image ** p_image, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager + ); +/** + * Ends the decompression procedures and possibiliy add data to be read after the + * codestream. + */ +bool jp2_end_decompress(opj_jp2_t *jp2, struct opj_stream_private *cio, struct opj_event_mgr * p_manager); + +/** + * Writes a tile. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool jp2_write_tile ( + opj_jp2_t *p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Decode tile data. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool jp2_decode_tile ( + opj_jp2_t * p_jp2, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Reads a tile header. + * @param p_j2k the jpeg2000 codec. + * @param p_stream the stream to write data to. + * @param p_manager the user event manager. + */ +bool jp2_read_tile_header ( + opj_jp2_t * p_j2k, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_go_on, + struct opj_stream_private *p_stream, + struct opj_event_mgr * p_manager + ); +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_jp2 the jpeg2000 codec. + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * @param p_manager the user event manager + * + * @return true if the area could be set. + */ +bool jp2_set_decode_area( + opj_jp2_t *p_jp2, + OPJ_INT32 p_start_x, + OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, + OPJ_INT32 p_end_y, + struct opj_event_mgr * p_manager + ); + /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/jpt.c b/libopenjpeg/jpt.c index a2566ea8..dcbafbd9 100644 --- a/libopenjpeg/jpt.c +++ b/libopenjpeg/jpt.c @@ -3,6 +3,7 @@ * Copyright (c) 2002-2007, Professor Benoit Macq * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,33 +28,61 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" - +#include "jpt.h" +#include "openjpeg.h" +#include "cio.h" +#include "event.h" /* * Read the information contains in VBAS [JPP/JPT stream message header] * Store information (7 bits) in value - * + * @param p_cio the stream to read from. + * @param p_value the data to update + * @return the nb of bytes read or -1 if an io error occurred. */ -unsigned int jpt_read_VBAS_info(opj_cio_t *cio, unsigned int value) { - unsigned char elmt; +bool jpt_read_VBAS_info(opj_stream_private_t * p_cio, OPJ_UINT32 * p_nb_bytes_read, OPJ_UINT32 * p_value, opj_event_mgr_t * p_manager) +{ + OPJ_BYTE l_elmt; + OPJ_UINT32 l_nb_bytes_read = 0; + + // read data till the MSB of the current byte is 1. + // concatenate 7 bits of data, last bit is finish flag - elmt = cio_read(cio, 1); - while ((elmt >> 7) == 1) { - value = (value << 7); - value |= (elmt & 0x7f); - elmt = cio_read(cio, 1); + // read data from the stream + + if + (opj_stream_read_data(p_cio,&l_elmt,1,p_manager) != 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data.\n"); + return false; } - value = (value << 7); - value |= (elmt & 0x7f); + ++l_nb_bytes_read; - return value; + // is the MSB equal to 1 ? + while + (l_elmt & 0x80) + { + // concatenate 7 bits of data, last bit is finish flag + *p_value = (*p_value << 7) | (l_elmt & 0x7f); + if + (opj_stream_read_data(p_cio,&l_elmt,1,p_manager) != 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data.\n"); + return false; + } + ++l_nb_bytes_read; + } + // concatenate 7 bits of data, last bit is finish flag + *p_value = (*p_value << 7) | (l_elmt & 0x7f); + * p_nb_bytes_read = l_nb_bytes_read; + return true; } /* * Initialize the value of the message header structure * */ -void jpt_init_msg_header(opj_jpt_msg_header_t * header) { +void jpt_init_msg_header(opj_jpt_msg_header_t * header) +{ header->Id = 0; /* In-class Identifier */ header->last_byte = 0; /* Last byte information */ header->Class_Id = 0; /* Class Identifier */ @@ -69,7 +98,8 @@ void jpt_init_msg_header(opj_jpt_msg_header_t * header) { * Only parameters always present in message header * */ -void jpt_reinit_msg_header(opj_jpt_msg_header_t * header) { +void jpt_reinit_msg_header(opj_jpt_msg_header_t * header) +{ header->Id = 0; /* In-class Identifier */ header->last_byte = 0; /* Last byte information */ header->Msg_offset = 0; /* Message offset */ @@ -80,19 +110,31 @@ void jpt_reinit_msg_header(opj_jpt_msg_header_t * header) { * Read the message header for a JPP/JPT - stream * */ -void jpt_read_msg_header(opj_common_ptr cinfo, opj_cio_t *cio, opj_jpt_msg_header_t *header) { - unsigned char elmt, Class = 0, CSn = 0; - jpt_reinit_msg_header(header); +bool jpt_read_msg_header(opj_stream_private_t *cio, opj_jpt_msg_header_t *header, OPJ_UINT32 * p_nb_bytes_read, opj_event_mgr_t * p_manager) +{ + OPJ_BYTE elmt, Class = 0, CSn = 0; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_last_nb_bytes_read; + + jpt_reinit_msg_header(header); + /* ------------- */ /* VBAS : Bin-ID */ /* ------------- */ - elmt = cio_read(cio, 1); + if + (opj_stream_read_data(cio,&elmt,1,p_manager) != 1) + { + opj_event_msg(p_manager, EVT_ERROR, "Forbidden value encounter in message header !!\n"); + return false; + } + ++l_nb_bytes_read; /* See for Class and CSn */ - switch ((elmt >> 5) & 0x03) { + switch ((elmt >> 5) & 0x03) + { case 0: - opj_event_msg(cinfo, EVT_ERROR, "Forbidden value encounter in message header !!\n"); + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); break; case 1: Class = 0; @@ -111,45 +153,97 @@ void jpt_read_msg_header(opj_common_ptr cinfo, opj_cio_t *cio, opj_jpt_msg_heade } /* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */ - if (((elmt >> 4) & 0x01) == 1) + if + (((elmt >> 4) & 0x01) == 1) + { header->last_byte = 1; + } /* In-class identifier */ header->Id |= (elmt & 0x0f); - if ((elmt >> 7) == 1) - header->Id = jpt_read_VBAS_info(cio, header->Id); + if + ((elmt >> 7) == 1) + { + l_last_nb_bytes_read = 0; + if + (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Id), p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); + return false; + } + l_nb_bytes_read += l_last_nb_bytes_read; + } /* ------------ */ /* VBAS : Class */ /* ------------ */ - if (Class == 1) { + if (Class == 1) + { header->Class_Id = 0; - header->Class_Id = jpt_read_VBAS_info(cio, header->Class_Id); + l_last_nb_bytes_read = 0; + if + (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Class_Id), p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); + return false; + } + l_nb_bytes_read += l_last_nb_bytes_read; } /* ---------- */ /* VBAS : CSn */ /* ---------- */ - if (CSn == 1) { + if (CSn == 1) + { header->CSn_Id = 0; - header->CSn_Id = jpt_read_VBAS_info(cio, header->CSn_Id); + l_last_nb_bytes_read = 0; + if + (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->CSn_Id), p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); + return false; + } + l_nb_bytes_read += l_last_nb_bytes_read; } /* ----------------- */ /* VBAS : Msg_offset */ /* ----------------- */ - header->Msg_offset = jpt_read_VBAS_info(cio, header->Msg_offset); + l_last_nb_bytes_read = 0; + if + (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Msg_offset), p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); + return false; + } + l_nb_bytes_read += l_last_nb_bytes_read; /* ----------------- */ /* VBAS : Msg_length */ /* ----------------- */ - header->Msg_length = jpt_read_VBAS_info(cio, header->Msg_length); + l_last_nb_bytes_read = 0; + if + (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Msg_length), p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); + return false; + } + l_nb_bytes_read += l_last_nb_bytes_read; /* ---------- */ /* VBAS : Aux */ /* ---------- */ - if ((header->Class_Id & 0x01) == 1) { + if ((header->Class_Id & 0x01) == 1) + { header->Layer_nb = 0; - header->Layer_nb = jpt_read_VBAS_info(cio, header->Layer_nb); + if + (! jpt_read_VBAS_info(cio, &l_last_nb_bytes_read, &(header->Layer_nb), p_manager)) + { + opj_event_msg(p_manager, EVT_ERROR, "Error trying to read a byte of data!!!\n"); + return false; + } + l_nb_bytes_read += l_last_nb_bytes_read; } + * p_nb_bytes_read = l_nb_bytes_read; + return true; } diff --git a/libopenjpeg/jpt.h b/libopenjpeg/jpt.h index eb01f98e..22af2c2a 100644 --- a/libopenjpeg/jpt.h +++ b/libopenjpeg/jpt.h @@ -3,6 +3,7 @@ * Copyright (c) 2002-2007, Professor Benoit Macq * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,25 +36,28 @@ JPT-stream functions are implemented in J2K.C. */ - +#include "openjpeg.h" +struct opj_stream_private; +struct opj_event_mgr; /** Message Header JPT stream structure */ -typedef struct opj_jpt_msg_header { +typedef struct opj_jpt_msg_header +{ /** In-class Identifier */ - unsigned int Id; + OPJ_UINT32 Id; /** Last byte information */ - unsigned int last_byte; + OPJ_UINT32 last_byte; /** Class Identifier */ - unsigned int Class_Id; + OPJ_UINT32 Class_Id; /** CSn : index identifier */ - unsigned int CSn_Id; + OPJ_UINT32 CSn_Id; /** Message offset */ - unsigned int Msg_offset; + OPJ_UINT32 Msg_offset; /** Message length */ - unsigned int Msg_length; + OPJ_UINT32 Msg_length; /** Auxiliary for JPP case */ - unsigned int Layer_nb; + OPJ_UINT32 Layer_nb; } opj_jpt_msg_header_t; /* ----------------------------------------------------------------------- */ @@ -65,11 +69,15 @@ Initialize the value of the message header structure void jpt_init_msg_header(opj_jpt_msg_header_t * header); /** -Read the message header for a JPP/JPT - stream -@param cinfo Codec context info -@param cio CIO handle -@param header Message header structure + * Read the message header for a JPP/JPT - stream + * @param p_cio stream handle + * @param header JPT Message header structure + * @param p_manager user event manager to display nice messages. */ -void jpt_read_msg_header(opj_common_ptr cinfo, opj_cio_t *cio, opj_jpt_msg_header_t *header); +bool jpt_read_msg_header( + struct opj_stream_private * p_cio, + opj_jpt_msg_header_t * p_header, + OPJ_UINT32 * p_nb_bytes_read, + struct opj_event_mgr * p_manager); #endif diff --git a/libopenjpeg/mct.c b/libopenjpeg/mct.c index ca21744f..1c7565de 100644 --- a/libopenjpeg/mct.c +++ b/libopenjpeg/mct.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,35 +30,51 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" +#include "mct.h" +#include "fix.h" +#include "opj_malloc.h" /* */ /* This table contains the norms of the basis function of the reversible MCT. */ /* */ -static const double mct_norms[3] = { 1.732, .8292, .8292 }; +static const OPJ_FLOAT64 mct_norms[3] = { 1.732, .8292, .8292 }; /* */ /* This table contains the norms of the basis function of the irreversible MCT. */ /* */ -static const double mct_norms_real[3] = { 1.732, 1.805, 1.573 }; +static const OPJ_FLOAT64 mct_norms_real[3] = { 1.732, 1.805, 1.573 }; + + + +const OPJ_FLOAT64 * get_mct_norms () +{ + return mct_norms; +} + +const OPJ_FLOAT64 * get_mct_norms_real () +{ + return mct_norms_real; +} + + /* */ /* Foward reversible MCT. */ /* */ void mct_encode( - int* restrict c0, - int* restrict c1, - int* restrict c2, - int n) + OPJ_INT32* restrict c0, + OPJ_INT32* restrict c1, + OPJ_INT32* restrict c2, + OPJ_UINT32 n) { - int i; + OPJ_UINT32 i; for(i = 0; i < n; ++i) { - int r = c0[i]; - int g = c1[i]; - int b = c2[i]; - int y = (r + (g * 2) + b) >> 2; - int u = b - g; - int v = r - g; + OPJ_INT32 r = c0[i]; + OPJ_INT32 g = c1[i]; + OPJ_INT32 b = c2[i]; + OPJ_INT32 y = (r + (g * 2) + b) >> 2; + OPJ_INT32 u = b - g; + OPJ_INT32 v = r - g; c0[i] = y; c1[i] = u; c2[i] = v; @@ -68,19 +85,19 @@ void mct_encode( /* Inverse reversible MCT. */ /* */ void mct_decode( - int* restrict c0, - int* restrict c1, - int* restrict c2, - int n) + OPJ_INT32* restrict c0, + OPJ_INT32* restrict c1, + OPJ_INT32* restrict c2, + OPJ_UINT32 n) { - int i; + OPJ_UINT32 i; for (i = 0; i < n; ++i) { - int y = c0[i]; - int u = c1[i]; - int v = c2[i]; - int g = y - ((u + v) >> 2); - int r = v + g; - int b = u + g; + OPJ_INT32 y = c0[i]; + OPJ_INT32 u = c1[i]; + OPJ_INT32 v = c2[i]; + OPJ_INT32 g = y - ((u + v) >> 2); + OPJ_INT32 r = v + g; + OPJ_INT32 b = u + g; c0[i] = r; c1[i] = g; c2[i] = b; @@ -90,7 +107,7 @@ void mct_decode( /* */ /* Get norm of basis function of reversible MCT. */ /* */ -double mct_getnorm(int compno) { +OPJ_FLOAT64 mct_getnorm(OPJ_UINT32 compno) { return mct_norms[compno]; } @@ -98,19 +115,19 @@ double mct_getnorm(int compno) { /* Foward irreversible MCT. */ /* */ void mct_encode_real( - int* restrict c0, - int* restrict c1, - int* restrict c2, - int n) + OPJ_INT32* restrict c0, + OPJ_INT32* restrict c1, + OPJ_INT32* restrict c2, + OPJ_UINT32 n) { - int i; + OPJ_UINT32 i; for(i = 0; i < n; ++i) { - int r = c0[i]; - int g = c1[i]; - int b = c2[i]; - int y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934); - int u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096); - int v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666); + OPJ_INT32 r = c0[i]; + OPJ_INT32 g = c1[i]; + OPJ_INT32 b = c2[i]; + OPJ_INT32 y = fix_mul(r, 2449) + fix_mul(g, 4809) + fix_mul(b, 934); + OPJ_INT32 u = -fix_mul(r, 1382) - fix_mul(g, 2714) + fix_mul(b, 4096); + OPJ_INT32 v = fix_mul(r, 4096) - fix_mul(g, 3430) - fix_mul(b, 666); c0[i] = y; c1[i] = u; c2[i] = v; @@ -121,19 +138,19 @@ void mct_encode_real( /* Inverse irreversible MCT. */ /* */ void mct_decode_real( - float* restrict c0, - float* restrict c1, - float* restrict c2, - int n) + OPJ_FLOAT32* restrict c0, + OPJ_FLOAT32* restrict c1, + OPJ_FLOAT32* restrict c2, + OPJ_UINT32 n) { - int i; + OPJ_UINT32 i; for(i = 0; i < n; ++i) { - float y = c0[i]; - float u = c1[i]; - float v = c2[i]; - float r = y + (v * 1.402f); - float g = y - (u * 0.34413f) - (v * (0.71414f)); - float b = y + (u * 1.772f); + OPJ_FLOAT32 y = c0[i]; + OPJ_FLOAT32 u = c1[i]; + OPJ_FLOAT32 v = c2[i]; + OPJ_FLOAT32 r = y + (v * 1.402f); + OPJ_FLOAT32 g = y - (u * 0.34413f) - (v * (0.71414f)); + OPJ_FLOAT32 b = y + (u * 1.772f); c0[i] = r; c1[i] = g; c2[i] = b; @@ -143,6 +160,145 @@ void mct_decode_real( /* */ /* Get norm of basis function of irreversible MCT. */ /* */ -double mct_getnorm_real(int compno) { +OPJ_FLOAT64 mct_getnorm_real(OPJ_UINT32 compno) { return mct_norms_real[compno]; } + +bool mct_encode_custom( + // MCT data + OPJ_BYTE * pCodingdata, + // size of components + OPJ_UINT32 n, + // components + OPJ_BYTE ** pData, + // nb of components (i.e. size of pData) + OPJ_UINT32 pNbComp, + // tells if the data is signed + OPJ_UINT32 isSigned) +{ + OPJ_FLOAT32 * lMct = (OPJ_FLOAT32 *) pCodingdata; + OPJ_UINT32 i; + OPJ_UINT32 j; + OPJ_UINT32 k; + OPJ_UINT32 lNbMatCoeff = pNbComp * pNbComp; + OPJ_INT32 * lCurrentData = 00; + OPJ_INT32 * lCurrentMatrix = 00; + OPJ_INT32 ** lData = (OPJ_INT32 **) pData; + OPJ_UINT32 lMultiplicator = 1 << 13; + OPJ_INT32 * lMctPtr; + + lCurrentData = (OPJ_INT32 *) opj_malloc((pNbComp + lNbMatCoeff) * sizeof(OPJ_INT32)); + if + (! lCurrentData) + { + return false; + } + lCurrentMatrix = lCurrentData + pNbComp; + for + (i =0;i * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,7 +39,7 @@ The functions in MCT.C have for goal to realize reversible and irreversible multicomponent transform. The functions in MCT.C are used by some function in TCD.C. */ - +#include "openjpeg.h" /** @defgroup MCT MCT - Implementation of a multi-component transform */ /*@{*/ @@ -52,7 +53,7 @@ Apply a reversible multi-component transform to an image @param c2 Samples blue component @param n Number of samples for each component */ -void mct_encode(int *c0, int *c1, int *c2, int n); +void mct_encode(OPJ_INT32 *c0, OPJ_INT32 *c1, OPJ_INT32 *c2, OPJ_UINT32 n); /** Apply a reversible multi-component inverse transform to an image @param c0 Samples for luminance component @@ -60,13 +61,13 @@ Apply a reversible multi-component inverse transform to an image @param c2 Samples for blue chrominance component @param n Number of samples for each component */ -void mct_decode(int *c0, int *c1, int *c2, int n); +void mct_decode(OPJ_INT32 *c0, OPJ_INT32 *c1, OPJ_INT32 *c2, OPJ_UINT32 n); /** Get norm of the basis function used for the reversible multi-component transform @param compno Number of the component (0->Y, 1->U, 2->V) @return */ -double mct_getnorm(int compno); +OPJ_FLOAT64 mct_getnorm(OPJ_UINT32 compno); /** Apply an irreversible multi-component transform to an image @@ -75,7 +76,7 @@ Apply an irreversible multi-component transform to an image @param c2 Samples blue component @param n Number of samples for each component */ -void mct_encode_real(int *c0, int *c1, int *c2, int n); +void mct_encode_real(OPJ_INT32 *c0, OPJ_INT32 *c1, OPJ_INT32 *c2, OPJ_UINT32 n); /** Apply an irreversible multi-component inverse transform to an image @param c0 Samples for luminance component @@ -83,13 +84,44 @@ Apply an irreversible multi-component inverse transform to an image @param c2 Samples for blue chrominance component @param n Number of samples for each component */ -void mct_decode_real(float* c0, float* c1, float* c2, int n); +void mct_decode_real(OPJ_FLOAT32* c0, OPJ_FLOAT32* c1, OPJ_FLOAT32* c2, OPJ_UINT32 n); /** Get norm of the basis function used for the irreversible multi-component transform @param compno Number of the component (0->Y, 1->U, 2->V) @return */ -double mct_getnorm_real(int compno); +OPJ_FLOAT64 mct_getnorm_real(OPJ_UINT32 compno); + +bool mct_encode_custom( + // MCT data + OPJ_BYTE * p_coding_data, + // size of components + OPJ_UINT32 n, + // components + OPJ_BYTE ** p_data, + // nb of components (i.e. size of p_data) + OPJ_UINT32 p_nb_comp, + // tells if the data is signed + OPJ_UINT32 is_signed); + +bool mct_decode_custom( + // MCT data + OPJ_BYTE * pDecodingData, + // size of components + OPJ_UINT32 n, + // components + OPJ_BYTE ** pData, + // nb of components (i.e. size of pData) + OPJ_UINT32 pNbComp, + // tells if the data is signed + OPJ_UINT32 isSigned); + +void opj_calculate_norms(OPJ_FLOAT64 * pNorms,OPJ_UINT32 p_nb_comps,OPJ_FLOAT32 * pMatrix); + +const OPJ_FLOAT64 * get_mct_norms (); +const OPJ_FLOAT64 * get_mct_norms_real (); + + /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/mqc.c b/libopenjpeg/mqc.c index 9aa9d2c2..e6cfd325 100644 --- a/libopenjpeg/mqc.c +++ b/libopenjpeg/mqc.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,7 +30,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" +#include "mqc.h" +#include "t1.h" +#include "opj_malloc.h" /** @defgroup MQC MQC - Implementation of an MQ-Coder */ /*@{*/ @@ -68,13 +71,13 @@ FIXME: documentation ??? @param mqc MQC handle @return */ -static int mqc_mpsexchange(opj_mqc_t *mqc); +static OPJ_INT32 mqc_mpsexchange(opj_mqc_t *mqc); /** FIXME: documentation ??? @param mqc MQC handle @return */ -static int mqc_lpsexchange(opj_mqc_t *mqc); +static OPJ_INT32 mqc_lpsexchange(opj_mqc_t *mqc); /** Input a byte @param mqc MQC handle @@ -264,15 +267,15 @@ static void mqc_codelps(opj_mqc_t *mqc) { } static void mqc_setbits(opj_mqc_t *mqc) { - unsigned int tempc = mqc->c + mqc->a; + OPJ_UINT32 tempc = mqc->c + mqc->a; mqc->c |= 0xffff; if (mqc->c >= tempc) { mqc->c -= 0x8000; } } -static int mqc_mpsexchange(opj_mqc_t *mqc) { - int d; +static OPJ_INT32 mqc_mpsexchange(opj_mqc_t *mqc) { + OPJ_INT32 d; if (mqc->a < (*mqc->curctx)->qeval) { d = 1 - (*mqc->curctx)->mps; *mqc->curctx = (*mqc->curctx)->nlps; @@ -284,8 +287,8 @@ static int mqc_mpsexchange(opj_mqc_t *mqc) { return d; } -static int mqc_lpsexchange(opj_mqc_t *mqc) { - int d; +static OPJ_INT32 mqc_lpsexchange(opj_mqc_t *mqc) { + OPJ_INT32 d; if (mqc->a < (*mqc->curctx)->qeval) { mqc->a = (*mqc->curctx)->qeval; d = (*mqc->curctx)->mps; @@ -301,7 +304,7 @@ static int mqc_lpsexchange(opj_mqc_t *mqc) { static void mqc_bytein(opj_mqc_t *mqc) { if (mqc->bp != mqc->end) { - unsigned int c; + OPJ_UINT32 c; if (mqc->bp + 1 != mqc->end) { c = *(mqc->bp + 1); } else { @@ -350,28 +353,31 @@ opj_mqc_t* mqc_create(void) { } void mqc_destroy(opj_mqc_t *mqc) { - if(mqc) { + if + (mqc) + { opj_free(mqc); } } -int mqc_numbytes(opj_mqc_t *mqc) { +OPJ_UINT32 mqc_numbytes(opj_mqc_t *mqc) { return mqc->bp - mqc->start; } -void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) { +void mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) { mqc_setcurctx(mqc, 0); mqc->a = 0x8000; mqc->c = 0; mqc->bp = bp - 1; + *(mqc->bp) = 0; mqc->ct = 12; - if (*mqc->bp == 0xff) { + /*if (*mqc->bp == 0xff) { mqc->ct = 13; - } + }*/ mqc->start = bp; } -void mqc_encode(opj_mqc_t *mqc, int d) { +void mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) { if ((*mqc->curctx)->mps == d) { mqc_codemps(mqc); } else { @@ -399,7 +405,7 @@ void mqc_bypass_init_enc(opj_mqc_t *mqc) { } */ } -void mqc_bypass_enc(opj_mqc_t *mqc, int d) { +void mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) { mqc->ct--; mqc->c = mqc->c + (d << mqc->ct); if (mqc->ct == 0) { @@ -413,8 +419,8 @@ void mqc_bypass_enc(opj_mqc_t *mqc, int d) { } } -int mqc_bypass_flush_enc(opj_mqc_t *mqc) { - unsigned char bit_padding; +OPJ_UINT32 mqc_bypass_flush_enc(opj_mqc_t *mqc) { + OPJ_BYTE bit_padding; bit_padding = 0; @@ -440,11 +446,11 @@ void mqc_reset_enc(opj_mqc_t *mqc) { mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); } -int mqc_restart_enc(opj_mqc_t *mqc) { - int correction = 1; +OPJ_UINT32 mqc_restart_enc(opj_mqc_t *mqc) { + OPJ_UINT32 correction = 1; /* */ - int n = 27 - 15 - mqc->ct; + OPJ_INT32 n = 27 - 15 - mqc->ct; mqc->c <<= mqc->ct; while (n > 0) { mqc_byteout(mqc); @@ -469,7 +475,7 @@ void mqc_restart_init_enc(opj_mqc_t *mqc) { } void mqc_erterm_enc(opj_mqc_t *mqc) { - int k = 11 - mqc->ct + 1; + OPJ_INT32 k = 11 - mqc->ct + 1; while (k > 0) { mqc->c <<= mqc->ct; @@ -484,7 +490,7 @@ void mqc_erterm_enc(opj_mqc_t *mqc) { } void mqc_segmark_enc(opj_mqc_t *mqc) { - int i; + OPJ_UINT32 i; mqc_setcurctx(mqc, 18); for (i = 1; i < 5; i++) { @@ -492,7 +498,7 @@ void mqc_segmark_enc(opj_mqc_t *mqc) { } } -void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { +void mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len) { mqc_setcurctx(mqc, 0); mqc->start = bp; mqc->end = bp + len; @@ -505,8 +511,8 @@ void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) { mqc->a = 0x8000; } -int mqc_decode(opj_mqc_t *mqc) { - int d; +OPJ_UINT32 mqc_decode(opj_mqc_t *mqc) { + OPJ_INT32 d; mqc->a -= (*mqc->curctx)->qeval; if ((mqc->c >> 16) < (*mqc->curctx)->qeval) { d = mqc_lpsexchange(mqc); @@ -521,17 +527,17 @@ int mqc_decode(opj_mqc_t *mqc) { } } - return d; + return (OPJ_UINT32)d; } void mqc_resetstates(opj_mqc_t *mqc) { - int i; + OPJ_UINT32 i; for (i = 0; i < MQC_NUMCTXS; i++) { mqc->ctxs[i] = mqc_states; } } -void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) { +void mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob) { mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)]; } diff --git a/libopenjpeg/mqc.h b/libopenjpeg/mqc.h index 8cc8c934..93d3b943 100644 --- a/libopenjpeg/mqc.h +++ b/libopenjpeg/mqc.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,7 +39,7 @@ The functions in MQC.C have for goal to realize the MQ-coder operations. The functions in MQC.C are used by some function in T1.C. */ - +#include "openjpeg.h" /** @defgroup MQC MQC - Implementation of an MQ-Coder */ /*@{*/ @@ -47,27 +48,27 @@ This struct defines the state of a context. */ typedef struct opj_mqc_state { /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */ - unsigned int qeval; + OPJ_UINT32 qeval; /** the Most Probable Symbol (0 or 1) */ - int mps; + OPJ_INT32 mps; /** next state if the next encoded symbol is the MPS */ struct opj_mqc_state *nmps; /** next state if the next encoded symbol is the LPS */ struct opj_mqc_state *nlps; } opj_mqc_state_t; -#define MQC_NUMCTXS 19 +#define MQC_NUMCTXS 32 /** MQ coder */ typedef struct opj_mqc { - unsigned int c; - unsigned int a; - unsigned int ct; - unsigned char *bp; - unsigned char *start; - unsigned char *end; + OPJ_UINT32 c; + OPJ_UINT32 a; + OPJ_UINT32 ct; + OPJ_BYTE *bp; + OPJ_BYTE *start; + OPJ_BYTE *end; opj_mqc_state_t *ctxs[MQC_NUMCTXS]; opj_mqc_state_t **curctx; } opj_mqc_t; @@ -90,7 +91,7 @@ Return the number of bytes written/read since initialisation @param mqc MQC handle @return Returns the number of bytes already encoded */ -int mqc_numbytes(opj_mqc_t *mqc); +OPJ_UINT32 mqc_numbytes(opj_mqc_t *mqc); /** Reset the states of all the context of the coder/decoder (each context is set to a state where 0 and 1 are more or less equiprobable) @@ -104,25 +105,25 @@ Set the state of a particular context @param msb The MSB of the new state of the context @param prob Number that identifies the probability of the symbols for the new state of the context */ -void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob); +void mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob); /** Initialize the encoder @param mqc MQC handle @param bp Pointer to the start of the buffer where the bytes will be written */ -void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp); +void mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp); /** Set the current context used for coding/decoding @param mqc MQC handle @param ctxno Number that identifies the context */ -#define mqc_setcurctx(mqc, ctxno) (mqc)->curctx = &(mqc)->ctxs[(int)(ctxno)] +#define mqc_setcurctx(mqc, ctxno) (mqc)->curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)] /** Encode a symbol using the MQ-coder @param mqc MQC handle @param d The symbol to be encoded (0 or 1) */ -void mqc_encode(opj_mqc_t *mqc, int d); +void mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d); /** Flush the encoder, so that all remaining data is written @param mqc MQC handle @@ -142,14 +143,14 @@ JPEG 2000 p 505. @param mqc MQC handle @param d The symbol to be encoded (0 or 1) */ -void mqc_bypass_enc(opj_mqc_t *mqc, int d); +void mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d); /** BYPASS mode switch, flush operation

    Not fully implemented and tested !!

    @param mqc MQC handle @return Returns 1 (always) */ -int mqc_bypass_flush_enc(opj_mqc_t *mqc); +OPJ_UINT32 mqc_bypass_flush_enc(opj_mqc_t *mqc); /** RESET mode switch @param mqc MQC handle @@ -160,7 +161,7 @@ RESTART mode switch (TERMALL) @param mqc MQC handle @return Returns 1 (always) */ -int mqc_restart_enc(opj_mqc_t *mqc); +OPJ_UINT32 mqc_restart_enc(opj_mqc_t *mqc); /** RESTART mode switch (TERMALL) reinitialisation @param mqc MQC handle @@ -182,13 +183,13 @@ Initialize the decoder @param bp Pointer to the start of the buffer from which the bytes will be read @param len Length of the input buffer */ -void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len); +void mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len); /** Decode a symbol @param mqc MQC handle @return Returns the decoded symbol (0 or 1) */ -int mqc_decode(opj_mqc_t *mqc); +OPJ_UINT32 mqc_decode(opj_mqc_t *mqc); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/openjpeg.c b/libopenjpeg/openjpeg.c index 88d44fd0..574b5135 100644 --- a/libopenjpeg/openjpeg.c +++ b/libopenjpeg/openjpeg.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,7 +29,124 @@ #include #endif /* WIN32 */ -#include "opj_includes.h" +#include "openjpeg.h" +#include "opj_malloc.h" +#include "j2k.h" +#include "jp2.h" +#include "event.h" +#include "cio.h" + +typedef struct opj_decompression +{ + bool (* opj_read_header) ( + void *p_codec, + opj_image_t **, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + struct opj_stream_private *cio, + struct opj_event_mgr * p_manager); + opj_image_t* (* opj_decode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager); + bool (*opj_read_tile_header)( + void * p_codec, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32* p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_should_go_on, + struct opj_stream_private *p_cio, + struct opj_event_mgr * p_manager); + bool (*opj_decode_tile_data)(void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private *p_cio,struct opj_event_mgr * p_manager); + bool (* opj_end_decompress) (void *p_codec,struct opj_stream_private *cio,struct opj_event_mgr * p_manager); + void (* opj_destroy) (void * p_codec); + void (*opj_setup_decoder) (void * p_codec,opj_dparameters_t * p_param); + bool (*opj_set_decode_area) (void * p_codec,OPJ_INT32 p_start_x,OPJ_INT32 p_end_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_y,struct opj_event_mgr * p_manager); + + +}opj_decompression_t; + +typedef struct opj_compression +{ + bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image, struct opj_event_mgr * p_manager); + bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager); + bool (* opj_write_tile) (void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private * p_cio,struct opj_event_mgr * p_manager); + bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager); + void (* opj_destroy) (void * p_codec); + void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager); + +}opj_compression_t; + + + +typedef struct opj_codec_private +{ + union + { /* code-blocks informations */ + opj_decompression_t m_decompression; + opj_compression_t m_compression; + } m_codec_data; + void * m_codec; + opj_event_mgr_t m_event_mgr; + unsigned is_decompressor : 1; +} +opj_codec_private_t; + + + +/** + * Default callback function. + * Do nothing. + */ +void opj_default_callback (const char *msg, void *client_data) +{ +} + +void set_default_event_handler(opj_event_mgr_t * p_manager) +{ + p_manager->m_error_data = 00; + p_manager->m_warning_data = 00; + p_manager->m_info_data = 00; + p_manager->error_handler = opj_default_callback; + p_manager->info_handler = opj_default_callback; + p_manager->warning_handler = opj_default_callback; +} + +OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file) +{ + OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file); + return l_nb_read ? l_nb_read : -1; +} + +OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file) +{ + return fwrite(p_buffer,1,p_nb_bytes,p_file); +} + +OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data) +{ + if + (fseek(p_user_data,p_nb_bytes,SEEK_CUR)) + { + return -1; + } + return p_nb_bytes; +} + +bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data) +{ + if + (fseek(p_user_data,p_nb_bytes,SEEK_SET)) + { + return false; + } + return true; +} /* ---------------------------------------------------------------------- */ #ifdef WIN32 @@ -57,56 +175,133 @@ const char* OPJ_CALLCONV opj_version(void) { return OPENJPEG_VERSION; } -opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) { - opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t)); - if(!dinfo) return NULL; - dinfo->is_decompressor = true; - switch(format) { +opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format) +{ + opj_codec_private_t *l_info = 00; + + l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t)); + if + (!l_info) + { + return 00; + } + memset(l_info, 0, sizeof(opj_codec_private_t)); + l_info->is_decompressor = 1; + switch + (p_format) + { case CODEC_J2K: - case CODEC_JPT: - /* get a J2K decoder handle */ - dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo); - if(!dinfo->j2k_handle) { - opj_free(dinfo); - return NULL; + l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))j2k_decode; + l_info->m_codec_data.m_decompression.opj_end_decompress = (bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *))j2k_end_decompress; + l_info->m_codec_data.m_decompression.opj_read_header = (bool (*) ( + void *, + opj_image_t **, + OPJ_INT32 * , + OPJ_INT32 * , + OPJ_UINT32 * , + OPJ_UINT32 * , + OPJ_UINT32 * , + OPJ_UINT32 * , + struct opj_stream_private *, + struct opj_event_mgr * )) j2k_read_header; + l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))j2k_destroy; + l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) j2k_setup_decoder; + l_info->m_codec_data.m_decompression.opj_read_tile_header = (bool (*) ( + void *, + OPJ_UINT32*, + OPJ_UINT32*, + OPJ_INT32 * , + OPJ_INT32 * , + OPJ_INT32 * , + OPJ_INT32 * , + OPJ_UINT32 * , + bool *, + struct opj_stream_private *, + struct opj_event_mgr * )) j2k_read_tile_header; + l_info->m_codec_data.m_decompression.opj_decode_tile_data = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) j2k_decode_tile; + l_info->m_codec_data.m_decompression.opj_set_decode_area = (bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) j2k_set_decode_area; + l_info->m_codec = j2k_create_decompress(); + if + (! l_info->m_codec) + { + opj_free(l_info); + return 00; } break; + case CODEC_JP2: /* get a JP2 decoder handle */ - dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo); - if(!dinfo->jp2_handle) { - opj_free(dinfo); - return NULL; + l_info->m_codec_data.m_decompression.opj_decode = (opj_image_t* (*) (void *, struct opj_stream_private *, struct opj_event_mgr * ))jp2_decode; + l_info->m_codec_data.m_decompression.opj_end_decompress = (bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress; + l_info->m_codec_data.m_decompression.opj_read_header = (bool (*) ( + void *, + opj_image_t **, + + OPJ_INT32 * , + OPJ_INT32 * , + OPJ_UINT32 * , + OPJ_UINT32 * , + OPJ_UINT32 * , + OPJ_UINT32 * , + struct opj_stream_private *, + struct opj_event_mgr * )) jp2_read_header; + + l_info->m_codec_data.m_decompression.opj_read_tile_header = ( + bool (*) ( + void *, + OPJ_UINT32*, + OPJ_UINT32*, + OPJ_INT32*, + OPJ_INT32*, + OPJ_INT32 * , + OPJ_INT32 * , + OPJ_UINT32 * , + bool *, + struct opj_stream_private *, + struct opj_event_mgr * )) jp2_read_tile_header; + + l_info->m_codec_data.m_decompression.opj_decode_tile_data = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) jp2_decode_tile; + + l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy; + l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder; + l_info->m_codec_data.m_decompression.opj_set_decode_area = (bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area; + + + l_info->m_codec = jp2_create(true); + if + (! l_info->m_codec) + { + opj_free(l_info); + return 00; } break; case CODEC_UNKNOWN: + case CODEC_JPT: default: - opj_free(dinfo); - return NULL; + opj_free(l_info); + return 00; } - - dinfo->codec_format = format; - - return dinfo; + set_default_event_handler(&(l_info->m_event_mgr)); + return (opj_codec_t*) l_info; } -void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) { - if(dinfo) { - /* destroy the codec */ - switch(dinfo->codec_format) { - case CODEC_J2K: - case CODEC_JPT: - j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle); - break; - case CODEC_JP2: - jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle); - break; - case CODEC_UNKNOWN: - default: - break; +void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info) +{ + if + (p_info) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_info; + if + (l_info->is_decompressor) + { + l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec); } - /* destroy the decompressor */ - opj_free(dinfo); + else + { + l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec); + } + l_info->m_codec = 00; + opj_free(l_info); } } @@ -116,7 +311,6 @@ void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *paramete /* default decoding parameters */ parameters->cp_layer = 0; parameters->cp_reduce = 0; - parameters->cp_limit_decoding = NO_LIMITATION; parameters->decod_format = -1; parameters->cod_format = -1; @@ -130,95 +324,317 @@ void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *paramete } } -void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) { - if(dinfo && parameters) { - switch(dinfo->codec_format) { - case CODEC_J2K: - case CODEC_JPT: - j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters); - break; - case CODEC_JP2: - jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters); - break; - case CODEC_UNKNOWN: - default: - break; +bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_info, opj_dparameters_t *parameters) { + if + (p_info && parameters) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_info; + if + (! l_info->is_decompressor) + { + return false; } + l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec,parameters); + return true; } + return false; } -opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) { - return opj_decode_with_info(dinfo, cio, NULL); -} - -opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) { - if(dinfo && cio) { - switch(dinfo->codec_format) { - case CODEC_J2K: - return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info); - case CODEC_JPT: - return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info); - case CODEC_JP2: - return jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info); - case CODEC_UNKNOWN: - default: - break; +opj_image_t* OPJ_CALLCONV opj_decode(opj_codec_t *p_info, opj_stream_t *cio) +{ + if + (p_info && cio) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_info; + opj_stream_private_t * l_cio = (opj_stream_private_t *) cio; + if + (! l_info->is_decompressor) + { + return 00; } + return l_info->m_codec_data.m_decompression.opj_decode(l_info->m_codec,l_cio,&(l_info->m_event_mgr)); } - return NULL; + return 00; } -opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) { - opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t)); - if(!cinfo) return NULL; - cinfo->is_decompressor = false; - switch(format) { +/** + * Writes a tile with the given data. + * + * @param p_compressor the jpeg2000 codec. + * @param p_tile_index the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence. + * @param p_data pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set. + * @param p_data_size this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes, + * depending on the precision of the given component. + * @param p_stream the stream to write data to. + */ +bool opj_write_tile ( + opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream + ) +{ + if + (p_codec && p_stream && p_data) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream; + if + (l_info->is_decompressor) + { + return false; + } + return l_info->m_codec_data.m_compression.opj_write_tile(l_info->m_codec,p_tile_index,p_data,p_data_size,l_cio,&(l_info->m_event_mgr)); + } + return false; +} + +/** + * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success. + * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case + * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same + * as depicted in opj_write_tile. + * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image). + * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image). + * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image). + * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image). + * @param p_nb_comps pointer to a value that will hold the number of components in the tile. + * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the + * codestream is over at the time of the call, the value will be set to false. The user should then stop + * the decoding. + * @param p_stream the stream to decode. + * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true. + * returning false may be the result of a shortage of memory or an internal error. + */ +bool opj_read_tile_header( + opj_codec_t *p_codec, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_should_go_on, + opj_stream_t * p_stream) +{ + if + (p_codec && p_stream && p_data_size && p_tile_index) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream; + if + (! l_info->is_decompressor) + { + return false; + } + return l_info->m_codec_data.m_decompression.opj_read_tile_header( + l_info->m_codec, + p_tile_index, + p_data_size, + p_tile_x0, + p_tile_y0, + p_tile_x1, + p_tile_y1, + p_nb_comps, + p_should_go_on, + l_cio,&(l_info->m_event_mgr)); + } + return false; +} + +/** + * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header. + * @param p_data pointer to a memory block that will hold the decoded data. + * @param p_data_size size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header. + * @param p_stream the stream to decode. + * + * @return true if the data could be decoded. + */ +bool opj_decode_tile_data( + opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream + ) +{ + if + (p_codec && p_data && p_stream) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream; + if + (! l_info->is_decompressor) + { + return false; + } + return l_info->m_codec_data.m_decompression.opj_decode_tile_data(l_info->m_codec,p_tile_index,p_data,p_data_size,l_cio,&(l_info->m_event_mgr)); + } + return false; +} + +bool OPJ_CALLCONV opj_read_header ( + opj_codec_t *p_codec, + opj_image_t ** p_image, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + opj_stream_t *p_cio) +{ + if + (p_codec && p_cio) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio; + if + (! l_info->is_decompressor) + { + return false; + } + return l_info->m_codec_data.m_decompression.opj_read_header( + l_info->m_codec, + p_image, + p_tile_x0, + p_tile_y0, + p_tile_width, + p_tile_height, + p_nb_tiles_x, + p_nb_tiles_y, + l_cio, + &(l_info->m_event_mgr)); + } + return false; +} + +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_codec the jpeg2000 codec. + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * + * @return true if the area could be set. + */ +bool opj_set_decode_area( + opj_codec_t *p_codec, + OPJ_INT32 p_start_x, + OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, + OPJ_INT32 p_end_y + ) +{ + if + (p_codec) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + if + (! l_info->is_decompressor) + { + return false; + } + return l_info->m_codec_data.m_decompression.opj_set_decode_area( + l_info->m_codec, + p_start_x, + p_start_y, + p_end_x, + p_end_y, + &(l_info->m_event_mgr)); + + } + return false; + +} + +bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio) +{ + if + (p_codec && p_cio) + { + opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio; + if + (! l_info->is_decompressor) + { + return false; + } + return l_info->m_codec_data.m_decompression.opj_end_decompress(l_info->m_codec,l_cio,&(l_info->m_event_mgr)); + } + return false; +} + + +opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format) +{ + opj_codec_private_t *l_info = 00; + + l_info = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t)); + if + (!l_info) + { + return 00; + } + memset(l_info, 0, sizeof(opj_codec_private_t)); + l_info->is_decompressor = 0; + switch + (p_format) + { case CODEC_J2K: - /* get a J2K coder handle */ - cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo); - if(!cinfo->j2k_handle) { - opj_free(cinfo); - return NULL; + l_info->m_codec_data.m_compression.opj_encode = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr * )) j2k_encode; + l_info->m_codec_data.m_compression.opj_end_compress = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *)) j2k_end_compress; + l_info->m_codec_data.m_compression.opj_start_compress = (bool (*) (void *,struct opj_stream_private *,struct opj_image * , struct opj_event_mgr *)) j2k_start_compress; + l_info->m_codec_data.m_compression.opj_write_tile = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr *)) j2k_write_tile; + l_info->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy; + l_info->m_codec_data.m_compression.opj_setup_encoder = (void (*) (void *,opj_cparameters_t *,struct opj_image *, struct opj_event_mgr * )) j2k_setup_encoder; + + l_info->m_codec = j2k_create_compress(); + if + (! l_info->m_codec) + { + opj_free(l_info); + return 00; } break; + case CODEC_JP2: - /* get a JP2 coder handle */ - cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo); - if(!cinfo->jp2_handle) { - opj_free(cinfo); - return NULL; + /* get a JP2 decoder handle */ + l_info->m_codec_data.m_compression.opj_encode = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr * )) jp2_encode; + l_info->m_codec_data.m_compression.opj_end_compress = (bool (*) (void *, struct opj_stream_private *, struct opj_event_mgr *)) jp2_end_compress; + l_info->m_codec_data.m_compression.opj_start_compress = (bool (*) (void *,struct opj_stream_private *,struct opj_image * , struct opj_event_mgr *)) jp2_start_compress; + l_info->m_codec_data.m_compression.opj_write_tile = (bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr *)) jp2_write_tile; + l_info->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy; + l_info->m_codec_data.m_compression.opj_setup_encoder = (void (*) (void *,opj_cparameters_t *,struct opj_image *, struct opj_event_mgr * )) jp2_setup_encoder; + + l_info->m_codec = jp2_create(false); + if + (! l_info->m_codec) + { + opj_free(l_info); + return 00; } break; - case CODEC_JPT: case CODEC_UNKNOWN: + case CODEC_JPT: default: - opj_free(cinfo); - return NULL; - } - - cinfo->codec_format = format; - - return cinfo; -} - -void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) { - if(cinfo) { - /* destroy the codec */ - switch(cinfo->codec_format) { - case CODEC_J2K: - j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle); - break; - case CODEC_JP2: - jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle); - break; - case CODEC_JPT: - case CODEC_UNKNOWN: - default: - break; - } - /* destroy the decompressor */ - opj_free(cinfo); + opj_free(l_info); + return 00; } + set_default_event_handler(&(l_info->m_event_mgr)); + return (opj_codec_t*) l_info; } void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) { @@ -238,10 +654,6 @@ void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *paramete parameters->tp_on = 0; parameters->decod_format = -1; parameters->cod_format = -1; - parameters->tcp_rates[0] = 0; - parameters->tcp_numlayers = 1; - parameters->cp_disto_alloc = 1; - /* UniPG>> */ #ifdef USE_JPWL parameters->jpwl_epc_on = false; @@ -277,38 +689,153 @@ void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *paramete } } -void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) { - if(cinfo && parameters && image) { - switch(cinfo->codec_format) { - case CODEC_J2K: - j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image); - break; - case CODEC_JP2: - jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image); - break; - case CODEC_JPT: - case CODEC_UNKNOWN: - default: - break; +/** + * Helper function. + * Sets the stream to be a file stream. The FILE must have been open previously. + * @param p_stream the stream to modify + * @param p_file handler to an already open file. +*/ +opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file,bool p_is_read_stream) +{ + return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream); +} + +opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file,OPJ_UINT32 p_size,bool p_is_read_stream) +{ + opj_stream_t* l_stream = 00; + if + (! p_file) + { + return 00; + } + l_stream = opj_stream_create(p_size,p_is_read_stream); + if + (! l_stream) + { + return 00; + } + opj_stream_set_user_data(l_stream,p_file); + opj_stream_set_read_function(l_stream,(opj_stream_read_fn) opj_read_from_file); + opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file); + opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file); + opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file); + return l_stream; +} + + +bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_info, opj_cparameters_t *parameters, opj_image_t *image) +{ + if + (p_info && parameters && image) + { + opj_codec_private_t * l_codec = ((opj_codec_private_t *) p_info); + if + (! l_codec->is_decompressor) + { + l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,parameters,image,&(l_codec->m_event_mgr)); + return true; } } + return false; } -bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) { - if (index != NULL) - opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n" - "To extract the index, use the opj_encode_with_info() function.\n" - "No index will be generated during this encoding\n"); - return opj_encode_with_info(cinfo, cio, image, NULL); +bool OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *cio) +{ + if + (p_info && cio) + { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info; + opj_stream_private_t * l_cio = (opj_stream_private_t *) cio; + if + (! l_codec->is_decompressor) + { + l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,l_cio,&(l_codec->m_event_mgr)); + return true; + } + } + return false; + } -bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { +bool OPJ_CALLCONV opj_start_compress (opj_codec_t *p_codec,opj_image_t * p_image,opj_stream_t *p_cio) +{ + if + (p_codec && p_cio) + { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio; + if + (! l_codec->is_decompressor) + { + return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,l_cio,p_image,&(l_codec->m_event_mgr)); + } + } + return false; +} + +bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,opj_stream_t *p_cio) +{ + if + (p_codec && p_cio) + { + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio; + if + (! l_codec->is_decompressor) + { + return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,l_cio,&(l_codec->m_event_mgr)); + } + } + return false; + +} + +bool OPJ_CALLCONV opj_set_info_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + if + (! l_codec) + { + return false; + } + l_codec->m_event_mgr.info_handler = p_callback; + l_codec->m_event_mgr.m_info_data = p_user_data; + return true; +} + +bool OPJ_CALLCONV opj_set_warning_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + if + (! l_codec) + { + return false; + } + l_codec->m_event_mgr.warning_handler = p_callback; + l_codec->m_event_mgr.m_warning_data = p_user_data; + return true; +} + +bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, opj_msg_callback p_callback,void * p_user_data) +{ + opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec; + if + (! l_codec) + { + return false; + } + l_codec->m_event_mgr.error_handler = p_callback; + l_codec->m_event_mgr.m_error_data = p_user_data; + return true; +} + +/*bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_stream_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) { if(cinfo && cio && image) { switch(cinfo->codec_format) { case CODEC_J2K: - return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info); + return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, (opj_stream_private_t *) cio, image, cstr_info); case CODEC_JP2: - return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info); + return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, (opj_stream_private_t *) cio, image, cstr_info); case CODEC_JPT: case CODEC_UNKNOWN: default: @@ -316,10 +843,12 @@ bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_i } } return false; -} +}*/ void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) { - if (cstr_info) { + if + (cstr_info) + { int tileno; for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) { opj_tile_info_t *tile_info = &cstr_info->tile[tileno]; @@ -329,6 +858,45 @@ void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) { } opj_free(cstr_info->tile); opj_free(cstr_info->marker); - opj_free(cstr_info->numdecompos); } } + +bool opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp) +{ + OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32); + OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32); + OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size; + // add MCT capability + parameters->cp_rsiz |= 0x8100; + parameters->irreversible = 1; + // use array based MCT + parameters->tcp_mct = 2; + parameters->mct_data = opj_malloc(l_mct_total_size); + if + (! parameters->mct_data) + { + return false; + } + memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size); + memcpy(((OPJ_BYTE *) parameters->mct_data) + l_matrix_size,p_dc_shift,l_dc_shift_size); + return true; +} + +/** + * Restricts the decoding to the given image area. + * + * @param parameters the parameters to update. + * @param p_start_x the starting x position of the area to decode. + * @param p_start_y the starting y position of the area to decode. + * @param p_end_x the x end position of the area to decode. + * @param p_end_x the y end position of the area to decode. + */ +OPJ_API bool OPJ_CALLCONV opj_restrict_decoding (opj_dparameters_t *parameters,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y) +{ + parameters->m_use_restrict_decode = 1; + parameters->m_decode_start_x = p_start_x; + parameters->m_decode_start_y = p_start_y; + parameters->m_decode_end_x = p_end_x; + parameters->m_decode_end_y = p_end_y; + return true; +} diff --git a/libopenjpeg/openjpeg.h b/libopenjpeg/openjpeg.h index ffcaacaf..48d863e1 100644 --- a/libopenjpeg/openjpeg.h +++ b/libopenjpeg/openjpeg.h @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,58 +33,59 @@ #ifndef OPENJPEG_H #define OPENJPEG_H -#define OPENJPEG_VERSION "1.3.0" +#if defined(OPJ_STATIC) || !(defined(WIN32) || defined(__WIN32__)) + #define OPJ_API + #define OPJ_CALLCONV +#else + #define OPJ_CALLCONV __stdcall + #ifdef OPJ_EXPORTS + #define OPJ_API __declspec(dllexport) + #else + #define OPJ_API __declspec(dllimport) + #endif /* OPJ_EXPORTS */ +#endif /* !OPJ_STATIC || !WIN32 */ +#ifndef __cplusplus + #if defined(HAVE_STDBOOL_H) + #include + #else + #if !defined(bool) + #define bool int + #endif + #if !defined(true) + #define true 1 + #endif + #if !defined(false) + #define false 0 + #endif + #endif +#endif /* __cplusplus */ +typedef unsigned int OPJ_UINT32; +typedef int OPJ_INT32; +typedef unsigned short OPJ_UINT16; +typedef short OPJ_INT16; +typedef char OPJ_CHAR; +typedef unsigned char OPJ_BYTE; +typedef unsigned int OPJ_SIZE_T; +typedef double OPJ_FLOAT64; +typedef float OPJ_FLOAT32; +#if defined(_MSC_VER) || defined(__BORLANDC__) + typedef __int64 OPJ_INT64; +#else + typedef long long OPJ_INT64; +#endif + +#define OPENJPEG_VERSION "1.2.0" /* ========================================================== Compiler directives ========================================================== */ +#include + + -#if defined(OPJ_STATIC) || !(defined(WIN32) || defined(__WIN32__)) -#define OPJ_API -#define OPJ_CALLCONV -#else -#define OPJ_CALLCONV __stdcall -/* -The following ifdef block is the standard way of creating macros which make exporting -from a DLL simpler. All files within this DLL are compiled with the OPJ_EXPORTS -symbol defined on the command line. this symbol should not be defined on any project -that uses this DLL. This way any other project whose source files include this file see -OPJ_API functions as being imported from a DLL, wheras this DLL sees symbols -defined with this macro as being exported. -*/ -#ifdef OPJ_EXPORTS -#define OPJ_API __declspec(dllexport) -#else -#define OPJ_API __declspec(dllimport) -#endif /* OPJ_EXPORTS */ -#endif /* !OPJ_STATIC || !WIN32 */ -#ifndef __cplusplus -#if defined(HAVE_STDBOOL_H) -/* -The C language implementation does correctly provide the standard header -file "stdbool.h". - */ -#include -#else -/* -The C language implementation does not provide the standard header file -"stdbool.h" as required by ISO/IEC 9899:1999. Try to compensate for this -braindamage below. -*/ -#if !defined(bool) -#define bool int -#endif -#if !defined(true) -#define true 1 -#endif -#if !defined(false) -#define false 0 -#endif -#endif -#endif /* __cplusplus */ /* ========================================================== @@ -91,10 +93,14 @@ braindamage below. ========================================================== */ -#define OPJ_PATH_LEN 4096 /**< Maximum allowed size for filenames */ - -#define J2K_MAXRLVLS 33 /**< Number of maximum resolution level authorized */ -#define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /**< Number of maximum sub-band linked to number of resolution level */ +#define OPJ_PATH_LEN 4096 /**< Maximum allowed size for filenames */ +#define J2K_MAXRLVLS 33 /**< Number of maximum resolution level authorized */ +#define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /**< Number of maximum sub-band linked to number of resolution level */ +#define J2K_DEFAULT_NB_SEGS 10 +#define J2K_STREAM_CHUNK_SIZE 0x100000 /** 1 mega by default */ +#define J2K_DEFAULT_HEADER_SIZE 1000 +#define J2K_MCC_DEFAULT_NB_RECORDS 10 +#define J2K_MCT_DEFAULT_NB_RECORDS 10 /* UniPG>> */ #define JPWL_MAX_NO_TILESPECS 16 /**< Maximum number of tile parts expected by JPWL: increase at your will */ @@ -118,7 +124,8 @@ Rsiz Capabilities typedef enum RSIZ_CAPABILITIES { STD_RSIZ = 0, /** Standard JPEG2000 profile*/ CINEMA2K = 3, /** Profile name for a 2K image*/ - CINEMA4K = 4 /** Profile name for a 4K image*/ + CINEMA4K = 4, /** Profile name for a 4K image*/ + MCT = 0x8100 } OPJ_RSIZ_CAPABILITIES; /** @@ -163,14 +170,8 @@ typedef enum CODEC_FORMAT { CODEC_JP2 = 2 /**< JPEG-2000 file format : read/write */ } OPJ_CODEC_FORMAT; -/** -Limit decoding to certain portions of the codestream. -*/ -typedef enum LIMIT_DECODING { - NO_LIMITATION = 0, /**< No limitation for the decoding. The entire codestream will de decoded */ - LIMIT_TO_MAIN_HEADER = 1, /**< The decoding is limited to the Main Header */ - DECODE_ALL_BUT_PACKETS = 2 /**< Decode everything except the JPEG 2000 packets */ -} OPJ_LIMIT_DECODING; + + /* ========================================================== @@ -183,25 +184,9 @@ Callback function prototype for events @param msg Event message @param client_data */ -typedef void (*opj_msg_callback) (const char *msg, void *client_data); +typedef void (*opj_msg_callback) (const OPJ_CHAR *msg, void *client_data); + -/** -Message handler object -used for -
      -
    • Error messages -
    • Warning messages -
    • Debugging messages -
    -*/ -typedef struct opj_event_mgr { - /** Error message callback if available, NULL otherwise */ - opj_msg_callback error_handler; - /** Warning message callback if available, NULL otherwise */ - opj_msg_callback warning_handler; - /** Debug message callback if available, NULL otherwise */ - opj_msg_callback info_handler; -} opj_event_mgr_t; /* @@ -213,29 +198,30 @@ typedef struct opj_event_mgr { /** Progression order changes */ -typedef struct opj_poc { +typedef struct opj_poc +{ /** Resolution num start, Component num start, given by POC */ - int resno0, compno0; + OPJ_UINT32 resno0, compno0; /** Layer num end,Resolution num end, Component num end, given by POC */ - int layno1, resno1, compno1; + OPJ_UINT32 layno1, resno1, compno1; /** Layer num start,Precinct num start, Precinct num end */ - int layno0, precno0, precno1; + OPJ_UINT32 layno0, precno0, precno1; /** Progression order enum*/ OPJ_PROG_ORDER prg1,prg; /** Progression order string*/ - char progorder[5]; + OPJ_CHAR progorder[5]; /** Tile number */ - int tile; + OPJ_UINT32 tile; /** Start and end values for Tile width and height*/ - int tx0,tx1,ty0,ty1; + OPJ_INT32 tx0,tx1,ty0,ty1; /** Start value, initialised in pi_initialise_encode*/ - int layS, resS, compS, prcS; + OPJ_UINT32 layS, resS, compS, prcS; /** End value, initialised in pi_initialise_encode */ - int layE, resE, compE, prcE; + OPJ_UINT32 layE, resE, compE, prcE; /** Start and end values of Tile width and height, initialised in pi_initialise_encode*/ - int txS,txE,tyS,tyE,dx,dy; + OPJ_UINT32 txS,txE,tyS,tyE,dx,dy; /** Temporary values for Tile parts, initialised in pi_create_encode */ - int lay_t, res_t, comp_t, prc_t,tx0_t,ty0_t; + OPJ_UINT32 lay_t, res_t, comp_t, prc_t,tx0_t,ty0_t; } opj_poc_t; /** @@ -365,6 +351,9 @@ typedef struct opj_cparameters { char tp_flag; /** MCT (multiple component transform) */ char tcp_mct; + /** Naive implementation of MCT restricted to a single reversible array based encoding without offset concerning all the components. */ + void * mct_data; + } opj_cparameters_t; /** @@ -387,6 +376,14 @@ typedef struct opj_dparameters { */ int cp_layer; + /** + * Restrictive decoding parameters. + */ + OPJ_INT32 m_decode_start_x; + OPJ_INT32 m_decode_start_y ; + OPJ_INT32 m_decode_end_x ; + OPJ_INT32 m_decode_end_y ; + /**@name command line encoder parameters (not used inside the library) */ /*@{*/ /** input file name */ @@ -408,61 +405,19 @@ typedef struct opj_dparameters { int jpwl_exp_comps; /** maximum number of tiles */ int jpwl_max_tiles; + + /** use restrictive decoding ? */ + OPJ_UINT32 m_use_restrict_decode : 1; /*@}*/ /* <cp. @param dinfo decompressor handle @param parameters decompression parameters */ -OPJ_API void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters); +OPJ_API bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *dinfo, opj_dparameters_t *parameters); +#endif + /** Decode an image from a JPEG-2000 codestream @param dinfo decompressor handle @param cio Input buffer stream @return Returns a decoded image if successful, returns NULL otherwise */ -OPJ_API opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio); +OPJ_API opj_image_t* OPJ_CALLCONV opj_decode(opj_codec_t *p_decompressor, opj_stream_t * cio); + +/** + * Writes a tile with the given data. + * + * @param p_compressor the jpeg2000 codec. + * @param p_tile_index the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence. + * @param p_data pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set. + * @param p_data_size this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes, + * depending on the precision of the given component. + * @param p_stream the stream to write data to. + * + * @return true if the data could be written. + */ +OPJ_API bool OPJ_CALLCONV opj_write_tile ( + opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream + ); + +/** + * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index pointer to a value that will hold the index of the tile being decoded, in case of success. + * @param p_data_size pointer to a value that will hold the maximum size of the decoded data, in case of success. In case + * of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same + * as depicted in opj_write_tile. + * @param p_tile_x0 pointer to a value that will hold the x0 pos of the tile (in the image). + * @param p_tile_y0 pointer to a value that will hold the y0 pos of the tile (in the image). + * @param p_tile_x1 pointer to a value that will hold the x1 pos of the tile (in the image). + * @param p_tile_y1 pointer to a value that will hold the y1 pos of the tile (in the image). + * @param p_nb_comps pointer to a value that will hold the number of components in the tile. + * @param p_should_go_on pointer to a boolean that will hold the fact that the decoding should go on. In case the + * codestream is over at the time of the call, the value will be set to false. The user should then stop + * the decoding. + * @param p_stream the stream to decode. + * @return true if the tile header could be decoded. In case the decoding should end, the returned value is still true. + * returning false may be the result of a shortage of memory or an internal error. + */ +OPJ_API bool OPJ_CALLCONV opj_read_tile_header( + opj_codec_t *p_codec, + OPJ_UINT32 * p_tile_index, + OPJ_UINT32 * p_data_size, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_INT32 * p_tile_x1, + OPJ_INT32 * p_tile_y1, + OPJ_UINT32 * p_nb_comps, + bool * p_should_go_on, + opj_stream_t * p_stream); + + +/** + * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before. + * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile. + * + * @param p_codec the jpeg2000 codec. + * @param p_tile_index the index of the tile being decoded, this should be the value set by opj_read_tile_header. + * @param p_data pointer to a memory block that will hold the decoded data. + * @param p_data_size size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header. + * @param p_stream the stream to decode. + * + * @return true if the data could be decoded. + */ +OPJ_API bool OPJ_CALLCONV opj_decode_tile_data( + opj_codec_t *p_codec, + OPJ_UINT32 p_tile_index, + OPJ_BYTE * p_data, + OPJ_UINT32 p_data_size, + opj_stream_t *p_stream + ); + +/** + * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading. + * + * @param p_codec the jpeg2000 codec. + * @param p_start_x the left position of the rectangle to decode (in image coordinates). + * @param p_end_x the right position of the rectangle to decode (in image coordinates). + * @param p_start_y the up position of the rectangle to decode (in image coordinates). + * @param p_end_y the bottom position of the rectangle to decode (in image coordinates). + * + * @return true if the area could be set. + */ +OPJ_API bool OPJ_CALLCONV opj_set_decode_area( + opj_codec_t *p_codec, + OPJ_INT32 p_start_x, + OPJ_INT32 p_start_y, + OPJ_INT32 p_end_x, + OPJ_INT32 p_end_y + ); + /** Decode an image from a JPEG-2000 codestream and extract the codestream information @@ -839,18 +927,20 @@ Decode an image from a JPEG-2000 codestream and extract the codestream informati @param cstr_info Codestream information structure if needed afterwards, NULL otherwise @return Returns a decoded image if successful, returns NULL otherwise */ -OPJ_API opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info); +//OPJ_API opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_stream_t cio, opj_codestream_info_t *cstr_info); /** Creates a J2K/JP2 compression structure @param format Coder to select @return Returns a handle to a compressor if successful, returns NULL otherwise */ -OPJ_API opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format); +OPJ_API opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format); + /** -Destroy a compressor handle -@param cinfo compressor handle to destroy +Destroy a decompressor handle +@param dinfo decompressor handle to destroy */ -OPJ_API void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo); +OPJ_API void OPJ_CALLCONV opj_destroy_codec(opj_codec_t * p_codec); + /** Set encoding parameters to default values, that means :
      @@ -873,13 +963,70 @@ Set encoding parameters to default values, that means : @param parameters Compression parameters */ OPJ_API void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters); + +/** + * Sets the MCT matrix to use. + * + * @param parameters the parameters to change. + * @param pEncodingMatrix the encoding matrix. + * @param p_dc_shift the dc shift coefficients to use. + * @param pNbComp the number of components of the image. + * + * @return true if the parameters could be set. + */ +OPJ_API bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp); + +/** + * Restricts the decoding to the given image area. + * + * @param parameters the parameters to update. + * @param p_start_x the starting x position of the area to decode. + * @param p_start_y the starting y position of the area to decode. + * @param p_end_x the x end position of the area to decode. + * @param p_end_x the y end position of the area to decode. + */ +OPJ_API bool OPJ_CALLCONV opj_restrict_decoding (opj_dparameters_t *parameters,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y); + +#ifdef USE_OPJ_DEPRECATED +#ifdef _MSC_VER +#pragma message ("warning, opj_setup_encoder is deprecated") +#else +#warning "warning, opj_setup_encoder is deprecated" +#endif /** Setup the encoder parameters using the current image and using user parameters. @param cinfo Compressor handle @param parameters Compression parameters @param image Input filled image */ -OPJ_API void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image); +OPJ_API bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image); +#endif + +/** + * Decodes an image header. + * + * @param p_codec codec to use to decode the image. + * @param p_image pointer to a previously created image. + * @param p_tile_x0 pointer to a value that will hold the reference point x0 of the tiling grid. + * @param p_tile_y0 pointer to a value that will hold the reference point y0 of the tiling grid. + * @param p_tile_width pointer to a value that will hold the size in x of a tile in the grid. + * @param p_tile_height pointer to a value that will hold the size in y of a tile in the grid. + * @param p_nb_tiles_x pointer to a value that will hold the number of tiles in the x direction. + * @param p_nb_tiles_y pointer to a value that will hold the number of tiles in the y direction. + */ +OPJ_API bool OPJ_CALLCONV opj_read_header ( + opj_codec_t *p_codec, + opj_image_t ** p_image, + OPJ_INT32 * p_tile_x0, + OPJ_INT32 * p_tile_y0, + OPJ_UINT32 * p_tile_width, + OPJ_UINT32 * p_tile_height, + OPJ_UINT32 * p_nb_tiles_x, + OPJ_UINT32 * p_nb_tiles_y, + opj_stream_t *p_cio); + + +OPJ_API bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio); /** Encode an image into a JPEG-2000 codestream @param cinfo compressor handle @@ -888,7 +1035,11 @@ Encode an image into a JPEG-2000 codestream @param index Depreacted -> Set to NULL. To extract index, used opj_encode_wci() @return Returns true if successful, returns false otherwise */ -OPJ_API bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index); +OPJ_API bool OPJ_CALLCONV opj_encode(opj_codec_t *cinfo, opj_stream_t * cio); + +OPJ_API bool OPJ_CALLCONV opj_start_compress (opj_codec_t *p_codec,opj_image_t * p_image,opj_stream_t *p_cio); + +OPJ_API bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,opj_stream_t *p_cio); /** Encode an image into a JPEG-2000 codestream and extract the codestream information @param cinfo compressor handle @@ -897,15 +1048,32 @@ Encode an image into a JPEG-2000 codestream and extract the codestream informati @param cstr_info Codestream information structure if needed afterwards, NULL otherwise @return Returns true if successful, returns false otherwise */ -OPJ_API bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info); +//OPJ_API bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_stream_t cio, opj_image_t *image, opj_codestream_info_t *cstr_info); /** Destroy Codestream information after compression or decompression @param cstr_info Codestream information structure */ OPJ_API void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info); +//============================================================================== +//============================================================================== +/** profiling part */ +#ifdef _PROFILE +void _ProfInit(void); +void _ProfPrint(void); +#define PROFINIT() _ProfInit(); +#define PROFSAVE(file) _ProfSave(file); +#define PROFPRINT() _ProfPrint(); +#else +#define PROFINIT() +#define PROFSAVE(file) +#define PROFPRINT() +#endif // !_PROFILE + #ifdef __cplusplus } #endif #endif /* OPENJPEG_H */ + + diff --git a/libopenjpeg/opj_configure.h b/libopenjpeg/opj_configure.h new file mode 100644 index 00000000..60058cbd --- /dev/null +++ b/libopenjpeg/opj_configure.h @@ -0,0 +1,16 @@ +/* + * here is where system comupted values get stored these values should only + * change when the target compile platform changes + */ + +/* what byte order */ +#ifndef __OPJ_CONFIGURE_H +#define __OPJ_CONFIGURE_H +/* #undef CMAKE_WORDS_BIGENDIAN */ +#ifdef CMAKE_WORDS_BIGENDIAN + #define OPJ_BIG_ENDIAN +#else + #define OPJ_LITTLE_ENDIAN +#endif + +#endif /* __OPJ_CONFIGURE_H */ diff --git a/libopenjpeg/opj_includes.h b/libopenjpeg/opj_includes.h index 80d43df9..d4ed1ae1 100644 --- a/libopenjpeg/opj_includes.h +++ b/libopenjpeg/opj_includes.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -40,13 +41,13 @@ #include #include #include +#include /* ========================================================== OpenJPEG interface ========================================================== */ -#include "openjpeg.h" /* ========================================================== @@ -100,33 +101,4 @@ static INLINE long lrintf(float f){ } #endif -#include "j2k_lib.h" -#include "opj_malloc.h" -#include "event.h" -#include "cio.h" - -#include "image.h" -#include "j2k.h" -#include "jp2.h" -#include "jpt.h" - -#include "mqc.h" -#include "raw.h" -#include "bio.h" -#include "tgt.h" -#include "pi.h" -#include "tcd.h" -#include "t1.h" -#include "dwt.h" -#include "t2.h" -#include "mct.h" -#include "int.h" -#include "fix.h" - -/* JPWL>> */ -#ifdef USE_JPWL -#include "../jpwl/jpwl.h" -#endif /* USE_JPWL */ -/* < - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef __OPJ_MALLOC_H -#define __OPJ_MALLOC_H -/** -@file opj_malloc.h -@brief Internal functions - -The functions in opj_malloc.h are internal utilities used for memory management. -*/ - -/** @defgroup MISC MISC - Miscellaneous internal functions */ -/*@{*/ - -/** @name Exported functions */ -/*@{*/ -/* ----------------------------------------------------------------------- */ - -/** -Allocate an uninitialized memory block -@param size Bytes to allocate -@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available -*/ -#define opj_malloc(size) malloc(size) - -/** -Allocate a memory block with elements initialized to 0 -@param num Blocks to allocate -@param size Bytes per block to allocate -@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available -*/ -#define opj_calloc(num, size) calloc(num, size) - -/** -Allocate memory aligned to a 16 byte boundry -@param size Bytes to allocate -@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available -*/ -/* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */ -#ifdef WIN32 - /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */ - #ifdef __GNUC__ - #include - #define HAVE_MM_MALLOC - #else /* MSVC, Intel C++ */ - #include - #ifdef _mm_malloc - #define HAVE_MM_MALLOC - #endif - #endif -#else /* Not WIN32 */ - #if defined(__sun) - #define HAVE_MEMALIGN - /* Linux x86_64 and OSX always align allocations to 16 bytes */ - #elif !defined(__amd64__) && !defined(__APPLE__) - #define HAVE_MEMALIGN - #include - #endif -#endif - -#define opj_aligned_malloc(size) malloc(size) -#define opj_aligned_free(m) free(m) - -#ifdef HAVE_MM_MALLOC - #undef opj_aligned_malloc - #define opj_aligned_malloc(size) _mm_malloc(size, 16) - #undef opj_aligned_free - #define opj_aligned_free(m) _mm_free(m) -#endif - -#ifdef HAVE_MEMALIGN - extern void* memalign(size_t, size_t); - #undef opj_aligned_malloc - #define opj_aligned_malloc(size) memalign(16, (size)) - #undef opj_aligned_free - #define opj_aligned_free(m) free(m) -#endif - -#ifdef HAVE_POSIX_MEMALIGN - #undef opj_aligned_malloc - extern int posix_memalign(void**, size_t, size_t); - - static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){ - void* mem = NULL; - posix_memalign(&mem, 16, size); - return mem; - } - #undef opj_aligned_free - #define opj_aligned_free(m) free(m) -#endif - -/** -Reallocate memory blocks. -@param memblock Pointer to previously allocated memory block -@param size New size in bytes -@return Returns a void pointer to the reallocated (and possibly moved) memory block -*/ -#define opj_realloc(m, s) realloc(m, s) - -/** -Deallocates or frees a memory block. -@param memblock Previously allocated memory block to be freed -*/ -#define opj_free(m) free(m) - -#ifdef __GNUC__ -#pragma GCC poison malloc calloc realloc free -#endif - -/* ----------------------------------------------------------------------- */ -/*@}*/ - -/*@}*/ - -#endif /* __OPJ_MALLOC_H */ - +/* + * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2007, Callum Lerwick + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __OPJ_MALLOC_H +#define __OPJ_MALLOC_H +/** +@file opj_malloc.h +@brief Internal functions + +The functions in opj_malloc.h are internal utilities used for memory management. +*/ +#include "openjpeg.h" +#include "opj_includes.h" +/** @defgroup MISC MISC - Miscellaneous internal functions */ +/*@{*/ + +/** @name Exported functions */ +/*@{*/ +/* ----------------------------------------------------------------------- */ + +/** +Allocate an uninitialized memory block +@param size Bytes to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +#define opj_malloc(size) malloc(size) +#define my_opj_malloc(size) malloc(size) + +/** +Allocate a memory block with elements initialized to 0 +@param num Blocks to allocate +@param size Bytes per block to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +#define opj_calloc(num, size) calloc(num, size) + +/** +Allocate memory aligned to a 16 byte boundry +@param size Bytes to allocate +@return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available +*/ +/* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */ +#ifdef WIN32 + /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */ + #ifdef __GNUC__ + #include + #define HAVE_MM_MALLOC + #else /* MSVC, Intel C++ */ + #include + #ifdef _mm_malloc + #define HAVE_MM_MALLOC + #endif + #endif +#else /* Not WIN32 */ + #if defined(__sun) + #define HAVE_MEMALIGN + /* Linux x86_64 and OSX always align allocations to 16 bytes */ + #elif !defined(__amd64__) && !defined(__APPLE__) + /* FIXME: Yes, this is a big assumption */ + #define HAVE_POSIX_MEMALIGN + #endif +#endif + +#define opj_aligned_malloc(size) malloc(size) +#define opj_aligned_free(m) free(m) + +#ifdef HAVE_MM_MALLOC + #undef opj_aligned_malloc + #define opj_aligned_malloc(size) _mm_malloc(size, 16) + #undef opj_aligned_free + #define opj_aligned_free(m) _mm_free(m) +#endif + +#ifdef HAVE_MEMALIGN + extern void* memalign(size_t, size_t); + #undef opj_aligned_malloc + #define opj_aligned_malloc(size) memalign(16, (size)) + #undef opj_aligned_free + #define opj_aligned_free(m) free(m) +#endif + +#ifdef HAVE_POSIX_MEMALIGN + #undef opj_aligned_malloc + extern int posix_memalign(void**, size_t, size_t); + + static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){ + void* mem = NULL; + posix_memalign(&mem, 16, size); + return mem; + } + #undef opj_aligned_free + #define opj_aligned_free(m) free(m) +#endif + +/** +Reallocate memory blocks. +@param memblock Pointer to previously allocated memory block +@param size New size in bytes +@return Returns a void pointer to the reallocated (and possibly moved) memory block +*/ +#define opj_realloc(m, s) realloc(m, s) +#define my_opj_realloc(m,s) realloc(m,s) + + +/** +Deallocates or frees a memory block. +@param memblock Previously allocated memory block to be freed +*/ +#define opj_free(m) free(m) + +#ifdef __GNUC__ +#pragma GCC poison malloc calloc realloc free +#endif + +/* ----------------------------------------------------------------------- */ +/*@}*/ + +/*@}*/ + +#endif /* __OPJ_MALLOC_H */ + diff --git a/libopenjpeg/pi.c b/libopenjpeg/pi.c index ac7654c5..97efc38b 100644 --- a/libopenjpeg/pi.c +++ b/libopenjpeg/pi.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,8 +31,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" - +#include "pi.h" +#include "int.h" +#include "opj_malloc.h" +#include "j2k.h" /** @defgroup PI PI - Implementation of a packet iterator */ /*@{*/ @@ -69,6 +72,139 @@ Get next packet in component-precinct-resolution-layer order. */ static bool pi_next_cprl(opj_pi_iterator_t * pi); +/** + * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used). + * + * @param p_cp the coding parameters to modify + * @param p_tileno the tile index being concerned. + * @param p_tx0 X0 parameter for the tile + * @param p_tx1 X1 parameter for the tile + * @param p_ty0 Y0 parameter for the tile + * @param p_ty1 Y1 parameter for the tile + * @param p_max_prec the maximum precision for all the bands of the tile + * @param p_max_res the maximum number of resolutions for all the poc inside the tile. + * @param dx_min the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min the minimum dy of all the components of all the resolutions for the tile. + */ +void pi_update_encode_poc_and_final ( + opj_cp_t *p_cp, + OPJ_UINT32 p_tileno, + OPJ_INT32 p_tx0, + OPJ_INT32 p_tx1, + OPJ_INT32 p_ty0, + OPJ_INT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min); + +/** + * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used). + * + * @param p_cp the coding parameters to modify + * @param p_tileno the tile index being concerned. + * @param p_tx0 X0 parameter for the tile + * @param p_tx1 X1 parameter for the tile + * @param p_ty0 Y0 parameter for the tile + * @param p_ty1 Y1 parameter for the tile + * @param p_max_prec the maximum precision for all the bands of the tile + * @param p_max_res the maximum number of resolutions for all the poc inside the tile. + * @param dx_min the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min the minimum dy of all the components of all the resolutions for the tile. + */ +void pi_update_encode_not_poc ( + opj_cp_t *p_cp, + OPJ_UINT32 p_num_comps, + OPJ_UINT32 p_tileno, + OPJ_INT32 p_tx0, + OPJ_INT32 p_tx1, + OPJ_INT32 p_ty0, + OPJ_INT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min); + +/** + * Gets the encoding parameters needed to update the coding parameters and all the pocs. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param tileno the tile index of the tile being encoded. + * @param p_tx0 pointer that will hold the X0 parameter for the tile + * @param p_tx1 pointer that will hold the X1 parameter for the tile + * @param p_ty0 pointer that will hold the Y0 parameter for the tile + * @param p_ty1 pointer that will hold the Y1 parameter for the tile + * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile + * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile. + * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile. + */ +void get_encoding_parameters( + const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + OPJ_INT32 * p_tx0, + OPJ_INT32 * p_tx1, + OPJ_INT32 * p_ty0, + OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res + ); + +/** + * Gets the encoding parameters needed to update the coding parameters and all the pocs. + * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well. + * the last parameter of the function should be an array of pointers of size nb components, each pointer leading + * to an area of size 4 * max_res. The data is stored inside this area with the following pattern : + * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ... + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param tileno the tile index of the tile being encoded. + * @param p_tx0 pointer that will hold the X0 parameter for the tile + * @param p_tx1 pointer that will hold the X1 parameter for the tile + * @param p_ty0 pointer that will hold the Y0 parameter for the tile + * @param p_ty1 pointer that will hold the Y1 parameter for the tile + * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile + * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile. + * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile. + * @param p_resolutions pointer to an area corresponding to the one described above. + */ +void get_all_encoding_parameters( + const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + OPJ_INT32 * p_tx0, + OPJ_INT32 * p_tx1, + OPJ_INT32 * p_ty0, + OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res, + OPJ_UINT32 ** p_resolutions + ); +/** + * Allocates memory for a packet iterator. Data and data sizes are set by this operation. + * No other data is set. The include section of the packet iterator is not allocated. + * + * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant. + * @param p_cp the coding parameters. + * @param p_tile_no the index of the tile from which creating the packet iterator. + */ +opj_pi_iterator_t * pi_create( + const opj_image_t *image, + const opj_cp_t *cp, + OPJ_UINT32 tileno + ); +void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res); +void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res); + + /*@}*/ /*@}*/ @@ -80,9 +216,9 @@ static bool pi_next_cprl(opj_pi_iterator_t * pi); */ static bool pi_next_lrcp(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; + opj_pi_comp_t *comp = 00; + opj_pi_resolution_t *res = 00; + OPJ_UINT32 index = 0; if (!pi->first) { comp = &pi->comps[pi->compno]; @@ -120,9 +256,9 @@ LABEL_SKIP:; } static bool pi_next_rlcp(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; + opj_pi_comp_t *comp = 00; + opj_pi_resolution_t *res = 00; + OPJ_UINT32 index = 0; if (!pi->first) { comp = &pi->comps[pi->compno]; @@ -159,21 +295,21 @@ LABEL_SKIP:; } static bool pi_next_rpcl(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; + opj_pi_comp_t *comp = 00; + opj_pi_resolution_t *res = 00; + OPJ_UINT32 index = 0; if (!pi->first) { goto LABEL_SKIP; } else { - int compno, resno; + OPJ_UINT32 compno, resno; pi->first = 0; pi->dx = 0; pi->dy = 0; for (compno = 0; compno < pi->numcomps; compno++) { comp = &pi->comps[compno]; for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; + OPJ_UINT32 dx, dy; res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); @@ -192,11 +328,11 @@ if (!pi->tp_on){ for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) { for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - int levelno; - int trx0, try0; - int trx1, try1; - int rpx, rpy; - int prci, prcj; + OPJ_UINT32 levelno; + OPJ_INT32 trx0, try0; + OPJ_INT32 trx1, try1; + OPJ_UINT32 rpx, rpy; + OPJ_INT32 prci, prcj; comp = &pi->comps[pi->compno]; if (pi->resno >= comp->numresolutions) { continue; @@ -216,7 +352,7 @@ if (!pi->tp_on){ continue; } - if ((res->pw==0)||(res->pw==0)) continue; + if ((res->pw==0)||(res->ph==0)) continue; if ((trx0==trx1)||(try0==try1)) continue; @@ -242,22 +378,22 @@ LABEL_SKIP:; } static bool pi_next_pcrl(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; + opj_pi_comp_t *comp = 00; + opj_pi_resolution_t *res = 00; + OPJ_UINT32 index = 0; if (!pi->first) { comp = &pi->comps[pi->compno]; goto LABEL_SKIP; } else { - int compno, resno; + OPJ_UINT32 compno, resno; pi->first = 0; pi->dx = 0; pi->dy = 0; for (compno = 0; compno < pi->numcomps; compno++) { comp = &pi->comps[compno]; for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; + OPJ_UINT32 dx, dy; res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); @@ -276,12 +412,13 @@ static bool pi_next_pcrl(opj_pi_iterator_t * pi) { for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { comp = &pi->comps[pi->compno]; - for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { - int levelno; - int trx0, try0; - int trx1, try1; - int rpx, rpy; - int prci, prcj; + // TODO + for (pi->resno = pi->poc.resno0; pi->resno < uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { + OPJ_UINT32 levelno; + OPJ_INT32 trx0, try0; + OPJ_INT32 trx1, try1; + OPJ_UINT32 rpx, rpy; + OPJ_INT32 prci, prcj; res = &comp->resolutions[pi->resno]; levelno = comp->numresolutions - 1 - pi->resno; trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); @@ -297,7 +434,7 @@ static bool pi_next_pcrl(opj_pi_iterator_t * pi) { continue; } - if ((res->pw==0)||(res->pw==0)) continue; + if ((res->pw==0)||(res->ph==0)) continue; if ((trx0==trx1)||(try0==try1)) continue; @@ -323,9 +460,9 @@ LABEL_SKIP:; } static bool pi_next_cprl(opj_pi_iterator_t * pi) { - opj_pi_comp_t *comp = NULL; - opj_pi_resolution_t *res = NULL; - long index = 0; + opj_pi_comp_t *comp = 00; + opj_pi_resolution_t *res = 00; + OPJ_UINT32 index = 0; if (!pi->first) { comp = &pi->comps[pi->compno]; @@ -335,12 +472,12 @@ static bool pi_next_cprl(opj_pi_iterator_t * pi) { } for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { - int resno; + OPJ_UINT32 resno; comp = &pi->comps[pi->compno]; pi->dx = 0; pi->dy = 0; for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; + OPJ_UINT32 dx, dy; res = &comp->resolutions[resno]; dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); @@ -355,12 +492,13 @@ static bool pi_next_cprl(opj_pi_iterator_t * pi) { } for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) { for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) { - for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { - int levelno; - int trx0, try0; - int trx1, try1; - int rpx, rpy; - int prci, prcj; + // TODO + for (pi->resno = pi->poc.resno0; pi->resno < uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { + OPJ_UINT32 levelno; + OPJ_INT32 trx0, try0; + OPJ_INT32 trx1, try1; + OPJ_UINT32 rpx, rpy; + OPJ_INT32 prci, prcj; res = &comp->resolutions[pi->resno]; levelno = comp->numresolutions - 1 - pi->resno; trx0 = int_ceildiv(pi->tx0, comp->dx << levelno); @@ -406,304 +544,1095 @@ LABEL_SKIP:; Packet iterator interface ========================================================== */ +opj_pi_iterator_t *pi_create_decode( + opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no + ) +{ + // loop + OPJ_UINT32 pino; + OPJ_UINT32 compno, resno; + + // to store w, h, dx and dy fro all components and resolutions + OPJ_UINT32 * l_tmp_data; + OPJ_UINT32 ** l_tmp_ptr; + + // encoding prameters to set + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1; + OPJ_UINT32 l_dx_min,l_dy_min; + OPJ_UINT32 l_bound; + OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ; + OPJ_UINT32 l_data_stride; -opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) { - int p, q; - int compno, resno, pino; - opj_pi_iterator_t *pi = NULL; - opj_tcp_t *tcp = NULL; - opj_tccp_t *tccp = NULL; + // pointers + opj_pi_iterator_t *l_pi = 00; + opj_tcp_t *l_tcp = 00; + const opj_tccp_t *l_tccp = 00; + opj_pi_comp_t *l_current_comp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_pi_iterator_t * l_current_pi = 00; + OPJ_UINT32 * l_encoding_value_ptr = 00; + + // preconditions in debug + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); - tcp = &cp->tcps[tileno]; - - pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t)); - if(!pi) { - /* TODO: throw an error */ - return NULL; + // initializations + l_tcp = &p_cp->tcps[p_tile_no]; + l_bound = l_tcp->numpocs+1; + + l_data_stride = 4 * J2K_MAXRLVLS; + l_tmp_data = opj_malloc(l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32)); + if + (! l_tmp_data) + { + return 00; + } + l_tmp_ptr = opj_malloc(p_image->numcomps * sizeof(OPJ_UINT32 *)); + if + (! l_tmp_ptr) + { + opj_free(l_tmp_data); + return 00; } - for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */ - int maxres = 0; - int maxprec = 0; - p = tileno % cp->tw; - q = tileno / cp->tw; - - pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0); - pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0); - pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); - pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); - pi[pino].numcomps = image->numcomps; - - pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t)); - if(!pi[pino].comps) { - /* TODO: throw an error */ - pi_destroy(pi, cp, tileno); - return NULL; - } - - for (compno = 0; compno < pi->numcomps; compno++) { - int tcx0, tcy0, tcx1, tcy1; - opj_pi_comp_t *comp = &pi[pino].comps[compno]; - tccp = &tcp->tccps[compno]; - comp->dx = image->comps[compno].dx; - comp->dy = image->comps[compno].dy; - comp->numresolutions = tccp->numresolutions; - - comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t)); - if(!comp->resolutions) { - /* TODO: throw an error */ - pi_destroy(pi, cp, tileno); - return NULL; - } - - tcx0 = int_ceildiv(pi->tx0, comp->dx); - tcy0 = int_ceildiv(pi->ty0, comp->dy); - tcx1 = int_ceildiv(pi->tx1, comp->dx); - tcy1 = int_ceildiv(pi->ty1, comp->dy); - if (comp->numresolutions > maxres) { - maxres = comp->numresolutions; - } - - for (resno = 0; resno < comp->numresolutions; resno++) { - int levelno; - int rx0, ry0, rx1, ry1; - int px0, py0, px1, py1; - opj_pi_resolution_t *res = &comp->resolutions[resno]; - if (tccp->csty & J2K_CCP_CSTY_PRT) { - res->pdx = tccp->prcw[resno]; - res->pdy = tccp->prch[resno]; - } else { - res->pdx = 15; - res->pdy = 15; - } - levelno = comp->numresolutions - 1 - resno; - rx0 = int_ceildivpow2(tcx0, levelno); - ry0 = int_ceildivpow2(tcy0, levelno); - rx1 = int_ceildivpow2(tcx1, levelno); - ry1 = int_ceildivpow2(tcy1, levelno); - px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; - py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; - px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; - py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; - res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); - res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); - - if (res->pw*res->ph > maxprec) { - maxprec = res->pw*res->ph; - } - - } - } - - tccp = &tcp->tccps[0]; - pi[pino].step_p = 1; - pi[pino].step_c = maxprec * pi[pino].step_p; - pi[pino].step_r = image->numcomps * pi[pino].step_c; - pi[pino].step_l = maxres * pi[pino].step_r; - - if (pino == 0) { - pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int)); - if(!pi[pino].include) { - /* TODO: throw an error */ - pi_destroy(pi, cp, tileno); - return NULL; - } - } - else { - pi[pino].include = pi[pino - 1].include; - } - - if (tcp->POC == 0) { - pi[pino].first = 1; - pi[pino].poc.resno0 = 0; - pi[pino].poc.compno0 = 0; - pi[pino].poc.layno1 = tcp->numlayers; - pi[pino].poc.resno1 = maxres; - pi[pino].poc.compno1 = image->numcomps; - pi[pino].poc.prg = tcp->prg; - } else { - pi[pino].first = 1; - pi[pino].poc.resno0 = tcp->pocs[pino].resno0; - pi[pino].poc.compno0 = tcp->pocs[pino].compno0; - pi[pino].poc.layno1 = tcp->pocs[pino].layno1; - pi[pino].poc.resno1 = tcp->pocs[pino].resno1; - pi[pino].poc.compno1 = tcp->pocs[pino].compno1; - pi[pino].poc.prg = tcp->pocs[pino].prg; - } - pi[pino].poc.layno0 = 0; - pi[pino].poc.precno0 = 0; - pi[pino].poc.precno1 = maxprec; - + // memory allocation for pi + l_pi = pi_create(p_image,p_cp,p_tile_no); + if + (!l_pi) + { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + return 00; } - return pi; + l_encoding_value_ptr = l_tmp_data; + // update pointer array + for + (compno = 0; compno < p_image->numcomps; ++compno) + { + l_tmp_ptr[compno] = l_encoding_value_ptr; + l_encoding_value_ptr += l_data_stride; + } + // get encoding parameters + get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr); + + // step calculations + l_step_p = 1; + l_step_c = l_max_prec * l_step_p; + l_step_r = p_image->numcomps * l_step_c; + l_step_l = l_max_res * l_step_r; + + // set values for first packet iterator + l_current_pi = l_pi; + + // memory allocation for include + l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16)); + if + (!l_current_pi->include) + { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + pi_destroy(l_pi, l_bound); + return 00; + } + memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16)); + + // special treatment for the first packet iterator + l_current_comp = l_current_pi->comps; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + + //l_current_pi->dx = l_img_comp->dx; + //l_current_pi->dy = l_img_comp->dy; + + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by pi_create */ + for + (compno = 0; compno < l_current_pi->numcomps; ++compno) + { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for + (resno = 0; resno < l_current_comp->numresolutions; resno++) + { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + ++l_current_pi; + + for + (pino = 1 ; pinocomps; + opj_image_comp_t * l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + //l_current_pi->dx = l_dx_min; + //l_current_pi->dy = l_dy_min; + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by pi_create */ + for + (compno = 0; compno < l_current_pi->numcomps; ++compno) + { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for + (resno = 0; resno < l_current_comp->numresolutions; resno++) + { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + // special treatment + l_current_pi->include = (l_current_pi-1)->include; + ++l_current_pi; + } + opj_free(l_tmp_data); + l_tmp_data = 00; + opj_free(l_tmp_ptr); + l_tmp_ptr = 00; + if + (l_tcp->POC) + { + pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res); + } + else + { + pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res); + } + return l_pi; +} + +void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res) +{ + // loop + OPJ_UINT32 pino; + + // encoding prameters to set + OPJ_UINT32 l_bound; + + opj_pi_iterator_t * l_current_pi = 00; + opj_poc_t* l_current_poc = 0; + + // preconditions in debug + assert(p_pi != 00); + assert(p_tcp != 00); + + // initializations + l_bound = p_tcp->numpocs+1; + l_current_pi = p_pi; + l_current_poc = p_tcp->pocs; + + for + (pino = 0;pinopoc.prg = l_current_poc->prg; + l_current_pi->first = 1; + + l_current_pi->poc.resno0 = l_current_poc->resno0; + l_current_pi->poc.compno0 = l_current_poc->compno0; + l_current_pi->poc.layno0 = 0; + l_current_pi->poc.precno0 = 0; + l_current_pi->poc.resno1 = l_current_poc->resno1; + l_current_pi->poc.compno1 = l_current_poc->compno1; + l_current_pi->poc.layno1 = l_current_poc->layno1; + l_current_pi->poc.precno1 = p_max_precision; + ++l_current_pi; + ++l_current_poc; + } +} + +void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res) +{ + // loop + OPJ_UINT32 pino; + + // encoding prameters to set + OPJ_UINT32 l_bound; + + opj_pi_iterator_t * l_current_pi = 00; + // preconditions in debug + assert(p_tcp != 00); + assert(p_pi != 00); + + // initializations + l_bound = p_tcp->numpocs+1; + l_current_pi = p_pi; + + for + (pino = 0;pinopoc.prg = p_tcp->prg; + l_current_pi->first = 1; + l_current_pi->poc.resno0 = 0; + l_current_pi->poc.compno0 = 0; + l_current_pi->poc.layno0 = 0; + l_current_pi->poc.precno0 = 0; + l_current_pi->poc.resno1 = p_max_res; + l_current_pi->poc.compno1 = l_current_pi->numcomps; + l_current_pi->poc.layno1 = p_tcp->numlayers; + l_current_pi->poc.precno1 = p_max_precision; + ++l_current_pi; + } +} + +/** + * Creates a packet iterator for encoding. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param p_tile_no index of the tile being encoded. + * @param p_t2_mode the type of pass for generating the packet iterator + * @return a list of packet iterator that points to the first packet of the tile (not true). +*/ +opj_pi_iterator_t *pi_initialise_encode( + const opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no, + J2K_T2_MODE p_t2_mode + ) +{ + // loop + OPJ_UINT32 pino; + OPJ_UINT32 compno, resno; + + // to store w, h, dx and dy fro all components and resolutions + OPJ_UINT32 * l_tmp_data; + OPJ_UINT32 ** l_tmp_ptr; + + // encoding prameters to set + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1; + OPJ_UINT32 l_dx_min,l_dy_min; + OPJ_UINT32 l_bound; + OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ; + OPJ_UINT32 l_data_stride; + + // pointers + opj_pi_iterator_t *l_pi = 00; + opj_tcp_t *l_tcp = 00; + const opj_tccp_t *l_tccp = 00; + opj_pi_comp_t *l_current_comp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_pi_iterator_t * l_current_pi = 00; + OPJ_UINT32 * l_encoding_value_ptr = 00; + + // preconditions in debug + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); + + // initializations + l_tcp = &p_cp->tcps[p_tile_no]; + l_bound = l_tcp->numpocs+1; + + l_data_stride = 4 * J2K_MAXRLVLS; + l_tmp_data = opj_malloc(l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32)); + if + (! l_tmp_data) + { + return 00; + } + l_tmp_ptr = opj_malloc(p_image->numcomps * sizeof(OPJ_UINT32 *)); + if + (! l_tmp_ptr) + { + opj_free(l_tmp_data); + return 00; + } + + // memory allocation for pi + l_pi = pi_create(p_image,p_cp,p_tile_no); + if + (!l_pi) + { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + return 00; + } + + l_encoding_value_ptr = l_tmp_data; + // update pointer array + for + (compno = 0; compno < p_image->numcomps; ++compno) + { + l_tmp_ptr[compno] = l_encoding_value_ptr; + l_encoding_value_ptr += l_data_stride; + } + // get encoding parameters + get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr); + + // step calculations + l_step_p = 1; + l_step_c = l_max_prec * l_step_p; + l_step_r = p_image->numcomps * l_step_c; + l_step_l = l_max_res * l_step_r; + + // set values for first packet iterator + l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on; + l_current_pi = l_pi; + + // memory allocation for include + l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16)); + if + (!l_current_pi->include) + { + opj_free(l_tmp_data); + opj_free(l_tmp_ptr); + pi_destroy(l_pi, l_bound); + return 00; + } + memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16)); + + // special treatment for the first packet iterator + l_current_comp = l_current_pi->comps; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + l_current_pi->dx = l_dx_min; + l_current_pi->dy = l_dy_min; + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by pi_create */ + for + (compno = 0; compno < l_current_pi->numcomps; ++compno) + { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for + (resno = 0; resno < l_current_comp->numresolutions; resno++) + { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + ++l_current_pi; + + for + (pino = 1 ; pinocomps; + opj_image_comp_t * l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; + + l_current_pi->tx0 = l_tx0; + l_current_pi->ty0 = l_ty0; + l_current_pi->tx1 = l_tx1; + l_current_pi->ty1 = l_ty1; + l_current_pi->dx = l_dx_min; + l_current_pi->dy = l_dy_min; + l_current_pi->step_p = l_step_p; + l_current_pi->step_c = l_step_c; + l_current_pi->step_r = l_step_r; + l_current_pi->step_l = l_step_l; + + /* allocation for components and number of components has already been calculated by pi_create */ + for + (compno = 0; compno < l_current_pi->numcomps; ++compno) + { + opj_pi_resolution_t *l_res = l_current_comp->resolutions; + l_encoding_value_ptr = l_tmp_ptr[compno]; + + l_current_comp->dx = l_img_comp->dx; + l_current_comp->dy = l_img_comp->dy; + /* resolutions have already been initialized */ + for + (resno = 0; resno < l_current_comp->numresolutions; resno++) + { + l_res->pdx = *(l_encoding_value_ptr++); + l_res->pdy = *(l_encoding_value_ptr++); + l_res->pw = *(l_encoding_value_ptr++); + l_res->ph = *(l_encoding_value_ptr++); + ++l_res; + } + ++l_current_comp; + ++l_img_comp; + ++l_tccp; + } + // special treatment + l_current_pi->include = (l_current_pi-1)->include; + ++l_current_pi; + } + opj_free(l_tmp_data); + l_tmp_data = 00; + opj_free(l_tmp_ptr); + l_tmp_ptr = 00; + if + (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) + { + pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min); + } + else + { + pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min); + } + return l_pi; +} + +/** + * Updates the encoding parameters of the codec. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param p_tile_no index of the tile being encoded. +*/ +void pi_update_encoding_parameters( + const opj_image_t *p_image, + opj_cp_t *p_cp, + OPJ_UINT32 p_tile_no + ) +{ + // encoding prameters to set + OPJ_UINT32 l_max_res; + OPJ_UINT32 l_max_prec; + OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1; + OPJ_UINT32 l_dx_min,l_dy_min; + + // pointers + opj_tcp_t *l_tcp = 00; + + // preconditions in debug + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tile_no < p_cp->tw * p_cp->th); + + l_tcp = &(p_cp->tcps[p_tile_no]); + // get encoding parameters + get_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res); + if + (l_tcp->POC) + { + pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min); + } + else + { + pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min); + } } -opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ - int p, q, pino; - int compno, resno; - int maxres = 0; - int maxprec = 0; - opj_pi_iterator_t *pi = NULL; - opj_tcp_t *tcp = NULL; - opj_tccp_t *tccp = NULL; +/** + * Gets the encoding parameters needed to update the coding parameters and all the pocs. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param p_tileno the tile index of the tile being encoded. + * @param p_tx0 pointer that will hold the X0 parameter for the tile + * @param p_tx1 pointer that will hold the X1 parameter for the tile + * @param p_ty0 pointer that will hold the Y0 parameter for the tile + * @param p_ty1 pointer that will hold the Y1 parameter for the tile + * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile + * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile. + * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile. + */ +void get_encoding_parameters( + const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 p_tileno, + OPJ_INT32 * p_tx0, + OPJ_INT32 * p_tx1, + OPJ_INT32 * p_ty0, + OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res + ) +{ + // loop + OPJ_UINT32 compno, resno; + // pointers + const opj_tcp_t *l_tcp = 00; + const opj_tccp_t * l_tccp = 00; + const opj_image_comp_t * l_img_comp = 00; - tcp = &cp->tcps[tileno]; + // position in x and y of tile + OPJ_UINT32 p, q; + + // preconditions in debug + assert(p_cp != 00); + assert(p_image != 00); + assert(p_tileno < p_cp->tw * p_cp->th); - pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t)); - if(!pi) { return NULL;} - pi->tp_on = cp->tp_on; + // initializations + l_tcp = &p_cp->tcps [p_tileno]; + l_img_comp = p_image->comps; + l_tccp = l_tcp->tccps; - for(pino = 0;pino < tcp->numpocs+1 ; pino ++){ - p = tileno % cp->tw; - q = tileno / cp->tw; + /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */ + p = p_tileno % p_cp->tw; + q = p_tileno / p_cp->tw; + + // find extent of tile + *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0); + *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1); + *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0); + *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1); + + // max precision is 0 (can only grow) + *p_max_prec = 0; + *p_max_res = 0; - pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0); - pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0); - pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); - pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); - pi[pino].numcomps = image->numcomps; - - pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t)); - if(!pi[pino].comps) { - pi_destroy(pi, cp, tileno); - return NULL; + // take the largest value for dx_min and dy_min + *p_dx_min = 0x7fffffff; + *p_dy_min = 0x7fffffff; + + for + (compno = 0; compno < p_image->numcomps; ++compno) + { + // aritmetic variables to calculate + OPJ_UINT32 l_level_no; + OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1; + OPJ_INT32 l_px0, l_py0, l_px1, py1; + OPJ_UINT32 l_pdx, l_pdy; + OPJ_UINT32 l_pw, l_ph; + OPJ_UINT32 l_product; + OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; + + l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx); + l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy); + l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx); + l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy); + if + (l_tccp->numresolutions > *p_max_res) + { + *p_max_res = l_tccp->numresolutions; } - - for (compno = 0; compno < pi[pino].numcomps; compno++) { - int tcx0, tcy0, tcx1, tcy1; - opj_pi_comp_t *comp = &pi[pino].comps[compno]; - tccp = &tcp->tccps[compno]; - comp->dx = image->comps[compno].dx; - comp->dy = image->comps[compno].dy; - comp->numresolutions = tccp->numresolutions; + // use custom size for precincts + for + (resno = 0; resno < l_tccp->numresolutions; ++resno) + { + OPJ_UINT32 l_dx, l_dy; + // precinct width and height + l_pdx = l_tccp->prcw[resno]; + l_pdy = l_tccp->prch[resno]; - comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t)); - if(!comp->resolutions) { - pi_destroy(pi, cp, tileno); - return NULL; - } - - tcx0 = int_ceildiv(pi[pino].tx0, comp->dx); - tcy0 = int_ceildiv(pi[pino].ty0, comp->dy); - tcx1 = int_ceildiv(pi[pino].tx1, comp->dx); - tcy1 = int_ceildiv(pi[pino].ty1, comp->dy); - if (comp->numresolutions > maxres) { - maxres = comp->numresolutions; - } - - for (resno = 0; resno < comp->numresolutions; resno++) { - int levelno; - int rx0, ry0, rx1, ry1; - int px0, py0, px1, py1; - opj_pi_resolution_t *res = &comp->resolutions[resno]; - if (tccp->csty & J2K_CCP_CSTY_PRT) { - res->pdx = tccp->prcw[resno]; - res->pdy = tccp->prch[resno]; - } else { - res->pdx = 15; - res->pdy = 15; - } - levelno = comp->numresolutions - 1 - resno; - rx0 = int_ceildivpow2(tcx0, levelno); - ry0 = int_ceildivpow2(tcy0, levelno); - rx1 = int_ceildivpow2(tcx1, levelno); - ry1 = int_ceildivpow2(tcy1, levelno); - px0 = int_floordivpow2(rx0, res->pdx) << res->pdx; - py0 = int_floordivpow2(ry0, res->pdy) << res->pdy; - px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx; - py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy; - res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); - res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); - - if (res->pw*res->ph > maxprec) { - maxprec = res->pw * res->ph; - } + l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno)); + l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno)); + // take the minimum size for dx for each comp and resolution + *p_dx_min = uint_min(*p_dx_min, l_dx); + *p_dy_min = uint_min(*p_dy_min, l_dy); + // various calculations of extents + l_level_no = l_tccp->numresolutions - 1 - resno; + l_rx0 = int_ceildivpow2(l_tcx0, l_level_no); + l_ry0 = int_ceildivpow2(l_tcy0, l_level_no); + l_rx1 = int_ceildivpow2(l_tcx1, l_level_no); + l_ry1 = int_ceildivpow2(l_tcy1, l_level_no); + l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx; + l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy; + l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx; + py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy; + l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx); + l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy); + l_product = l_pw * l_ph; + // update precision + if + (l_product > *p_max_prec) + { + *p_max_prec = l_product; } } - - tccp = &tcp->tccps[0]; - pi[pino].step_p = 1; - pi[pino].step_c = maxprec * pi[pino].step_p; - pi[pino].step_r = image->numcomps * pi[pino].step_c; - pi[pino].step_l = maxres * pi[pino].step_r; - - for (compno = 0; compno < pi->numcomps; compno++) { - opj_pi_comp_t *comp = &pi->comps[compno]; - for (resno = 0; resno < comp->numresolutions; resno++) { - int dx, dy; - opj_pi_resolution_t *res = &comp->resolutions[resno]; - dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno)); - pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx); - pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy); - } - } - - if (pino == 0) { - pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int)); - if(!pi[pino].include) { - pi_destroy(pi, cp, tileno); - return NULL; - } - } - else { - pi[pino].include = pi[pino - 1].include; - } - - /* Generation of boundaries for each prog flag*/ - if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){ - tcp->pocs[pino].compS= tcp->pocs[pino].compno0; - tcp->pocs[pino].compE= tcp->pocs[pino].compno1; - tcp->pocs[pino].resS = tcp->pocs[pino].resno0; - tcp->pocs[pino].resE = tcp->pocs[pino].resno1; - tcp->pocs[pino].layE = tcp->pocs[pino].layno1; - tcp->pocs[pino].prg = tcp->pocs[pino].prg1; - if (pino > 0) - tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0; - }else { - tcp->pocs[pino].compS= 0; - tcp->pocs[pino].compE= image->numcomps; - tcp->pocs[pino].resS = 0; - tcp->pocs[pino].resE = maxres; - tcp->pocs[pino].layS = 0; - tcp->pocs[pino].layE = tcp->numlayers; - tcp->pocs[pino].prg = tcp->prg; - } - tcp->pocs[pino].prcS = 0; - tcp->pocs[pino].prcE = maxprec;; - tcp->pocs[pino].txS = pi[pino].tx0; - tcp->pocs[pino].txE = pi[pino].tx1; - tcp->pocs[pino].tyS = pi[pino].ty0; - tcp->pocs[pino].tyE = pi[pino].ty1; - tcp->pocs[pino].dx = pi[pino].dx; - tcp->pocs[pino].dy = pi[pino].dy; - } - return pi; + ++l_img_comp; + ++l_tccp; } +} +/** + * Gets the encoding parameters needed to update the coding parameters and all the pocs. + * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well. + * the last parameter of the function should be an array of pointers of size nb components, each pointer leading + * to an area of size 4 * max_res. The data is stored inside this area with the following pattern : + * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ... + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param tileno the tile index of the tile being encoded. + * @param p_tx0 pointer that will hold the X0 parameter for the tile + * @param p_tx1 pointer that will hold the X1 parameter for the tile + * @param p_ty0 pointer that will hold the Y0 parameter for the tile + * @param p_ty1 pointer that will hold the Y1 parameter for the tile + * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile + * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile. + * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile. + * @param p_resolutions pointer to an area corresponding to the one described above. + */ +void get_all_encoding_parameters( + const opj_image_t *p_image, + const opj_cp_t *p_cp, + OPJ_UINT32 tileno, + OPJ_INT32 * p_tx0, + OPJ_INT32 * p_tx1, + OPJ_INT32 * p_ty0, + OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_dx_min, + OPJ_UINT32 * p_dy_min, + OPJ_UINT32 * p_max_prec, + OPJ_UINT32 * p_max_res, + OPJ_UINT32 ** p_resolutions + ) +{ + // loop + OPJ_UINT32 compno, resno; + // pointers + const opj_tcp_t *tcp = 00; + const opj_tccp_t * l_tccp = 00; + const opj_image_comp_t * l_img_comp = 00; -void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) { - int compno, pino; - opj_tcp_t *tcp = &cp->tcps[tileno]; - if(pi) { - for (pino = 0; pino < tcp->numpocs + 1; pino++) { - if(pi[pino].comps) { - for (compno = 0; compno < pi->numcomps; compno++) { - opj_pi_comp_t *comp = &pi[pino].comps[compno]; - if(comp->resolutions) { - opj_free(comp->resolutions); - } - } - opj_free(pi[pino].comps); + // to store l_dx, l_dy, w and h for each resolution and component. + OPJ_UINT32 * lResolutionPtr; + + // position in x and y of tile + OPJ_UINT32 p, q; + + // preconditions in debug + assert(p_cp != 00); + assert(p_image != 00); + assert(tileno < p_cp->tw * p_cp->th); + + // initializations + tcp = &p_cp->tcps [tileno]; + l_tccp = tcp->tccps; + l_img_comp = p_image->comps; + + // position in x and y of tile + + p = tileno % p_cp->tw; + q = tileno / p_cp->tw; + + /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */ + *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0); + *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1); + *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0); + *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1); + + // max precision and resolution is 0 (can only grow) + *p_max_prec = 0; + *p_max_res = 0; + + // take the largest value for dx_min and dy_min + *p_dx_min = 0x7fffffff; + *p_dy_min = 0x7fffffff; + + for + (compno = 0; compno < p_image->numcomps; ++compno) + { + // aritmetic variables to calculate + OPJ_UINT32 l_level_no; + OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1; + OPJ_INT32 l_px0, l_py0, l_px1, py1; + OPJ_UINT32 l_product; + OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; + OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph; + + lResolutionPtr = p_resolutions[compno]; + + l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx); + l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy); + l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx); + l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy); + if + (l_tccp->numresolutions > *p_max_res) + { + *p_max_res = l_tccp->numresolutions; + } + + // use custom size for precincts + l_level_no = l_tccp->numresolutions - 1; + for + (resno = 0; resno < l_tccp->numresolutions; ++resno) + { + OPJ_UINT32 l_dx, l_dy; + // precinct width and height + l_pdx = l_tccp->prcw[resno]; + l_pdy = l_tccp->prch[resno]; + *lResolutionPtr++ = l_pdx; + *lResolutionPtr++ = l_pdy; + l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no)); + l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no)); + // take the minimum size for l_dx for each comp and resolution + *p_dx_min = int_min(*p_dx_min, l_dx); + *p_dy_min = int_min(*p_dy_min, l_dy); + // various calculations of extents + + l_rx0 = int_ceildivpow2(l_tcx0, l_level_no); + l_ry0 = int_ceildivpow2(l_tcy0, l_level_no); + l_rx1 = int_ceildivpow2(l_tcx1, l_level_no); + l_ry1 = int_ceildivpow2(l_tcy1, l_level_no); + l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx; + l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy; + l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx; + py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy; + l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx); + l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy); + *lResolutionPtr++ = l_pw; + *lResolutionPtr++ = l_ph; + l_product = l_pw * l_ph; + // update precision + if + (l_product > *p_max_prec) + { + *p_max_prec = l_product; } + --l_level_no; } - if(pi->include) { - opj_free(pi->include); + ++l_tccp; + ++l_img_comp; + } +} + +/** + * Allocates memory for a packet iterator. Data and data sizes are set by this operation. + * No other data is set. The include section of the packet iterator is not allocated. + * + * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant. + * @param p_cp the coding parameters. + * @param p_tile_no the index of the tile from which creating the packet iterator. + */ +opj_pi_iterator_t * pi_create( + const opj_image_t *image, + const opj_cp_t *cp, + OPJ_UINT32 tileno + ) +{ + // loop + OPJ_UINT32 pino, compno; + // number of poc in the p_pi + OPJ_UINT32 l_poc_bound; + + // pointers to tile coding parameters and components. + opj_pi_iterator_t *l_pi = 00; + opj_tcp_t *tcp = 00; + const opj_tccp_t *tccp = 00; + + // current packet iterator being allocated + opj_pi_iterator_t *l_current_pi = 00; + + // preconditions in debug + assert(cp != 00); + assert(image != 00); + assert(tileno < cp->tw * cp->th); + + // initializations + tcp = &cp->tcps[tileno]; + l_poc_bound = tcp->numpocs+1; + + + // memory allocations + l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t)); + + if + (!l_pi) + { + return 00; + } + memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t)); + l_current_pi = l_pi; + for + (pino = 0; pino < l_poc_bound ; ++pino) + { + l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t)); + if + (! l_current_pi->comps) + { + pi_destroy(l_pi, l_poc_bound); + return 00; } - opj_free(pi); + l_current_pi->numcomps = image->numcomps; + memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t)); + for + (compno = 0; compno < image->numcomps; ++compno) + { + opj_pi_comp_t *comp = &l_current_pi->comps[compno]; + tccp = &tcp->tccps[compno]; + comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t)); + if + (!comp->resolutions) + { + pi_destroy(l_pi, l_poc_bound); + return 00; + } + comp->numresolutions = tccp->numresolutions; + memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t)); + } + ++l_current_pi; + } + return l_pi; +} + +/** + * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used). + * + * @param p_cp the coding parameters to modify + * @param p_tileno the tile index being concerned. + * @param p_tx0 X0 parameter for the tile + * @param p_tx1 X1 parameter for the tile + * @param p_ty0 Y0 parameter for the tile + * @param p_ty1 Y1 parameter for the tile + * @param p_max_prec the maximum precision for all the bands of the tile + * @param p_max_res the maximum number of resolutions for all the poc inside the tile. + * @param dx_min the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min the minimum dy of all the components of all the resolutions for the tile. + */ +void pi_update_encode_poc_and_final ( + opj_cp_t *p_cp, + OPJ_UINT32 p_tileno, + OPJ_INT32 p_tx0, + OPJ_INT32 p_tx1, + OPJ_INT32 p_ty0, + OPJ_INT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min) +{ + // loop + OPJ_UINT32 pino; + // tile coding parameter + opj_tcp_t *l_tcp = 00; + // current poc being updated + opj_poc_t * l_current_poc = 00; + + // number of pocs + OPJ_UINT32 l_poc_bound; + + // preconditions in debug + assert(p_cp != 00); + assert(p_tileno < p_cp->tw * p_cp->th); + + // initializations + l_tcp = &p_cp->tcps [p_tileno]; + /* number of iterations in the loop */ + l_poc_bound = l_tcp->numpocs+1; + + // start at first element, and to make sure the compiler will not make a calculation each time in the loop + // store a pointer to the current element to modify rather than l_tcp->pocs[i] + l_current_poc = l_tcp->pocs; + + l_current_poc->compS = l_current_poc->compno0; + l_current_poc->compE = l_current_poc->compno1; + l_current_poc->resS = l_current_poc->resno0; + l_current_poc->resE = l_current_poc->resno1; + l_current_poc->layE = l_current_poc->layno1; + + // special treatment for the first element + l_current_poc->layS = 0; + l_current_poc->prg = l_current_poc->prg1; + l_current_poc->prcS = 0; + + l_current_poc->prcE = p_max_prec; + l_current_poc->txS = p_tx0; + l_current_poc->txE = p_tx1; + l_current_poc->tyS = p_ty0; + l_current_poc->tyE = p_ty1; + l_current_poc->dx = p_dx_min; + l_current_poc->dy = p_dy_min; + + ++ l_current_poc; + for + (pino = 1;pino < l_poc_bound ; ++pino) + { + l_current_poc->compS = l_current_poc->compno0; + l_current_poc->compE= l_current_poc->compno1; + l_current_poc->resS = l_current_poc->resno0; + l_current_poc->resE = l_current_poc->resno1; + l_current_poc->layE = l_current_poc->layno1; + l_current_poc->prg = l_current_poc->prg1; + l_current_poc->prcS = 0; + // special treatment here different from the first element + l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0; + + l_current_poc->prcE = p_max_prec; + l_current_poc->txS = p_tx0; + l_current_poc->txE = p_tx1; + l_current_poc->tyS = p_ty0; + l_current_poc->tyE = p_ty1; + l_current_poc->dx = p_dx_min; + l_current_poc->dy = p_dy_min; + ++ l_current_poc; + } +} + +/** + * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used). + * + * @param p_cp the coding parameters to modify + * @param p_tileno the tile index being concerned. + * @param p_tx0 X0 parameter for the tile + * @param p_tx1 X1 parameter for the tile + * @param p_ty0 Y0 parameter for the tile + * @param p_ty1 Y1 parameter for the tile + * @param p_max_prec the maximum precision for all the bands of the tile + * @param p_max_res the maximum number of resolutions for all the poc inside the tile. + * @param dx_min the minimum dx of all the components of all the resolutions for the tile. + * @param dy_min the minimum dy of all the components of all the resolutions for the tile. + */ +void pi_update_encode_not_poc ( + opj_cp_t *p_cp, + OPJ_UINT32 p_num_comps, + OPJ_UINT32 p_tileno, + OPJ_INT32 p_tx0, + OPJ_INT32 p_tx1, + OPJ_INT32 p_ty0, + OPJ_INT32 p_ty1, + OPJ_UINT32 p_max_prec, + OPJ_UINT32 p_max_res, + OPJ_UINT32 p_dx_min, + OPJ_UINT32 p_dy_min) +{ + // loop + OPJ_UINT32 pino; + // tile coding parameter + opj_tcp_t *l_tcp = 00; + // current poc being updated + opj_poc_t * l_current_poc = 00; + // number of pocs + OPJ_UINT32 l_poc_bound; + + // preconditions in debug + assert(p_cp != 00); + assert(p_tileno < p_cp->tw * p_cp->th); + + // initializations + l_tcp = &p_cp->tcps [p_tileno]; + + /* number of iterations in the loop */ + l_poc_bound = l_tcp->numpocs+1; + + // start at first element, and to make sure the compiler will not make a calculation each time in the loop + // store a pointer to the current element to modify rather than l_tcp->pocs[i] + l_current_poc = l_tcp->pocs; + + for + (pino = 0; pino < l_poc_bound ; ++pino) + { + l_current_poc->compS = 0; + l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/ + l_current_poc->resS = 0; + l_current_poc->resE = p_max_res; + l_current_poc->layS = 0; + l_current_poc->layE = l_tcp->numlayers; + l_current_poc->prg = l_tcp->prg; + l_current_poc->prcS = 0; + l_current_poc->prcE = p_max_prec; + l_current_poc->txS = p_tx0; + l_current_poc->txE = p_tx1; + l_current_poc->tyS = p_ty0; + l_current_poc->tyE = p_ty1; + l_current_poc->dx = p_dx_min; + l_current_poc->dy = p_dy_min; + ++ l_current_poc; + } +} + +/** + * Destroys a packet iterator array. + * + * @param p_pi the packet iterator array to destroy. + * @param p_nb_elements the number of elements in the array. + */ +void pi_destroy( + opj_pi_iterator_t *p_pi, + OPJ_UINT32 p_nb_elements) +{ + OPJ_UINT32 compno, pino; + opj_pi_iterator_t *l_current_pi = p_pi; + if + (p_pi) + { + if + (p_pi->include) + { + opj_free(p_pi->include); + p_pi->include = 00; + } + // TODO + for + (pino = 0; pino < p_nb_elements; ++pino) + { + if + (l_current_pi->comps) + { + opj_pi_comp_t *l_current_component = l_current_pi->comps; + for + (compno = 0; compno < l_current_pi->numcomps; compno++) + { + if + (l_current_component->resolutions) + { + opj_free(l_current_component->resolutions); + l_current_component->resolutions = 00; + } + ++l_current_component; + } + opj_free(l_current_pi->comps); + l_current_pi->comps = 0; + } + ++l_current_pi; + } + opj_free(p_pi); } } @@ -726,32 +1655,103 @@ bool pi_next(opj_pi_iterator_t * pi) { return false; } -bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp){ - char prog[4]; - int i; - int incr_top=1,resetX=0; +OPJ_INT32 pi_check_next_level(OPJ_INT32 pos,opj_cp_t *cp,OPJ_UINT32 tileno, OPJ_UINT32 pino, const OPJ_CHAR *prog) +{ + OPJ_INT32 i,l; + opj_tcp_t *tcps =&cp->tcps[tileno]; + opj_poc_t *tcp = &tcps->pocs[pino]; + if(pos>=0){ + for(i=pos;pos>=0;i--){ + switch(prog[i]){ + case 'R': + if(tcp->res_t==tcp->resE){ + l=pi_check_next_level(pos-1,cp,tileno,pino,prog); + if(l==1){ + return 1; + }else{ + return 0; + } + }else{ + return 1; + } + break; + case 'C': + if(tcp->comp_t==tcp->compE){ + l=pi_check_next_level(pos-1,cp,tileno,pino,prog); + if(l==1){ + return 1; + }else{ + return 0; + } + }else{ + return 1; + } + break; + case 'L': + if(tcp->lay_t==tcp->layE){ + l=pi_check_next_level(pos-1,cp,tileno,pino,prog); + if(l==1){ + return 1; + }else{ + return 0; + } + }else{ + return 1; + } + break; + case 'P': + switch(tcp->prg){ + case LRCP||RLCP: + if(tcp->prc_t == tcp->prcE){ + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + return 1; + }else{ + return 0; + } + }else{ + return 1; + } + break; + default: + if(tcp->tx0_t == tcp->txE){ + //TY + if(tcp->ty0_t == tcp->tyE){ + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + return 1; + }else{ + return 0; + } + }else{ + return 1; + }//TY + }else{ + return 1; + } + break; + }//end case P + }//end switch + }//end for + }//end if + return 0; +} + + +void pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,OPJ_UINT32 tileno, OPJ_UINT32 pino,OPJ_UINT32 tpnum, OPJ_INT32 tppos, J2K_T2_MODE t2_mode) +{ + const OPJ_CHAR *prog; + OPJ_INT32 i,l; + OPJ_UINT32 incr_top=1,resetX=0; opj_tcp_t *tcps =&cp->tcps[tileno]; opj_poc_t *tcp= &tcps->pocs[pino]; + + prog = j2k_convert_progression_order(tcp->prg); pi[pino].first = 1; - pi[pino].poc.prg = tcp->prg; - - switch(tcp->prg){ - case CPRL: strncpy(prog, "CPRL",4); - break; - case LRCP: strncpy(prog, "LRCP",4); - break; - case PCRL: strncpy(prog, "PCRL",4); - break; - case RLCP: strncpy(prog, "RLCP",4); - break; - case RPCL: strncpy(prog, "RPCL",4); - break; - case PROG_UNKNOWN: - return true; - } - - if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){ + pi[pino].poc.prg = tcp->prg; + + if(!(cp->m_specific_param.m_enc.m_tp_on&& ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){ pi[pino].poc.resno0 = tcp->resS; pi[pino].poc.resno1 = tcp->resE; pi[pino].poc.compno0 = tcp->compS; @@ -765,147 +1765,69 @@ bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino, pi[pino].poc.tx1 = tcp->txE; pi[pino].poc.ty1 = tcp->tyE; }else { - if( tpnum < cur_totnum_tp){ - for(i=3;i>=0;i--){ + for(i=tppos+1;i<4;i++){ + switch(prog[i]){ + case 'R': + pi[pino].poc.resno0 = tcp->resS; + pi[pino].poc.resno1 = tcp->resE; + break; + case 'C': + pi[pino].poc.compno0 = tcp->compS; + pi[pino].poc.compno1 = tcp->compE; + break; + case 'L': + pi[pino].poc.layno0 = tcp->layS; + pi[pino].poc.layno1 = tcp->layE; + break; + case 'P': + switch(tcp->prg){ + case LRCP: + case RLCP: + pi[pino].poc.precno0 = tcp->prcS; + pi[pino].poc.precno1 = tcp->prcE; + break; + default: + pi[pino].poc.tx0 = tcp->txS; + pi[pino].poc.ty0 = tcp->tyS; + pi[pino].poc.tx1 = tcp->txE; + pi[pino].poc.ty1 = tcp->tyE; + break; + } + break; + } + } + + if(tpnum==0){ + for(i=tppos;i>=0;i--){ switch(prog[i]){ - case 'C': - if (i > tppos){ - pi[pino].poc.compno0 = tcp->compS; - pi[pino].poc.compno1 = tcp->compE; - }else{ - if (tpnum == 0){ + case 'C': tcp->comp_t = tcp->compS; pi[pino].poc.compno0 = tcp->comp_t; pi[pino].poc.compno1 = tcp->comp_t+1; tcp->comp_t+=1; - }else{ - if (incr_top == 1){ - if(tcp->comp_t ==tcp->compE){ - tcp->comp_t = tcp->compS; - pi[pino].poc.compno0 = tcp->comp_t; - pi[pino].poc.compno1 = tcp->comp_t+1; - tcp->comp_t+=1; - incr_top=1; - }else{ - pi[pino].poc.compno0 = tcp->comp_t; - pi[pino].poc.compno1 = tcp->comp_t+1; - tcp->comp_t+=1; - incr_top=0; - } - }else{ - pi[pino].poc.compno0 = tcp->comp_t-1; - pi[pino].poc.compno1 = tcp->comp_t; - } - } - } - break; - - case 'R': - if (i > tppos){ - pi[pino].poc.resno0 = tcp->resS; - pi[pino].poc.resno1 = tcp->resE; - }else{ - if (tpnum == 0){ + break; + case 'R': tcp->res_t = tcp->resS; pi[pino].poc.resno0 = tcp->res_t; pi[pino].poc.resno1 = tcp->res_t+1; tcp->res_t+=1; - }else{ - if (incr_top == 1){ - if(tcp->res_t==tcp->resE){ - tcp->res_t = tcp->resS; - pi[pino].poc.resno0 = tcp->res_t; - pi[pino].poc.resno1 = tcp->res_t+1; - tcp->res_t+=1; - incr_top=1; - }else{ - pi[pino].poc.resno0 = tcp->res_t; - pi[pino].poc.resno1 = tcp->res_t+1; - tcp->res_t+=1; - incr_top=0; - } - }else{ - pi[pino].poc.resno0 = tcp->res_t - 1; - pi[pino].poc.resno1 = tcp->res_t; - } - } - } - break; - - case 'L': - if (i > tppos){ - pi[pino].poc.layno0 = tcp->layS; - pi[pino].poc.layno1 = tcp->layE; - }else{ - if (tpnum == 0){ + break; + case 'L': tcp->lay_t = tcp->layS; pi[pino].poc.layno0 = tcp->lay_t; pi[pino].poc.layno1 = tcp->lay_t+1; tcp->lay_t+=1; - }else{ - if (incr_top == 1){ - if(tcp->lay_t == tcp->layE){ - tcp->lay_t = tcp->layS; - pi[pino].poc.layno0 = tcp->lay_t; - pi[pino].poc.layno1 = tcp->lay_t+1; - tcp->lay_t+=1; - incr_top=1; - }else{ - pi[pino].poc.layno0 = tcp->lay_t; - pi[pino].poc.layno1 = tcp->lay_t+1; - tcp->lay_t+=1; - incr_top=0; - } - }else{ - pi[pino].poc.layno0 = tcp->lay_t - 1; - pi[pino].poc.layno1 = tcp->lay_t; - } - } - } - break; - - case 'P': - switch(tcp->prg){ - case LRCP: - case RLCP: - if (i > tppos){ - pi[pino].poc.precno0 = tcp->prcS; - pi[pino].poc.precno1 = tcp->prcE; - }else{ - if (tpnum == 0){ + break; + case 'P': + switch(tcp->prg){ + case LRCP: + case RLCP: tcp->prc_t = tcp->prcS; pi[pino].poc.precno0 = tcp->prc_t; pi[pino].poc.precno1 = tcp->prc_t+1; tcp->prc_t+=1; - }else{ - if (incr_top == 1){ - if(tcp->prc_t == tcp->prcE){ - tcp->prc_t = tcp->prcS; - pi[pino].poc.precno0 = tcp->prc_t; - pi[pino].poc.precno1 = tcp->prc_t+1; - tcp->prc_t+=1; - incr_top=1; - }else{ - pi[pino].poc.precno0 = tcp->prc_t; - pi[pino].poc.precno1 = tcp->prc_t+1; - tcp->prc_t+=1; - incr_top=0; - } - }else{ - pi[pino].poc.precno0 = tcp->prc_t - 1; - pi[pino].poc.precno1 = tcp->prc_t; - } - } - } - break; - default: - if (i > tppos){ - pi[pino].poc.tx0 = tcp->txS; - pi[pino].poc.ty0 = tcp->tyS; - pi[pino].poc.tx1 = tcp->txE; - pi[pino].poc.ty1 = tcp->tyE; - }else{ - if (tpnum == 0){ + break; + default: tcp->tx0_t = tcp->txS; tcp->ty0_t = tcp->tyS; pi[pino].poc.tx0 = tcp->tx0_t; @@ -914,15 +1836,137 @@ bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino, pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); tcp->tx0_t = pi[pino].poc.tx1; tcp->ty0_t = pi[pino].poc.ty1; + break; + } + break; + } + } + incr_top=1; + }else{ + for(i=tppos;i>=0;i--){ + switch(prog[i]){ + case 'C': + pi[pino].poc.compno0 = tcp->comp_t-1; + pi[pino].poc.compno1 = tcp->comp_t; + break; + case 'R': + pi[pino].poc.resno0 = tcp->res_t-1; + pi[pino].poc.resno1 = tcp->res_t; + break; + case 'L': + pi[pino].poc.layno0 = tcp->lay_t-1; + pi[pino].poc.layno1 = tcp->lay_t; + break; + case 'P': + switch(tcp->prg){ + case LRCP: + case RLCP: + pi[pino].poc.precno0 = tcp->prc_t-1; + pi[pino].poc.precno1 = tcp->prc_t; + break; + default: + pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx); + pi[pino].poc.tx1 = tcp->tx0_t ; + pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); + pi[pino].poc.ty1 = tcp->ty0_t ; + break; + } + break; + } + if(incr_top==1){ + switch(prog[i]){ + case 'R': + if(tcp->res_t==tcp->resE){ + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + tcp->res_t = tcp->resS; + pi[pino].poc.resno0 = tcp->res_t; + pi[pino].poc.resno1 = tcp->res_t+1; + tcp->res_t+=1; + incr_top=1; + }else{ + incr_top=0; + } }else{ - if (incr_top == 1){ + pi[pino].poc.resno0 = tcp->res_t; + pi[pino].poc.resno1 = tcp->res_t+1; + tcp->res_t+=1; + incr_top=0; + } + break; + case 'C': + if(tcp->comp_t ==tcp->compE){ + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + tcp->comp_t = tcp->compS; + pi[pino].poc.compno0 = tcp->comp_t; + pi[pino].poc.compno1 = tcp->comp_t+1; + tcp->comp_t+=1; + incr_top=1; + }else{ + incr_top=0; + } + }else{ + pi[pino].poc.compno0 = tcp->comp_t; + pi[pino].poc.compno1 = tcp->comp_t+1; + tcp->comp_t+=1; + incr_top=0; + } + break; + case 'L': + if(tcp->lay_t == tcp->layE){ + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + tcp->lay_t = tcp->layS; + pi[pino].poc.layno0 = tcp->lay_t; + pi[pino].poc.layno1 = tcp->lay_t+1; + tcp->lay_t+=1; + incr_top=1; + }else{ + incr_top=0; + } + }else{ + pi[pino].poc.layno0 = tcp->lay_t; + pi[pino].poc.layno1 = tcp->lay_t+1; + tcp->lay_t+=1; + incr_top=0; + } + break; + case 'P': + switch(tcp->prg){ + case LRCP: + case RLCP: + if(tcp->prc_t == tcp->prcE){ + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + tcp->prc_t = tcp->prcS; + pi[pino].poc.precno0 = tcp->prc_t; + pi[pino].poc.precno1 = tcp->prc_t+1; + tcp->prc_t+=1; + incr_top=1; + }else{ + incr_top=0; + } + }else{ + pi[pino].poc.precno0 = tcp->prc_t; + pi[pino].poc.precno1 = tcp->prc_t+1; + tcp->prc_t+=1; + incr_top=0; + } + break; + default: if(tcp->tx0_t >= tcp->txE){ if(tcp->ty0_t >= tcp->tyE){ - tcp->ty0_t = tcp->tyS; - pi[pino].poc.ty0 = tcp->ty0_t; - pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); - tcp->ty0_t = pi[pino].poc.ty1; - incr_top=1;resetX=1; + l=pi_check_next_level(i-1,cp,tileno,pino,prog); + if(l==1){ + tcp->ty0_t = tcp->tyS; + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); + tcp->ty0_t = pi[pino].poc.ty1; + incr_top=1;resetX=1; + }else{ + incr_top=0;resetX=0; + } }else{ pi[pino].poc.ty0 = tcp->ty0_t; pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); @@ -939,25 +1983,17 @@ bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino, pi[pino].poc.tx0 = tcp->tx0_t; pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx); tcp->tx0_t = pi[pino].poc.tx1; - pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); - pi[pino].poc.ty1 = tcp->ty0_t ; incr_top=0; } - }else{ - pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx); - pi[pino].poc.tx1 = tcp->tx0_t ; - pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); - pi[pino].poc.ty1 = tcp->ty0_t ; - } + break; } - } - break; - } - break; - } - } + break; + } + } + } } - } - return false; + } } + + diff --git a/libopenjpeg/pi.h b/libopenjpeg/pi.h index b5e0f6a4..a8abbf74 100644 --- a/libopenjpeg/pi.h +++ b/libopenjpeg/pi.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,25 +40,29 @@ The functions in PI.C have for goal to realize a packet iterator that permits to packet following the progression order and change of it. The functions in PI.C are used by some function in T2.C. */ - +#include "openjpeg.h" +#include "t2.h" /** @defgroup PI PI - Implementation of a packet iterator */ /*@{*/ +struct opj_poc; +struct opj_image; +struct opj_cp; /** FIXME: documentation */ typedef struct opj_pi_resolution { - int pdx, pdy; - int pw, ph; + OPJ_UINT32 pdx, pdy; + OPJ_UINT32 pw, ph; } opj_pi_resolution_t; /** FIXME: documentation */ typedef struct opj_pi_comp { - int dx, dy; + OPJ_UINT32 dx, dy; /** number of resolution levels */ - int numresolutions; + OPJ_UINT32 numresolutions; opj_pi_resolution_t *resolutions; } opj_pi_comp_t; @@ -66,50 +71,67 @@ Packet iterator */ typedef struct opj_pi_iterator { /** Enabling Tile part generation*/ - char tp_on; + OPJ_BYTE tp_on; /** precise if the packet has been already used (usefull for progression order change) */ - short int *include; + OPJ_INT16 *include; /** layer step used to localize the packet in the include vector */ - int step_l; + OPJ_UINT32 step_l; /** resolution step used to localize the packet in the include vector */ - int step_r; + OPJ_UINT32 step_r; /** component step used to localize the packet in the include vector */ - int step_c; + OPJ_UINT32 step_c; /** precinct step used to localize the packet in the include vector */ - int step_p; + OPJ_UINT32 step_p; /** component that identify the packet */ - int compno; + OPJ_UINT32 compno; /** resolution that identify the packet */ - int resno; + OPJ_UINT32 resno; /** precinct that identify the packet */ - int precno; + OPJ_UINT32 precno; /** layer that identify the packet */ - int layno; - /** 0 if the first packet */ - int first; + OPJ_UINT32 layno; /** progression order change information */ - opj_poc_t poc; + struct opj_poc poc; /** number of components in the image */ - int numcomps; + OPJ_UINT32 numcomps; /** Components*/ opj_pi_comp_t *comps; - int tx0, ty0, tx1, ty1; - int x, y, dx, dy; + OPJ_INT32 tx0, ty0, tx1, ty1; + OPJ_INT32 x, y; + OPJ_UINT32 dx, dy; + /** 0 if the first packet */ + OPJ_UINT32 first : 1; } opj_pi_iterator_t; /** @name Exported functions */ /*@{*/ /* ----------------------------------------------------------------------- */ /** -Create a packet iterator for Encoder -@param image Raw image for which the packets will be listed -@param cp Coding parameters -@param tileno Number that identifies the tile for which to list the packets -@param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass -@return Returns a packet iterator that points to the first packet of the tile -@see pi_destroy + * Creates a packet iterator for encoding. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param p_tile_no index of the tile being encoded. + * @param p_t2_mode the type of pass for generating the packet iterator + * @return a list of packet iterator that points to the first packet of the tile (not true). */ -opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno,J2K_T2_MODE t2_mode); +opj_pi_iterator_t *pi_initialise_encode(const struct opj_image *image,struct opj_cp *cp, OPJ_UINT32 tileno,J2K_T2_MODE t2_mode); + +/** + * Updates the encoding parameters of the codec. + * + * @param p_image the image being encoded. + * @param p_cp the coding parameters. + * @param p_tile_no index of the tile being encoded. +*/ +void pi_update_encoding_parameters( + const struct opj_image *p_image, + struct opj_cp *p_cp, + OPJ_UINT32 p_tile_no + ); + + + /** Modify the packet iterator for enabling tile part generation @param pi Handle to the packet iterator generated in pi_initialise_encode @@ -117,10 +139,10 @@ Modify the packet iterator for enabling tile part generation @param tileno Number that identifies the tile for which to list the packets @param tpnum Tile part number of the current tile @param tppos The position of the tile part flag in the progression order -@param cur_totnum_tp The total number of tile parts in the current tile -@return Returns true if an error is detected */ -bool pi_create_encode(opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp); +void pi_create_encode( opj_pi_iterator_t *pi, struct opj_cp *cp,OPJ_UINT32 tileno, OPJ_UINT32 pino,OPJ_UINT32 tpnum, OPJ_INT32 tppos, J2K_T2_MODE t2_mode); + + /** Create a packet iterator for Decoder @param image Raw image for which the packets will be listed @@ -129,16 +151,19 @@ Create a packet iterator for Decoder @return Returns a packet iterator that points to the first packet of the tile @see pi_destroy */ -opj_pi_iterator_t *pi_create_decode(opj_image_t * image, opj_cp_t * cp, int tileno); +opj_pi_iterator_t *pi_create_decode(struct opj_image * image, struct opj_cp * cp, OPJ_UINT32 tileno); + + /** -Destroy a packet iterator -@param pi Previously created packet iterator -@param cp Coding parameters -@param tileno Number that identifies the tile for which the packets were listed -@see pi_create -*/ -void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno); + * Destroys a packet iterator array. + * + * @param p_pi the packet iterator array to destroy. + * @param p_nb_elements the number of elements in the array. + */ +void pi_destroy( + opj_pi_iterator_t *p_pi, + OPJ_UINT32 p_nb_elements); /** Modify the packet iterator to point to the next packet @@ -146,6 +171,8 @@ Modify the packet iterator to point to the next packet @return Returns false if pi pointed to the last packet or else returns true */ bool pi_next(opj_pi_iterator_t * pi); + + /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/profile.c b/libopenjpeg/profile.c new file mode 100644 index 00000000..d153b278 --- /dev/null +++ b/libopenjpeg/profile.c @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * Adapted from Herb Marselas +"Profiling, Data Analysis, Scalability, and Magic Numbers: Meeting the Minimum System Requirements for AGE OF EMPIRES 2: THE AGE OF KINGS" +Game Developer magazine +June, 2000 issue. +*/ + +#include "profile.h" +#include +#include +#include +//============================================================================== +static OPJ_PROFILE_LIST group_list [PGROUP_LASTGROUP]; + +//============================================================================== +static void GetTimeStamp(OPJ_UINT32 *pdwtime); + +//============================================================================== +#define SetMajorSection(entry, major) \ + { group_list[ entry ].section = entry ; \ + group_list[ entry ].sectionName = #major ; } + +//============================================================================== +void _ProfInit(void) +{ + // clear everything out + memset(group_list, 0, sizeof(group_list)); + + // set groups and parents for timing + SetMajorSection(PGROUP_DWT,PGROUP_DWT); + SetMajorSection(PGROUP_T1, PGROUP_T1); + SetMajorSection(PGROUP_T2, PGROUP_T2); +} // ProfInit + +//============================================================================== +void _ProfStart (OPJ_PROFILE_GROUP group) +{ + // make sure this hasn't been incorrectly started twice + if (group_list[group].start) + { + return; + } + + // get the start time + GetTimeStamp(&(group_list[group].start)); + +} // _ProfStart + +//============================================================================== +void _ProfStop(OPJ_PROFILE_GROUP group) +{ + // make sure we called start first + if (!group_list[group].start) + { + return; + } + + // get ending time + GetTimeStamp(&(group_list[group].end)); + + // calculate this latest elapsed interval + group_list[group].total_time += group_list[group].end - group_list[group].start; + + // reset starting time + group_list[group].start = 0; + + // incr the number of calls made + ++group_list[group].totalCalls; + +} // _ProfStop + +//============================================================================== +#define proftracef(id,totalTime) \ + fprintf(p, #id "\t%u\t\t%6.6f\t\t%12.6f\t%2.2f%%\n", \ + group_list[ id ].totalCalls, \ + (OPJ_FLOAT64) group_list[ id ].total_time / CLOCKS_PER_SEC, \ + ((OPJ_FLOAT64) group_list[ id ].total_time / (group_list[ id ].totalCalls ? group_list[ id ].totalCalls : 1)), \ + ((OPJ_FLOAT64) group_list[ id ].total_time / totalTime * 100)) + +#define proftracep(id,totalTime) \ + printf(#id "\t%u\t\t%6.6f\t\t%12.6f\t%2.2f%%\n", \ + group_list[ id ].totalCalls, \ + (OPJ_FLOAT64) group_list[ id ].total_time / CLOCKS_PER_SEC, \ + ((OPJ_FLOAT64) group_list[ id ].total_time / (group_list[ id ].totalCalls ? group_list[ id ].totalCalls : 1)), \ + ((OPJ_FLOAT64) group_list[ id ].total_time / totalTime * 100)) + +//============================================================================== +void _ProfSave(const OPJ_CHAR * pFileName) +{ + FILE *p = fopen(pFileName, "wt"); + OPJ_FLOAT64 totalTime = 0.; + OPJ_UINT32 i; + + if (!p) + { + return; + } + + for + (i=0;i + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * Adapted from Herb Marselas +"Profiling, Data Analysis, Scalability, and Magic Numbers: Meeting the Minimum System Requirements for AGE OF EMPIRES 2: THE AGE OF KINGS" +Game Developer magazine +June, 2000 issue. +*/ + + +#ifndef __PROFILE_H +#define __PROFILE_H + +#include "openjpeg.h" +//============================================================================== +typedef enum +{ + PGROUP_RATE, + PGROUP_DC_SHIFT, + PGROUP_MCT, + PGROUP_DWT, + PGROUP_T1, + PGROUP_T2, + PGROUP_LASTGROUP +} OPJ_PROFILE_GROUP; + +//============================================================================== +typedef struct PROFILELIST +{ + OPJ_UINT32 start; + OPJ_UINT32 end; + OPJ_UINT32 total_time; + OPJ_UINT32 totalCalls; + OPJ_PROFILE_GROUP section; + const OPJ_CHAR *sectionName; // string name of the profile group +} OPJ_PROFILE_LIST; + +//============================================================================== +void _ProfStart(OPJ_PROFILE_GROUP group); +void _ProfStop (OPJ_PROFILE_GROUP group); + +//============================================================================== +//============================================================================== +#ifdef _PROFILE +#define PROFINIT() _ProfInit(); +#define PROFSTART (group) _ProfStart (group); +#define PROFSTOP (group) _ProfStop (group); +#define PROFSAVE(file) _ProfSave(file); +#define PROFPRINT() _ProfPrint(); +#else +#define PROFINIT() +#define PROFSTART(group) +#define PROFSTOP (group) +#define PROFSAVE(file) +#define PROFPRINT() +#endif // !_PROFILE + +//============================================================================== +#endif // __PROFILE_H diff --git a/libopenjpeg/raw.c b/libopenjpeg/raw.c index 3d231bfd..d42528d2 100644 --- a/libopenjpeg/raw.c +++ b/libopenjpeg/raw.c @@ -3,6 +3,7 @@ * Copyright (c) 2002-2007, Professor Benoit Macq * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,7 +28,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" +#include "raw.h" +#include "opj_malloc.h" /* ========================================================== @@ -53,11 +55,11 @@ void raw_destroy(opj_raw_t *raw) { } } -int raw_numbytes(opj_raw_t *raw) { +OPJ_UINT32 raw_numbytes(opj_raw_t *raw) { return raw->bp - raw->start; } -void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len) { +void raw_init_dec(opj_raw_t *raw, OPJ_BYTE *bp, OPJ_UINT32 len) { raw->start = bp; raw->lenmax = len; raw->len = 0; @@ -65,8 +67,8 @@ void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len) { raw->ct = 0; } -int raw_decode(opj_raw_t *raw) { - int d; +OPJ_UINT32 raw_decode(opj_raw_t *raw) { + OPJ_UINT32 d; if (raw->ct == 0) { raw->ct = 8; if (raw->len == raw->lenmax) { diff --git a/libopenjpeg/raw.h b/libopenjpeg/raw.h index 3c4b372f..5d23f7a1 100644 --- a/libopenjpeg/raw.h +++ b/libopenjpeg/raw.h @@ -3,6 +3,7 @@ * Copyright (c) 2002-2007, Professor Benoit Macq * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,7 +37,7 @@ The functions in RAW.C have for goal to realize the operation of raw encoding linked with the corresponding mode switch. */ - +#include "openjpeg.h" /** @defgroup RAW RAW - Implementation of operations for raw encoding */ /*@{*/ @@ -45,17 +46,17 @@ RAW encoding operations */ typedef struct opj_raw { /** temporary buffer where bits are coded or decoded */ - unsigned char c; + OPJ_BYTE c; /** number of bits already read or free to write */ - unsigned int ct; + OPJ_UINT32 ct; /** maximum length to decode */ - unsigned int lenmax; + OPJ_UINT32 lenmax; /** length decoded */ - unsigned int len; + OPJ_UINT32 len; /** pointer to the current position in the buffer */ - unsigned char *bp; + OPJ_BYTE *bp; /** pointer to the start of the buffer */ - unsigned char *start; + OPJ_BYTE *start; /** pointer to the end of the buffer */ unsigned char *end; } opj_raw_t; @@ -78,20 +79,20 @@ Return the number of bytes written/read since initialisation @param raw RAW handle to destroy @return Returns the number of bytes already encoded */ -int raw_numbytes(opj_raw_t *raw); +OPJ_UINT32 raw_numbytes(opj_raw_t *raw); /** Initialize the decoder @param raw RAW handle @param bp Pointer to the start of the buffer from which the bytes will be read @param len Length of the input buffer */ -void raw_init_dec(opj_raw_t *raw, unsigned char *bp, int len); +void raw_init_dec(opj_raw_t *raw, OPJ_BYTE *bp, OPJ_UINT32 len); /** Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN @param raw RAW handle @return Returns the decoded symbol (0 or 1) */ -int raw_decode(opj_raw_t *raw); +OPJ_UINT32 raw_decode(opj_raw_t *raw); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/t1.c b/libopenjpeg/t1.c index ad1c6a83..139ff48e 100644 --- a/libopenjpeg/t1.c +++ b/libopenjpeg/t1.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2007, Callum Lerwick + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,155 +31,165 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" +#include "t1.h" #include "t1_luts.h" - +#include "opj_includes.h" +#include "j2k.h" +#include "tcd.h" +#include "mqc.h" +#include "raw.h" +#include "opj_malloc.h" +#include "int.h" +#include "dwt.h" +#include "fix.h" /** @defgroup T1 T1 - Implementation of the tier-1 coding */ /*@{*/ /** @name Local static functions */ /*@{*/ -static INLINE char t1_getctxno_zc(int f, int orient); -static char t1_getctxno_sc(int f); -static INLINE int t1_getctxno_mag(int f); -static char t1_getspb(int f); -static short t1_getnmsedec_sig(int x, int bitpos); -static short t1_getnmsedec_ref(int x, int bitpos); -static void t1_updateflags(flag_t *flagsp, int s, int stride); +static INLINE OPJ_BYTE t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient); +static OPJ_BYTE t1_getctxno_sc(OPJ_UINT32 f); +static INLINE OPJ_UINT32 t1_getctxno_mag(OPJ_UINT32 f); +static OPJ_BYTE t1_getspb(OPJ_UINT32 f); +static OPJ_INT16 t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos); +static OPJ_INT16 t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos); +static void t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride); /** Encode significant pass */ static void t1_enc_sigpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int bpno, - int one, - int *nmsedec, - char type, - int vsc); + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_INT32 one, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 vsc); /** Decode significant pass */ static void t1_dec_sigpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int oneplushalf, - char type, - int vsc); + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 oneplushalf, + OPJ_BYTE type, + OPJ_UINT32 vsc); /** Encode significant pass */ static void t1_enc_sigpass( opj_t1_t *t1, - int bpno, - int orient, - int *nmsedec, - char type, - int cblksty); + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 cblksty); /** Decode significant pass */ static void t1_dec_sigpass( opj_t1_t *t1, - int bpno, - int orient, - char type, - int cblksty); + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_BYTE type, + OPJ_UINT32 cblksty); /** Encode refinement pass */ static void t1_enc_refpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int bpno, - int one, - int *nmsedec, - char type, - int vsc); + OPJ_INT32 *datap, + OPJ_INT32 bpno, + OPJ_INT32 one, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 vsc); /** Decode refinement pass */ static void t1_dec_refpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int poshalf, - int neghalf, - char type, - int vsc); + OPJ_INT32 *datap, + OPJ_INT32 poshalf, + OPJ_INT32 neghalf, + OPJ_BYTE type, + OPJ_UINT32 vsc); /** Encode refinement pass */ static void t1_enc_refpass( opj_t1_t *t1, - int bpno, - int *nmsedec, - char type, - int cblksty); + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 cblksty); /** Decode refinement pass */ static void t1_dec_refpass( opj_t1_t *t1, - int bpno, - char type, - int cblksty); + OPJ_INT32 bpno, + OPJ_BYTE type, + OPJ_UINT32 cblksty); /** Encode clean-up pass */ static void t1_enc_clnpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int bpno, - int one, - int *nmsedec, - int partial, - int vsc); + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_INT32 one, + OPJ_INT32 *nmsedec, + OPJ_UINT32 partial, + OPJ_UINT32 vsc); /** Decode clean-up pass */ static void t1_dec_clnpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int oneplushalf, - int partial, - int vsc); + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 oneplushalf, + OPJ_UINT32 partial, + OPJ_UINT32 vsc); /** Encode clean-up pass */ static void t1_enc_clnpass( opj_t1_t *t1, - int bpno, - int orient, - int *nmsedec, - int cblksty); + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_INT32 *nmsedec, + OPJ_UINT32 cblksty); /** Decode clean-up pass */ static void t1_dec_clnpass( opj_t1_t *t1, - int bpno, - int orient, - int cblksty); -static double t1_getwmsedec( - int nmsedec, - int compno, - int level, - int orient, - int bpno, - int qmfbid, - double stepsize, - int numcomps); + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_UINT32 cblksty); + +static OPJ_FLOAT64 t1_getwmsedec( + OPJ_INT32 nmsedec, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 numcomps, + const OPJ_FLOAT64 * mct_norms); /** Encode 1 code-block @param t1 T1 handle @@ -195,14 +206,15 @@ Encode 1 code-block static void t1_encode_cblk( opj_t1_t *t1, opj_tcd_cblk_enc_t* cblk, - int orient, - int compno, - int level, - int qmfbid, - double stepsize, - int cblksty, - int numcomps, - opj_tcd_tile_t * tile); + OPJ_UINT32 orient, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 cblksty, + OPJ_UINT32 numcomps, + opj_tcd_tile_t * tile, + const OPJ_FLOAT64 * mct_norms); /** Decode 1 code-block @param t1 T1 handle @@ -214,9 +226,9 @@ Decode 1 code-block static void t1_decode_cblk( opj_t1_t *t1, opj_tcd_cblk_dec_t* cblk, - int orient, - int roishift, - int cblksty); + OPJ_UINT32 orient, + OPJ_UINT32 roishift, + OPJ_UINT32 cblksty); /*@}*/ @@ -224,25 +236,26 @@ static void t1_decode_cblk( /* ----------------------------------------------------------------------- */ -static char t1_getctxno_zc(int f, int orient) { +static OPJ_BYTE t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) { return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)]; } -static char t1_getctxno_sc(int f) { +static OPJ_BYTE t1_getctxno_sc(OPJ_UINT32 f) { return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; } -static int t1_getctxno_mag(int f) { - int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG; - int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1; +static OPJ_UINT32 t1_getctxno_mag(OPJ_UINT32 f) { + OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG; + OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1; return (tmp2); } -static char t1_getspb(int f) { +static OPJ_BYTE t1_getspb(OPJ_UINT32 f) { return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4]; } -static short t1_getnmsedec_sig(int x, int bitpos) { +static OPJ_INT16 t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) +{ if (bitpos > T1_NMSEDEC_FRACBITS) { return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; } @@ -250,7 +263,7 @@ static short t1_getnmsedec_sig(int x, int bitpos) { return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; } -static short t1_getnmsedec_ref(int x, int bitpos) { +static OPJ_INT16 t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) { if (bitpos > T1_NMSEDEC_FRACBITS) { return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)]; } @@ -258,7 +271,7 @@ static short t1_getnmsedec_ref(int x, int bitpos) { return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)]; } -static void t1_updateflags(flag_t *flagsp, int s, int stride) { +static void t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) { flag_t *np = flagsp - stride; flag_t *sp = flagsp + stride; @@ -285,15 +298,16 @@ static void t1_updateflags(flag_t *flagsp, int s, int stride) { static void t1_enc_sigpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int bpno, - int one, - int *nmsedec, - char type, - int vsc) + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_INT32 one, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 vsc) { - int v, flag; + OPJ_INT32 v; + OPJ_UINT32 flag; opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -324,13 +338,13 @@ static void t1_enc_sigpass_step( static void t1_dec_sigpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int oneplushalf, - char type, - int vsc) + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 oneplushalf, + OPJ_BYTE type, + OPJ_UINT32 vsc) { - int v, flag; + OPJ_UINT32 v, flag; opj_raw_t *raw = t1->raw; /* RAW component */ opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -358,13 +372,15 @@ static void t1_dec_sigpass_step( static void t1_enc_sigpass( opj_t1_t *t1, - int bpno, - int orient, - int *nmsedec, - char type, - int cblksty) + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 cblksty) { - int i, j, k, one, vsc; + OPJ_UINT32 i, j, k, vsc; + OPJ_INT32 one; + *nmsedec = 0; one = 1 << (bpno + T1_NMSEDEC_FRACBITS); for (k = 0; k < t1->h; k += 4) { @@ -388,12 +404,13 @@ static void t1_enc_sigpass( static void t1_dec_sigpass( opj_t1_t *t1, - int bpno, - int orient, - char type, - int cblksty) + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_BYTE type, + OPJ_UINT32 cblksty) { - int i, j, k, one, half, oneplushalf, vsc; + OPJ_UINT32 i, j, k, vsc; + OPJ_INT32 one, half, oneplushalf; one = 1 << bpno; half = one >> 1; oneplushalf = one | half; @@ -417,14 +434,15 @@ static void t1_dec_sigpass( static void t1_enc_refpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int bpno, - int one, - int *nmsedec, - char type, - int vsc) + OPJ_INT32 *datap, + OPJ_INT32 bpno, + OPJ_INT32 one, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 vsc) { - int v, flag; + OPJ_INT32 v; + OPJ_UINT32 flag; opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -445,13 +463,14 @@ static void t1_enc_refpass_step( static void t1_dec_refpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int poshalf, - int neghalf, - char type, - int vsc) + OPJ_INT32 *datap, + OPJ_INT32 poshalf, + OPJ_INT32 neghalf, + OPJ_BYTE type, + OPJ_UINT32 vsc) { - int v, t, flag; + OPJ_INT32 t; + OPJ_UINT32 v,flag; opj_mqc_t *mqc = t1->mqc; /* MQC component */ opj_raw_t *raw = t1->raw; /* RAW component */ @@ -472,12 +491,14 @@ static void t1_dec_refpass_step( static void t1_enc_refpass( opj_t1_t *t1, - int bpno, - int *nmsedec, - char type, - int cblksty) + OPJ_INT32 bpno, + OPJ_INT32 *nmsedec, + OPJ_BYTE type, + OPJ_UINT32 cblksty) { - int i, j, k, one, vsc; + OPJ_UINT32 i, j, k, vsc; + OPJ_INT32 one; + *nmsedec = 0; one = 1 << (bpno + T1_NMSEDEC_FRACBITS); for (k = 0; k < t1->h; k += 4) { @@ -500,12 +521,13 @@ static void t1_enc_refpass( static void t1_dec_refpass( opj_t1_t *t1, - int bpno, - char type, - int cblksty) + OPJ_INT32 bpno, + OPJ_BYTE type, + OPJ_UINT32 cblksty) { - int i, j, k, one, poshalf, neghalf; - int vsc; + OPJ_UINT32 i, j, k; + OPJ_INT32 one, poshalf, neghalf; + OPJ_UINT32 vsc; one = 1 << bpno; poshalf = one >> 1; neghalf = bpno > 0 ? -poshalf : -1; @@ -529,15 +551,16 @@ static void t1_dec_refpass( static void t1_enc_clnpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int bpno, - int one, - int *nmsedec, - int partial, - int vsc) + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_INT32 one, + OPJ_INT32 *nmsedec, + OPJ_UINT32 partial, + OPJ_UINT32 vsc) { - int v, flag; + OPJ_INT32 v; + OPJ_UINT32 flag; opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -564,13 +587,14 @@ LABEL_PARTIAL: static void t1_dec_clnpass_step( opj_t1_t *t1, flag_t *flagsp, - int *datap, - int orient, - int oneplushalf, - int partial, - int vsc) + OPJ_INT32 *datap, + OPJ_UINT32 orient, + OPJ_INT32 oneplushalf, + OPJ_UINT32 partial, + OPJ_UINT32 vsc) { - int v, flag; + OPJ_INT32 v; + OPJ_UINT32 flag; opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -593,12 +617,14 @@ LABEL_PARTIAL: static void t1_enc_clnpass( opj_t1_t *t1, - int bpno, - int orient, - int *nmsedec, - int cblksty) + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_INT32 *nmsedec, + OPJ_UINT32 cblksty) { - int i, j, k, one, agg, runlen, vsc; + OPJ_UINT32 i, j, k; + OPJ_INT32 one; + OPJ_UINT32 agg, runlen, vsc; opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -657,12 +683,14 @@ static void t1_enc_clnpass( static void t1_dec_clnpass( opj_t1_t *t1, - int bpno, - int orient, - int cblksty) + OPJ_INT32 bpno, + OPJ_UINT32 orient, + OPJ_UINT32 cblksty) { - int i, j, k, one, half, oneplushalf, agg, runlen, vsc; - int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; + OPJ_UINT32 i, j, k, one; + OPJ_INT32 half, oneplushalf; + OPJ_UINT32 agg, runlen, vsc; + OPJ_UINT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; opj_mqc_t *mqc = t1->mqc; /* MQC component */ @@ -712,7 +740,7 @@ static void t1_dec_clnpass( } } if (segsym) { - int v = 0; + OPJ_UINT32 v = 0; mqc_setcurctx(mqc, T1_CTXNO_UNI); v = mqc_decode(mqc); v = (v << 1) | mqc_decode(mqc); @@ -728,47 +756,51 @@ static void t1_dec_clnpass( /** mod fixed_quality */ -static double t1_getwmsedec( - int nmsedec, - int compno, - int level, - int orient, - int bpno, - int qmfbid, - double stepsize, - int numcomps) +static OPJ_FLOAT64 t1_getwmsedec( + OPJ_INT32 nmsedec, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 orient, + OPJ_INT32 bpno, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 numcomps, + const OPJ_FLOAT64 * mct_norms) { - double w1, w2, wmsedec; - if (qmfbid == 1) { - w1 = (numcomps > 1) ? mct_getnorm(compno) : 1.0; + OPJ_FLOAT64 w1 = 1, w2, wmsedec; + if + (mct_norms) + { + w1 = mct_norms[compno]; + } + if (qmfbid == 1) + { w2 = dwt_getnorm(level, orient); } else { /* if (qmfbid == 0) */ - w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1.0; w2 = dwt_getnorm_real(level, orient); } wmsedec = w1 * w2 * stepsize * (1 << bpno); wmsedec *= wmsedec * nmsedec / 8192.0; - return wmsedec; } static bool allocate_buffers( opj_t1_t *t1, - int w, - int h) + OPJ_UINT32 w, + OPJ_UINT32 h) { - int datasize=w * h; - int flagssize; + OPJ_UINT32 datasize=w * h; + OPJ_UINT32 flagssize; if(datasize > t1->datasize){ opj_aligned_free(t1->data); - t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int)); + t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32)); if(!t1->data){ return false; } t1->datasize=datasize; } - memset(t1->data,0,datasize * sizeof(int)); + memset(t1->data,0,datasize * sizeof(OPJ_INT32)); t1->flags_stride=w+2; flagssize=t1->flags_stride * (h+2); @@ -793,28 +825,32 @@ static bool allocate_buffers( static void t1_encode_cblk( opj_t1_t *t1, opj_tcd_cblk_enc_t* cblk, - int orient, - int compno, - int level, - int qmfbid, - double stepsize, - int cblksty, - int numcomps, - opj_tcd_tile_t * tile) + OPJ_UINT32 orient, + OPJ_UINT32 compno, + OPJ_UINT32 level, + OPJ_UINT32 qmfbid, + OPJ_FLOAT64 stepsize, + OPJ_UINT32 cblksty, + OPJ_UINT32 numcomps, + opj_tcd_tile_t * tile, + const OPJ_FLOAT64 * mct_norms) { - double cumwmsedec = 0.0; + OPJ_FLOAT64 cumwmsedec = 0.0; opj_mqc_t *mqc = t1->mqc; /* MQC component */ - int passno, bpno, passtype; - int nmsedec = 0; - int i, max; - char type = T1_TYPE_MQ; - double tempwmsedec; + OPJ_UINT32 passno; + OPJ_INT32 bpno; + OPJ_UINT32 passtype; + OPJ_INT32 nmsedec = 0; + OPJ_INT32 max; + OPJ_UINT32 i; + OPJ_BYTE type = T1_TYPE_MQ; + OPJ_FLOAT64 tempwmsedec; max = 0; for (i = 0; i < t1->w * t1->h; ++i) { - int tmp = abs(t1->data[i]); + OPJ_INT32 tmp = abs(t1->data[i]); max = int_max(max, tmp); } @@ -831,8 +867,8 @@ static void t1_encode_cblk( for (passno = 0; bpno >= 0; ++passno) { opj_tcd_pass_t *pass = &cblk->passes[passno]; - int correction = 3; - type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; + OPJ_UINT32 correction = 3; + type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; switch (passtype) { case 0: @@ -850,7 +886,7 @@ static void t1_encode_cblk( } /* fixed_quality */ - tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); + tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ; cumwmsedec += tempwmsedec; tile->distotile += tempwmsedec; @@ -866,7 +902,7 @@ static void t1_encode_cblk( } pass->term = 1; } else { - if (((bpno < (cblk->numbps - 4) && (passtype > 0)) + if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0)) || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) { if (type == T1_TYPE_RAW) { mqc_flush(mqc); @@ -888,7 +924,7 @@ static void t1_encode_cblk( } if (pass->term && bpno > 0) { - type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; + type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; if (type == T1_TYPE_RAW) mqc_bypass_init_enc(mqc); else @@ -926,16 +962,17 @@ static void t1_encode_cblk( static void t1_decode_cblk( opj_t1_t *t1, opj_tcd_cblk_dec_t* cblk, - int orient, - int roishift, - int cblksty) + OPJ_UINT32 orient, + OPJ_UINT32 roishift, + OPJ_UINT32 cblksty) { opj_raw_t *raw = t1->raw; /* RAW component */ opj_mqc_t *mqc = t1->mqc; /* MQC component */ - int bpno, passtype; - int segno, passno; - char type = T1_TYPE_MQ; /* BYPASS mode */ + OPJ_INT32 bpno; + OPJ_UINT32 passtype; + OPJ_UINT32 segno, passno; + OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */ if(!allocate_buffers( t1, @@ -953,13 +990,13 @@ static void t1_decode_cblk( mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3); mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4); - for (segno = 0; segno < cblk->numsegs; ++segno) { + for (segno = 0; segno < cblk->real_num_segs; ++segno) { opj_tcd_seg_t *seg = &cblk->segs[segno]; /* BYPASS mode */ - type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; + type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ; /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */ - if(seg->data == NULL){ + if(seg->data == 00){ continue; } if (type == T1_TYPE_RAW) { @@ -968,7 +1005,7 @@ static void t1_decode_cblk( mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len); } - for (passno = 0; passno < seg->numpasses; ++passno) { + for (passno = 0; passno < seg->real_num_passes; ++passno) { switch (passtype) { case 0: t1_dec_sigpass(t1, bpno+1, orient, type, cblksty); @@ -996,49 +1033,88 @@ static void t1_decode_cblk( } /* ----------------------------------------------------------------------- */ +/** + * Creates a new Tier 1 handle + * and initializes the look-up tables of the Tier-1 coder/decoder + * @return a new T1 handle if successful, returns NULL otherwise +*/ +opj_t1_t* t1_create() +{ + opj_t1_t *l_t1 = 00; -opj_t1_t* t1_create(opj_common_ptr cinfo) { - opj_t1_t *t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t)); - if(!t1) - return NULL; - - t1->cinfo = cinfo; - /* create MQC and RAW handles */ - t1->mqc = mqc_create(); - t1->raw = raw_create(); - - t1->data=NULL; - t1->flags=NULL; - t1->datasize=0; - t1->flagssize=0; - - return t1; -} - -void t1_destroy(opj_t1_t *t1) { - if(t1) { - /* destroy MQC and RAW handles */ - mqc_destroy(t1->mqc); - raw_destroy(t1->raw); - opj_aligned_free(t1->data); - opj_aligned_free(t1->flags); - opj_free(t1); + l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t)); + if + (!l_t1) + { + return 00; } + memset(l_t1,0,sizeof(opj_t1_t)); + + /* create MQC and RAW handles */ + l_t1->mqc = mqc_create(); + if + (! l_t1->mqc) + { + t1_destroy(l_t1); + return 00; + } + l_t1->raw = raw_create(); + if + (! l_t1->raw) + { + t1_destroy(l_t1); + return 00; + } + return l_t1; } -void t1_encode_cblks( +/** + * Destroys a previously created T1 handle + * + * @param p_t1 Tier 1 handle to destroy +*/ +void t1_destroy(opj_t1_t *p_t1) +{ + if + (! p_t1) + { + return; + } + + /* destroy MQC and RAW handles */ + mqc_destroy(p_t1->mqc); + p_t1->mqc = 00; + raw_destroy(p_t1->raw); + p_t1->raw = 00; + if + (p_t1->data) + { + opj_aligned_free(p_t1->data); + p_t1->data = 00; + } + if + (p_t1->flags) + { + opj_aligned_free(p_t1->flags); + p_t1->flags = 00; + } + opj_free(p_t1); +} + +bool t1_encode_cblks( opj_t1_t *t1, opj_tcd_tile_t *tile, - opj_tcp_t *tcp) + opj_tcp_t *tcp, + const OPJ_FLOAT64 * mct_norms) { - int compno, resno, bandno, precno, cblkno; + OPJ_UINT32 compno, resno, bandno, precno, cblkno; tile->distotile = 0; /* fixed_quality */ for (compno = 0; compno < tile->numcomps; ++compno) { opj_tcd_tilecomp_t* tilec = &tile->comps[compno]; opj_tccp_t* tccp = &tcp->tccps[compno]; - int tile_w = tilec->x1 - tilec->x0; + OPJ_UINT32 tile_w = tilec->x1 - tilec->x0; for (resno = 0; resno < tilec->numresolutions; ++resno) { opj_tcd_resolution_t *res = &tilec->resolutions[resno]; @@ -1051,14 +1127,14 @@ void t1_encode_cblks( for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) { opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - int* restrict datap; - int* restrict tiledp; - int cblk_w; - int cblk_h; - int i, j; + OPJ_INT32 * restrict datap; + OPJ_INT32* restrict tiledp; + OPJ_UINT32 cblk_w; + OPJ_UINT32 cblk_h; + OPJ_UINT32 i, j; - int x = cblk->x0 - band->x0; - int y = cblk->y0 - band->y0; + OPJ_INT32 x = cblk->x0 - band->x0; + OPJ_INT32 y = cblk->y0 - band->y0; if (band->bandno & 1) { opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1]; x += pres->x1 - pres->x0; @@ -1073,7 +1149,7 @@ void t1_encode_cblks( cblk->x1 - cblk->x0, cblk->y1 - cblk->y0)) { - return; + return false; } datap=t1->data; @@ -1084,18 +1160,18 @@ void t1_encode_cblks( if (tccp->qmfbid == 1) { for (j = 0; j < cblk_h; ++j) { for (i = 0; i < cblk_w; ++i) { - int tmp = tiledp[(j * tile_w) + i]; + OPJ_INT32 tmp = tiledp[(j * tile_w) + i]; datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS; } } } else { /* if (tccp->qmfbid == 0) */ for (j = 0; j < cblk_h; ++j) { for (i = 0; i < cblk_w; ++i) { - int tmp = tiledp[(j * tile_w) + i]; + OPJ_INT32 tmp = tiledp[(j * tile_w) + i]; datap[(j * cblk_w) + i] = fix_mul( tmp, - 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS); + 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS); } } } @@ -1110,13 +1186,15 @@ void t1_encode_cblks( band->stepsize, tccp->cblksty, tile->numcomps, - tile); + tile, + mct_norms); } /* cblkno */ } /* precno */ } /* bandno */ } /* resno */ } /* compno */ + return true; } void t1_decode_cblks( @@ -1124,11 +1202,11 @@ void t1_decode_cblks( opj_tcd_tilecomp_t* tilec, opj_tccp_t* tccp) { - int resno, bandno, precno, cblkno; + OPJ_UINT32 resno, bandno, precno, cblkno; - int tile_w = tilec->x1 - tilec->x0; + OPJ_UINT32 tile_w = tilec->x1 - tilec->x0; - for (resno = 0; resno < tilec->numresolutions; ++resno) { + for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) { opj_tcd_resolution_t* res = &tilec->resolutions[resno]; for (bandno = 0; bandno < res->numbands; ++bandno) { @@ -1139,11 +1217,11 @@ void t1_decode_cblks( for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) { opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno]; - int* restrict datap; + OPJ_INT32* restrict datap; void* restrict tiledp; - int cblk_w, cblk_h; - int x, y; - int i, j; + OPJ_UINT32 cblk_w, cblk_h; + OPJ_INT32 x, y; + OPJ_UINT32 i, j; t1_decode_cblk( t1, @@ -1168,11 +1246,11 @@ void t1_decode_cblks( cblk_h = t1->h; if (tccp->roishift) { - int thresh = 1 << tccp->roishift; + OPJ_INT32 thresh = 1 << tccp->roishift; for (j = 0; j < cblk_h; ++j) { for (i = 0; i < cblk_w; ++i) { - int val = datap[(j * cblk_w) + i]; - int mag = abs(val); + OPJ_INT32 val = datap[(j * cblk_w) + i]; + OPJ_INT32 mag = abs(val); if (mag >= thresh) { mag >>= tccp->roishift; datap[(j * cblk_w) + i] = val < 0 ? -mag : mag; @@ -1185,8 +1263,8 @@ void t1_decode_cblks( if (tccp->qmfbid == 1) { for (j = 0; j < cblk_h; ++j) { for (i = 0; i < cblk_w; ++i) { - int tmp = datap[(j * cblk_w) + i]; - ((int*)tiledp)[(j * tile_w) + i] = tmp / 2; + OPJ_INT32 tmp = datap[(j * cblk_w) + i]; + ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp >> 1; } } } else { /* if (tccp->qmfbid == 0) */ @@ -1197,12 +1275,12 @@ void t1_decode_cblks( } } } - opj_free(cblk->data); - opj_free(cblk->segs); + //opj_free(cblk->segs); + //cblk->segs = 00; } /* cblkno */ - opj_free(precinct->cblks.dec); } /* precno */ } /* bandno */ } /* resno */ } + diff --git a/libopenjpeg/t1.h b/libopenjpeg/t1.h index 0b4294e1..1a4402c8 100644 --- a/libopenjpeg/t1.h +++ b/libopenjpeg/t1.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,10 +38,10 @@ The functions in T1.C have for goal to realize the tier-1 coding operation. The functions in T1.C are used by some function in TCD.C. */ - +#include "openjpeg.h" /** @defgroup T1 T1 - Implementation of the tier-1 coding */ /*@{*/ - +//#include "raw.h" /* ----------------------------------------------------------------------- */ #define T1_NMSEDEC_BITS 7 @@ -84,6 +85,14 @@ in T1.C are used by some function in TCD.C. #define T1_TYPE_RAW 1 /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/ /* ----------------------------------------------------------------------- */ +struct opj_common_struct; +struct opj_tcd_tile; +struct opj_tcp; +struct opj_tcd_tilecomp; +struct opj_mqc; +struct opj_raw; +struct opj_tccp; + typedef short flag_t; @@ -91,21 +100,18 @@ typedef short flag_t; Tier-1 coding (coding of code-block coefficients) */ typedef struct opj_t1 { - /** codec context */ - opj_common_ptr cinfo; - /** MQC component */ - opj_mqc_t *mqc; + struct opj_mqc *mqc; /** RAW component */ - opj_raw_t *raw; + struct opj_raw *raw; - int *data; + OPJ_INT32 *data; flag_t *flags; - int w; - int h; - int datasize; - int flagssize; - int flags_stride; + OPJ_UINT32 w; + OPJ_UINT32 h; + OPJ_UINT32 datasize; + OPJ_UINT32 flagssize; + OPJ_UINT32 flags_stride; } opj_t1_t; #define MACRO_t1_flags(x,y) t1->flags[((x)*(t1->flags_stride))+(y)] @@ -113,32 +119,35 @@ typedef struct opj_t1 { /** @name Exported functions */ /*@{*/ /* ----------------------------------------------------------------------- */ + /** -Create a new T1 handle -and initialize the look-up tables of the Tier-1 coder/decoder -@return Returns a new T1 handle if successful, returns NULL otherwise -@see t1_init_luts + * Creates a new Tier 1 handle + * and initializes the look-up tables of the Tier-1 coder/decoder + * @return a new T1 handle if successful, returns NULL otherwise */ -opj_t1_t* t1_create(opj_common_ptr cinfo); +opj_t1_t* t1_create(); + /** -Destroy a previously created T1 handle -@param t1 T1 handle to destroy + * Destroys a previously created T1 handle + * + * @param p_t1 Tier 1 handle to destroy */ -void t1_destroy(opj_t1_t *t1); +void t1_destroy(opj_t1_t *p_t1); + /** Encode the code-blocks of a tile @param t1 T1 handle @param tile The tile to encode @param tcp Tile coding parameters */ -void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp); +bool t1_encode_cblks(opj_t1_t *t1, struct opj_tcd_tile *tile, struct opj_tcp *tcp,const OPJ_FLOAT64 * mct_norms); /** Decode the code-blocks of a tile @param t1 T1 handle @param tile The tile to decode @param tcp Tile coding parameters */ -void t1_decode_cblks(opj_t1_t* t1, opj_tcd_tilecomp_t* tilec, opj_tccp_t* tccp); +void t1_decode_cblks(opj_t1_t* t1, struct opj_tcd_tilecomp* tilec, struct opj_tccp* tccp); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/t2.c b/libopenjpeg/t2.c index be9b42a4..18397e33 100644 --- a/libopenjpeg/t2.c +++ b/libopenjpeg/t2.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,8 +29,19 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - +#include "openjpeg.h" #include "opj_includes.h" +#include "t2.h" +#include "bio.h" +#include "tcd.h" +#include "pi.h" +#include "event.h" +#include "j2k.h" +#include "tgt.h" +#include "int.h" +#include "opj_malloc.h" +#include "pi.h" + /** @defgroup T2 T2 - Implementation of a tier-2 coding */ /*@{*/ @@ -37,15 +49,15 @@ /** @name Local static functions */ /*@{*/ -static void t2_putcommacode(opj_bio_t *bio, int n); -static int t2_getcommacode(opj_bio_t *bio); +static void t2_putcommacode(opj_bio_t *bio, OPJ_UINT32 n); +static OPJ_UINT32 t2_getcommacode(opj_bio_t *bio); /** Variable length code for signalling delta Zil (truncation point) @param bio Bit Input/Output component @param n delta Zil */ -static void t2_putnumpasses(opj_bio_t *bio, int n); -static int t2_getnumpasses(opj_bio_t *bio); +static void t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n); +static OPJ_UINT32 t2_getnumpasses(opj_bio_t *bio); /** Encode a packet of a tile to a destination buffer @param tile Tile for which to write the packets @@ -57,13 +69,21 @@ Encode a packet of a tile to a destination buffer @param tileno Number of the tile encoded @return */ -static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_codestream_info_t *cstr_info, int tileno); +static bool t2_encode_packet( + OPJ_UINT32 tileno, + opj_tcd_tile_t *tile, + opj_tcp_t *tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 len, + opj_codestream_info_t *cstr_info); /** @param seg @param cblksty @param first */ -static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first); +static bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first); /** Decode a packet of a tile from a source buffer @param t2 T2 handle @@ -74,8 +94,15 @@ Decode a packet of a tile from a source buffer @param pi Packet identity @return */ -static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, - opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info); +static bool t2_decode_packet( + opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info); /*@}*/ @@ -85,22 +112,26 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t /* #define RESTART 0x04 */ -static void t2_putcommacode(opj_bio_t *bio, int n) { - while (--n >= 0) { +static void t2_putcommacode(opj_bio_t *bio, OPJ_UINT32 n) { + while + (--n != -1) + { bio_write(bio, 1, 1); } bio_write(bio, 0, 1); } -static int t2_getcommacode(opj_bio_t *bio) { - int n; - for (n = 0; bio_read(bio, 1); n++) { - ; +static OPJ_UINT32 t2_getcommacode(opj_bio_t *bio) { + OPJ_UINT32 n = 0; + while + (bio_read(bio, 1)) + { + ++n; } return n; } -static void t2_putnumpasses(opj_bio_t *bio, int n) { +static void t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) { if (n == 1) { bio_write(bio, 0, 1); } else if (n == 2) { @@ -114,8 +145,8 @@ static void t2_putnumpasses(opj_bio_t *bio, int n) { } } -static int t2_getnumpasses(opj_bio_t *bio) { - int n; +static OPJ_UINT32 t2_getnumpasses(opj_bio_t *bio) { + OPJ_UINT32 n; if (!bio_read(bio, 1)) return 1; if (!bio_read(bio, 1)) @@ -127,19 +158,32 @@ static int t2_getnumpasses(opj_bio_t *bio) { return (37 + bio_read(bio, 7)); } -static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) { - int bandno, cblkno; - unsigned char *c = dest; - - int compno = pi->compno; /* component value */ - int resno = pi->resno; /* resolution level value */ - int precno = pi->precno; /* precinct value */ - int layno = pi->layno; /* quality layer value */ +static bool t2_encode_packet( + OPJ_UINT32 tileno, + opj_tcd_tile_t * tile, + opj_tcp_t * tcp, + opj_pi_iterator_t *pi, + OPJ_BYTE *dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 length, + opj_codestream_info_t *cstr_info) +{ + OPJ_UINT32 bandno, cblkno; + OPJ_BYTE *c = dest; + OPJ_UINT32 l_nb_bytes; + OPJ_UINT32 compno = pi->compno; /* component value */ + OPJ_UINT32 resno = pi->resno; /* resolution level value */ + OPJ_UINT32 precno = pi->precno; /* precinct value */ + OPJ_UINT32 layno = pi->layno; /* quality layer value */ + OPJ_UINT32 l_nb_blocks; + opj_tcd_band_t *band = 00; + opj_tcd_cblk_enc_t* cblk = 00; + opj_tcd_pass_t *pass = 00; opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - opj_bio_t *bio = NULL; /* BIO component */ + opj_bio_t *bio = 00; /* BIO component */ /* */ if (tcp->csty & J2K_CP_CSTY_SOP) { @@ -150,20 +194,27 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera c[4] = (tile->packno % 65536) / 256; c[5] = (tile->packno % 65536) % 256; c += 6; + length -= 6; } /* */ if (!layno) { - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; + band = res->bands; + for + (bandno = 0; bandno < res->numbands; ++bandno) + { opj_tcd_precinct_t *prc = &band->precincts[precno]; tgt_reset(prc->incltree); tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { + l_nb_blocks = prc->cw * prc->ch; + for + (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) + { opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; cblk->numpasses = 0; tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); } + ++band; } } @@ -172,22 +223,32 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera bio_write(bio, 1, 1); /* Empty header bit */ /* Writing Packet header */ - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; + band = res->bands; + for + (bandno = 0; bandno < res->numbands; ++bandno) + { opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) + { opj_tcd_layer_t *layer = &cblk->layers[layno]; - if (!cblk->numpasses && layer->numpasses) { + if + (!cblk->numpasses && layer->numpasses) + { tgt_setvalue(prc->incltree, cblkno, layno); } + ++cblk; } - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + cblk = prc->cblks.enc; + for + (cblkno = 0; cblkno < l_nb_blocks; cblkno++) + { opj_tcd_layer_t *layer = &cblk->layers[layno]; - int increment = 0; - int nump = 0; - int len = 0, passno; + OPJ_UINT32 increment = 0; + OPJ_UINT32 nump = 0; + OPJ_UINT32 len = 0, passno; + OPJ_UINT32 l_nb_passes; /* cblk inclusion bits */ if (!cblk->numpasses) { tgt_encode(bio, prc->incltree, cblkno, layno + 1); @@ -195,53 +256,73 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera bio_write(bio, layer->numpasses != 0, 1); } /* if cblk not included, go to the next cblk */ - if (!layer->numpasses) { + if + (!layer->numpasses) + { + ++cblk; continue; } /* if first instance of cblk --> zero bit-planes information */ - if (!cblk->numpasses) { + if + (!cblk->numpasses) + { cblk->numlenbits = 3; tgt_encode(bio, prc->imsbtree, cblkno, 999); } /* number of coding passes included */ t2_putnumpasses(bio, layer->numpasses); - + l_nb_passes = cblk->numpasses + layer->numpasses; + pass = cblk->passes + cblk->numpasses; /* computation of the increase of the length indicator and insertion in the header */ - for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; - nump++; + for + (passno = cblk->numpasses; passno < l_nb_passes; ++passno) + { + ++nump; len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + if + (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) + { increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump))); len = 0; nump = 0; } + ++pass; } t2_putcommacode(bio, increment); /* computation of the new Length indicator */ cblk->numlenbits += increment; + pass = cblk->passes + cblk->numpasses; /* insertion of the codeword segment length */ - for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) { - opj_tcd_pass_t *pass = &cblk->passes[passno]; + for + (passno = cblk->numpasses; passno < l_nb_passes; ++passno) + { nump++; len += pass->len; - if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) { + if + (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) + { bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump)); len = 0; nump = 0; } + ++pass; } + ++cblk; } + ++band; } - if (bio_flush(bio)) { + if + (bio_flush(bio)) + { bio_destroy(bio); - return -999; /* modified to eliminate longjmp !! */ + return false; /* modified to eliminate longjmp !! */ } - - c += bio_numbytes(bio); + l_nb_bytes = bio_numbytes(bio); + c += l_nb_bytes; + length -= l_nb_bytes; bio_destroy(bio); /* */ @@ -249,6 +330,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera c[0] = 255; c[1] = 146; c += 2; + length -= 2; } /* */ @@ -257,28 +339,37 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera // Will be updated later by incrementing with packet start value if(cstr_info && cstr_info->index_write) { opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; - info_PK->end_ph_pos = (int)(c - dest); + info_PK->end_ph_pos = (OPJ_INT32)(c - dest); } /* INDEX >> */ /* Writing the packet body */ - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; + band = res->bands; + for + (bandno = 0; bandno < res->numbands; bandno++) + { opj_tcd_precinct_t *prc = &band->precincts[precno]; - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; + l_nb_blocks = prc->cw * prc->ch; + cblk = prc->cblks.enc; + for + (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) + { opj_tcd_layer_t *layer = &cblk->layers[layno]; - if (!layer->numpasses) { + if + (!layer->numpasses) + { + ++cblk; continue; } - if (c + layer->len > dest + length) { - return -999; + if + (layer->len > length) + { + return false; } - memcpy(c, layer->data, layer->len); cblk->numpasses += layer->numpasses; c += layer->len; + length -= layer->len; /* << INDEX */ if(cstr_info && cstr_info->index_write) { opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno]; @@ -287,21 +378,34 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera cstr_info->D_max = info_PK->disto; } } + ++cblk; /* INDEX >> */ } + ++band; } - - return (c - dest); + * p_data_written += (c - dest); + return true; } -static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) { - opj_tcd_seg_t* seg; - cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t)); +static bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, OPJ_UINT32 index, OPJ_UINT32 cblksty, OPJ_UINT32 first) +{ + opj_tcd_seg_t* seg = 00; + OPJ_UINT32 l_nb_segs = index + 1; + + if + (l_nb_segs > cblk->m_current_max_segs) + { + cblk->m_current_max_segs += J2K_DEFAULT_NB_SEGS; + cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t)); + if + (! cblk->segs) + { + return false; + } + } seg = &cblk->segs[index]; - seg->data = NULL; - seg->dataindex = 0; - seg->numpasses = 0; - seg->len = 0; + memset(seg,0,sizeof(opj_tcd_seg_t)); + if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { seg->maxpasses = 1; } @@ -314,50 +418,74 @@ static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int fi } else { seg->maxpasses = 109; } + return true; } -static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, - opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) { - int bandno, cblkno; - unsigned char *c = src; - - opj_cp_t *cp = t2->cp; - - int compno = pi->compno; /* component value */ - int resno = pi->resno; /* resolution level value */ - int precno = pi->precno; /* precinct value */ - int layno = pi->layno; /* quality layer value */ - - opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno]; - - unsigned char *hd = NULL; - int present; +static bool t2_read_packet_header( + opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + bool * p_is_data_present, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info) +{ + /* loop */ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + OPJ_UINT32 l_remaining_length; + OPJ_UINT32 l_header_length; + OPJ_UINT32 * l_modified_length_ptr = 00; + OPJ_BYTE *l_current_data = p_src_data; + opj_cp_t *l_cp = p_t2->cp; + opj_bio_t *l_bio = 00; /* BIO component */ + opj_tcd_band_t *l_band = 00; + opj_tcd_cblk_dec_t* l_cblk = 00; + opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; - opj_bio_t *bio = NULL; /* BIO component */ + OPJ_BYTE *l_header_data = 00; + OPJ_BYTE **l_header_data_start = 00; - if (layno == 0) { - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; + OPJ_UINT32 l_present; + + if + (p_pi->layno == 0) + { + l_band = l_res->bands; + /* reset tagtrees */ + for + (bandno = 0; bandno < l_res->numbands; ++bandno) + { + opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; - - tgt_reset(prc->incltree); - tgt_reset(prc->imsbtree); - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; - cblk->numsegs = 0; + if ( + ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))) + { + tgt_reset(l_prc->incltree); + tgt_reset(l_prc->imsbtree); + l_cblk = l_prc->cblks.dec; + l_nb_code_blocks = l_prc->cw * l_prc->ch; + for + (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) + { + l_cblk->numsegs = 0; + l_cblk->real_num_segs = 0; + ++l_cblk; + } } + ++l_band; } } /* SOP markers */ - if (tcp->csty & J2K_CP_CSTY_SOP) { - if ((*c) != 0xff || (*(c + 1) != 0x91)) { - opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); + if (p_tcp->csty & J2K_CP_CSTY_SOP) { + if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) { + // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); } else { - c += 6; + l_current_data += 6; } /** TODO : check the Nsop value */ @@ -370,179 +498,276 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t step 2: Return to codestream for decoding */ - bio = bio_create(); - - if (cp->ppm == 1) { /* PPM */ - hd = cp->ppm_data; - bio_init_dec(bio, hd, cp->ppm_len); - } else if (tcp->ppt == 1) { /* PPT */ - hd = tcp->ppt_data; - bio_init_dec(bio, hd, tcp->ppt_len); - } else { /* Normal Case */ - hd = c; - bio_init_dec(bio, hd, src+len-hd); + l_bio = bio_create(); + if + (! l_bio) + { + return false; } - present = bio_read(bio, 1); - - if (!present) { - bio_inalign(bio); - hd += bio_numbytes(bio); - bio_destroy(bio); + if + (l_cp->ppm == 1) + { /* PPM */ + l_header_data_start = &l_cp->ppm_data; + l_header_data = *l_header_data_start; + l_modified_length_ptr = &(l_cp->ppm_len); + } + else if + (p_tcp->ppt == 1) + { /* PPT */ + l_header_data_start = &(p_tcp->ppt_data); + l_header_data = *l_header_data_start; + l_modified_length_ptr = &(p_tcp->ppt_len); + } + else + { /* Normal Case */ + l_header_data_start = &(l_current_data); + l_header_data = *l_header_data_start; + l_remaining_length = p_src_data+p_max_length-l_header_data; + l_modified_length_ptr = &(l_remaining_length); + } + bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr); + l_present = bio_read(l_bio, 1); + if + (!l_present) + { + bio_inalign(l_bio); + l_header_data += bio_numbytes(l_bio); + bio_destroy(l_bio); /* EPH markers */ - - if (tcp->csty & J2K_CP_CSTY_EPH) { - if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { + if (p_tcp->csty & J2K_CP_CSTY_EPH) { + if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { printf("Error : expected EPH marker\n"); } else { - hd += 2; + l_header_data += 2; } } - + l_header_length = (l_header_data - *l_header_data_start); + *l_modified_length_ptr -= l_header_length; + *l_header_data_start += l_header_length; /* << INDEX */ // End of packet header position. Currently only represents the distance to start of packet // Will be updated later by incrementing with packet start value - if(pack_info) { - pack_info->end_ph_pos = (int)(c - src); + if + (p_pack_info) + { + p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); } /* INDEX >> */ - - if (cp->ppm == 1) { /* PPM case */ - cp->ppm_len += cp->ppm_data-hd; - cp->ppm_data = hd; - return (c - src); - } - if (tcp->ppt == 1) { /* PPT case */ - tcp->ppt_len+=tcp->ppt_data-hd; - tcp->ppt_data = hd; - return (c - src); - } - - return (hd - src); + * p_is_data_present = false; + *p_data_read = l_current_data - p_src_data; + return true; } - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; + l_band = l_res->bands; + for + (bandno = 0; bandno < l_res->numbands; ++bandno) + { + opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]); - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int included, increment, n, segno; - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; + if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) + { + ++l_band; + continue; + } + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + for + (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) + { + OPJ_UINT32 l_included,l_increment, l_segno; + OPJ_INT32 n; /* if cblk not yet included before --> inclusion tagtree */ - if (!cblk->numsegs) { - included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); + if + (!l_cblk->numsegs) + { + l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1); /* else one bit */ - } else { - included = bio_read(bio, 1); + } + else + { + l_included = bio_read(l_bio, 1); } /* if cblk not included */ - if (!included) { - cblk->numnewpasses = 0; + if + (!l_included) + { + l_cblk->numnewpasses = 0; + ++l_cblk; continue; } /* if cblk not yet included --> zero-bitplane tagtree */ - if (!cblk->numsegs) { - int i, numimsbs; - for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) { - ; + if + (!l_cblk->numsegs) + { + OPJ_UINT32 i = 0; + while + (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) + { + ++i; } - numimsbs = i - 1; - cblk->numbps = band->numbps - numimsbs; - cblk->numlenbits = 3; + l_cblk->numbps = l_band->numbps + 1 - i; + l_cblk->numlenbits = 3; } /* number of coding passes */ - cblk->numnewpasses = t2_getnumpasses(bio); - increment = t2_getcommacode(bio); + l_cblk->numnewpasses = t2_getnumpasses(l_bio); + l_increment = t2_getcommacode(l_bio); /* length indicator increment */ - cblk->numlenbits += increment; - segno = 0; - if (!cblk->numsegs) { - t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1); - } else { - segno = cblk->numsegs - 1; - if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) { - ++segno; - t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); + l_cblk->numlenbits += l_increment; + l_segno = 0; + if + (!l_cblk->numsegs) + { + if + (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) + { + bio_destroy(l_bio); + return false; + } + + } + else + { + l_segno = l_cblk->numsegs - 1; + if + (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) + { + ++l_segno; + if + (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) + { + bio_destroy(l_bio); + return false; + } } } - n = cblk->numnewpasses; + n = l_cblk->numnewpasses; do { - cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n); - cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses)); - n -= cblk->segs[segno].numnewpasses; - if (n > 0) { - ++segno; - t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); + l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n); + l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses)); + n -= l_cblk->segs[l_segno].numnewpasses; + if + (n > 0) + { + ++l_segno; + if + (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) + { + bio_destroy(l_bio); + return false; + } } - } while (n > 0); + } + while (n > 0); + ++l_cblk; } + ++l_band; } - if (bio_inalign(bio)) { - bio_destroy(bio); - return -999; + if + (bio_inalign(l_bio)) + { + bio_destroy(l_bio); + return false; } - hd += bio_numbytes(bio); - bio_destroy(bio); + l_header_data += bio_numbytes(l_bio); + bio_destroy(l_bio); /* EPH markers */ - if (tcp->csty & J2K_CP_CSTY_EPH) { - if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { - opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); + if (p_tcp->csty & J2K_CP_CSTY_EPH) { + if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { + // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); } else { - hd += 2; + l_header_data += 2; } } + + l_header_length = (l_header_data - *l_header_data_start); + *l_modified_length_ptr -= l_header_length; + *l_header_data_start += l_header_length; /* << INDEX */ // End of packet header position. Currently only represents the distance to start of packet // Will be updated later by incrementing with packet start value - if(pack_info) { - pack_info->end_ph_pos = (int)(hd - src); + if + (p_pack_info) + { + p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); } /* INDEX >> */ - - if (cp->ppm==1) { - cp->ppm_len+=cp->ppm_data-hd; - cp->ppm_data = hd; - } else if (tcp->ppt == 1) { - tcp->ppt_len+=tcp->ppt_data-hd; - tcp->ppt_data = hd; - } else { - c=hd; - } - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - opj_tcd_precinct_t *prc = &band->precincts[precno]; + * p_is_data_present = true; + *p_data_read = l_current_data - p_src_data; + return true; +} + +static bool t2_read_packet_data( + opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info) +{ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + OPJ_BYTE *l_current_data = p_src_data; + opj_tcd_band_t *l_band = 00; + opj_tcd_cblk_dec_t* l_cblk = 00; + opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + l_band = l_res->bands; + for + (bandno = 0; bandno < l_res->numbands; ++bandno) + { + opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; - if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; - opj_tcd_seg_t *seg = NULL; - if (!cblk->numnewpasses) + if + ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) + { + ++l_band; + continue; + } + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + for + (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) + { + opj_tcd_seg_t *l_seg = 00; + if + (!l_cblk->numnewpasses) + { + /* nothing to do */ + ++l_cblk; continue; - if (!cblk->numsegs) { - seg = &cblk->segs[0]; - cblk->numsegs++; - cblk->len = 0; - } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - seg++; - cblk->numsegs++; + } + if + (!l_cblk->numsegs) + { + l_seg = l_cblk->segs; + ++l_cblk->numsegs; + l_cblk->len = 0; + } + else + { + l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; + if + (l_seg->numpasses == l_seg->maxpasses) + { + ++l_seg; + ++l_cblk->numsegs; } } - do { - if (c + seg->newlen > src + len) { - return -999; + do + { + if + (l_current_data + l_seg->newlen > p_src_data + p_max_length) + { + return false; } #ifdef USE_JPWL @@ -565,99 +790,337 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t #endif /* USE_JPWL */ - cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*)); - memcpy(cblk->data + cblk->len, c, seg->newlen); - if (seg->numpasses == 0) { - seg->data = &cblk->data; - seg->dataindex = cblk->len; + memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen); + if + (l_seg->numpasses == 0) + { + l_seg->data = &l_cblk->data; + l_seg->dataindex = l_cblk->len; } - c += seg->newlen; - cblk->len += seg->newlen; - seg->len += seg->newlen; - seg->numpasses += seg->numnewpasses; - cblk->numnewpasses -= seg->numnewpasses; - if (cblk->numnewpasses > 0) { - seg++; - cblk->numsegs++; + l_current_data += l_seg->newlen; + l_seg->numpasses += l_seg->numnewpasses; + l_cblk->numnewpasses -= l_seg->numnewpasses; + + l_seg->real_num_passes = l_seg->numpasses; + l_cblk->len += l_seg->newlen; + l_seg->len += l_seg->newlen; + if + (l_cblk->numnewpasses > 0) + { + ++l_seg; + ++l_cblk->numsegs; } - } while (cblk->numnewpasses > 0); + } + while (l_cblk->numnewpasses > 0); + l_cblk->real_num_segs = l_cblk->numsegs; + ++l_cblk; + } + ++l_band; + } + *(p_data_read) = l_current_data - p_src_data; + return true; +} + + +static bool t2_skip_packet_data( + opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_pi_iterator_t *p_pi, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *pack_info) +{ + OPJ_UINT32 bandno, cblkno; + OPJ_UINT32 l_nb_code_blocks; + opj_tcd_band_t *l_band = 00; + opj_tcd_cblk_dec_t* l_cblk = 00; + + opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; + + *p_data_read = 0; + l_band = l_res->bands; + for + (bandno = 0; bandno < l_res->numbands; ++bandno) + { + opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; + + if + ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) + { + ++l_band; + continue; + } + l_nb_code_blocks = l_prc->cw * l_prc->ch; + l_cblk = l_prc->cblks.dec; + for + (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) + { + opj_tcd_seg_t *l_seg = 00; + if + (!l_cblk->numnewpasses) + { + /* nothing to do */ + ++l_cblk; + continue; + } + if + (!l_cblk->numsegs) + { + l_seg = l_cblk->segs; + ++l_cblk->numsegs; + l_cblk->len = 0; + } + else + { + l_seg = &l_cblk->segs[l_cblk->numsegs - 1]; + if + (l_seg->numpasses == l_seg->maxpasses) + { + ++l_seg; + ++l_cblk->numsegs; + } + } + + do + { + if + (* p_data_read + l_seg->newlen > p_max_length) + { + return false; + } + +#ifdef USE_JPWL + /* we need here a j2k handle to verify if making a check to + the validity of cblocks parameters is selected from user (-W) */ + + /* let's check that we are not exceeding */ + if ((cblk->len + seg->newlen) > 8192) { + opj_event_msg(t2->cinfo, EVT_WARNING, + "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", + seg->newlen, cblkno, precno, bandno, resno, compno); + if (!JPWL_ASSUME) { + opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); + return -999; + } + seg->newlen = 8192 - cblk->len; + opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen); + break; + }; + +#endif /* USE_JPWL */ + *(p_data_read) += l_seg->newlen; + l_seg->numpasses += l_seg->numnewpasses; + l_cblk->numnewpasses -= l_seg->numnewpasses; + if + (l_cblk->numnewpasses > 0) + { + ++l_seg; + ++l_cblk->numsegs; + } + } + while (l_cblk->numnewpasses > 0); + ++l_cblk; } } + return true; +} + +static bool t2_decode_packet( + opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info) +{ + bool l_read_data; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_nb_total_bytes_read = 0; + + *p_data_read = 0; - return (c - src); + if + (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) + { + return false; + } + p_src += l_nb_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + p_max_length -= l_nb_bytes_read; + /* we should read data for the packet */ + if + (l_read_data) + { + l_nb_bytes_read = 0; + if + (! t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) + { + return false; + } + l_nb_total_bytes_read += l_nb_bytes_read; + } + *p_data_read = l_nb_total_bytes_read; + return true; +} + +static bool t2_skip_packet( + opj_t2_t* p_t2, + opj_tcd_tile_t *p_tile, + opj_tcp_t *p_tcp, + opj_pi_iterator_t *p_pi, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_length, + opj_packet_info_t *p_pack_info) +{ + bool l_read_data; + OPJ_UINT32 l_nb_bytes_read = 0; + OPJ_UINT32 l_nb_total_bytes_read = 0; + + *p_data_read = 0; + + if + (! t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) + { + return false; + } + p_src += l_nb_bytes_read; + l_nb_total_bytes_read += l_nb_bytes_read; + p_max_length -= l_nb_bytes_read; + /* we should read data for the packet */ + if + (l_read_data) + { + l_nb_bytes_read = 0; + if + (! t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) + { + return false; + } + l_nb_total_bytes_read += l_nb_bytes_read; + } + *p_data_read = l_nb_total_bytes_read; + return true; } /* ----------------------------------------------------------------------- */ -int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){ - unsigned char *c = dest; - int e = 0; - int compno; - opj_pi_iterator_t *pi = NULL; - int poc; - opj_image_t *image = t2->image; - opj_cp_t *cp = t2->cp; - opj_tcp_t *tcp = &cp->tcps[tileno]; - int pocno = cp->cinema == CINEMA4K_24? 2: 1; - int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1; - - pi = pi_initialise_encode(image, cp, tileno, t2_mode); - if(!pi) { - /* TODO: throw an error */ - return -999; +bool t2_encode_packets( + opj_t2_t* p_t2, + OPJ_UINT32 p_tile_no, + opj_tcd_tile_t *p_tile, + OPJ_UINT32 p_maxlayers, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_len, + opj_codestream_info_t *cstr_info, + OPJ_UINT32 p_tp_num, + OPJ_INT32 p_tp_pos, + OPJ_UINT32 p_pino, + J2K_T2_MODE p_t2_mode) +{ + OPJ_BYTE *l_current_data = p_dest; + OPJ_UINT32 l_nb_bytes = 0; + OPJ_UINT32 compno; + OPJ_UINT32 poc; + opj_pi_iterator_t *l_pi = 00; + opj_pi_iterator_t *l_current_pi = 00; + opj_image_t *l_image = p_t2->image; + opj_cp_t *l_cp = p_t2->cp; + opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no]; + OPJ_UINT32 pocno = l_cp->m_specific_param.m_enc.m_cinema == CINEMA4K_24? 2: 1; + OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1; + OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; + + l_pi = pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode); + if + (!l_pi) + { + return false; } - - if(t2_mode == THRESH_CALC ){ /* Calculating threshold */ - for(compno = 0; compno < maxcomp; compno++ ){ - for(poc = 0; poc < pocno ; poc++){ - int comp_len = 0; - int tpnum = compno; - if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) { - opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n"); - return -999; - } - while (pi_next(&pi[poc])) { - if (pi[poc].layno < maxlayers) { - e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno); - comp_len = comp_len + e; - if (e == -999) { - break; - } else { - c += e; + * p_data_written = 0; + if + (p_t2_mode == THRESH_CALC ) + { /* Calculating threshold */ + l_current_pi = l_pi; + for + (compno = 0; compno < l_max_comp; ++compno) + { + OPJ_UINT32 l_comp_len = 0; + l_current_pi = l_pi; + + for + (poc = 0; poc < pocno ; ++poc) + { + OPJ_UINT32 l_tp_num = compno; + pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode); + while + (pi_next(l_current_pi)) + { + if + (l_current_pi->layno < p_maxlayers) + { + l_nb_bytes = 0; + if + (! t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) + { + pi_destroy(l_pi, l_nb_pocs); + return false; } + l_comp_len += l_nb_bytes; + l_current_data += l_nb_bytes; + p_max_len -= l_nb_bytes; + * p_data_written += l_nb_bytes; } } - if (e == -999) break; - if (cp->max_comp_size){ - if (comp_len > cp->max_comp_size){ - e = -999; - break; + if + (l_cp->m_specific_param.m_enc.m_max_comp_size) + { + if + (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) + { + pi_destroy(l_pi, l_nb_pocs); + return false; } } + ++l_current_pi; } - if (e == -999) break; } - }else{ /* t2_mode == FINAL_PASS */ - pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp); - while (pi_next(&pi[pino])) { - if (pi[pino].layno < maxlayers) { - e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno); - if (e == -999) { - break; - } else { - c += e; + } + else + { /* t2_mode == FINAL_PASS */ + pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode); + l_current_pi = &l_pi[p_pino]; + while + (pi_next(l_current_pi)) + { + if + (l_current_pi->layno < p_maxlayers) + { + l_nb_bytes=0; + if + (! t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) + { + pi_destroy(l_pi, l_nb_pocs); + return false; } + l_current_data += l_nb_bytes; + p_max_len -= l_nb_bytes; + * p_data_written += l_nb_bytes; + /* INDEX >> */ if(cstr_info) { if(cstr_info->index_write) { - opj_tile_info_t *info_TL = &cstr_info->tile[tileno]; + opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no]; opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; if (!cstr_info->packno) { info_PK->start_pos = info_TL->end_header + 1; } else { - info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; + info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; } - info_PK->end_pos = info_PK->start_pos + e - 1; + info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1; info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance // to start of packet is incremented by value of start of packet } @@ -665,123 +1128,163 @@ int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlaye cstr_info->packno++; } /* << INDEX */ - tile->packno++; + ++p_tile->packno; } } } - - pi_destroy(pi, cp, tileno); - - if (e == -999) { - return e; - } - - return (c - dest); + pi_destroy(l_pi, l_nb_pocs); + return true; } -int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) { - unsigned char *c = src; - opj_pi_iterator_t *pi; - int pino, e = 0; - int n = 0, curtp = 0; - int tp_start_packno; +bool t2_decode_packets( + opj_t2_t *p_t2, + OPJ_UINT32 p_tile_no, + struct opj_tcd_tile *p_tile, + OPJ_BYTE *p_src, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_len, + struct opj_codestream_info *p_cstr_info) +{ + OPJ_BYTE *l_current_data = p_src; + opj_pi_iterator_t *l_pi = 00; + OPJ_UINT32 pino; + opj_image_t *l_image = p_t2->image; + opj_cp_t *l_cp = p_t2->cp; + opj_cp_t *cp = p_t2->cp; + opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]); + OPJ_UINT32 l_nb_bytes_read; + OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1; + opj_pi_iterator_t *l_current_pi = 00; + OPJ_UINT32 curtp = 0; + OPJ_UINT32 tp_start_packno; + opj_packet_info_t *l_pack_info = 00; + opj_image_comp_t* l_img_comp = 00; - opj_image_t *image = t2->image; - opj_cp_t *cp = t2->cp; + + if + (p_cstr_info) + { + l_pack_info = p_cstr_info->tile[p_tile_no].packet; + } /* create a packet iterator */ - pi = pi_create_decode(image, cp, tileno); - if(!pi) { - /* TODO: throw an error */ - return -999; + l_pi = pi_create_decode(l_image, l_cp, p_tile_no); + if + (!l_pi) + { + return false; } tp_start_packno = 0; + l_current_pi = l_pi; - for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) { - while (pi_next(&pi[pino])) { - if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) { - opj_packet_info_t *pack_info; - if (cstr_info) - pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno]; - else - pack_info = NULL; - e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info); - } else { - e = 0; - } + for + (pino = 0; pino <= l_tcp->numpocs; ++pino) + { + while + (pi_next(l_current_pi)) + { - /* progression in resolution */ - image->comps[pi[pino].compno].resno_decoded = - (e > 0) ? - int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) - : image->comps[pi[pino].compno].resno_decoded; - n++; + if + (l_tcp->num_layers_to_decode > l_current_pi->layno && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) + { + l_nb_bytes_read = 0; + if + (! t2_decode_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) + { + pi_destroy(l_pi,l_nb_pocs); + return false; + } + l_img_comp = &(l_image->comps[l_current_pi->compno]); + l_img_comp->resno_decoded = uint_max(l_current_pi->resno, l_img_comp->resno_decoded); + } + else + { + l_nb_bytes_read = 0; + if + (! t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) + { + pi_destroy(l_pi,l_nb_pocs); + return false; + } + } + l_current_data += l_nb_bytes_read; + p_max_len -= l_nb_bytes_read; /* INDEX >> */ - if(cstr_info) { - opj_tile_info_t *info_TL = &cstr_info->tile[tileno]; - opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno]; - if (!cstr_info->packno) { + if(p_cstr_info) { + opj_tile_info_t *info_TL = &p_cstr_info->tile[p_tile_no]; + opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno]; + if (!p_cstr_info->packno) { info_PK->start_pos = info_TL->end_header + 1; - } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ // New tile part - info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part - tp_start_packno = cstr_info->packno; + } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ // New tile part + info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in previous tile-part + tp_start_packno = p_cstr_info->packno; curtp++; - info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1; + info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1; } else { - info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1; + info_PK->start_pos = (cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1; } - info_PK->end_pos = info_PK->start_pos + e - 1; + info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1; info_PK->end_ph_pos += info_PK->start_pos - 1; // End of packet header which now only represents the distance - // to start of packet is incremented by value of start of packet - cstr_info->packno++; + ++p_cstr_info->packno; } /* << INDEX */ - - if (e == -999) { /* ADD */ - break; - } else { - c += e; - } } + ++l_current_pi; } /* INDEX >> */ - if(cstr_info) { - cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; // Number of packets in last tile-part + if + (p_cstr_info) { + p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; // Number of packets in last tile-part } /* << INDEX */ /* don't forget to release pi */ - pi_destroy(pi, cp, tileno); - - if (e == -999) { - return e; - } - - return (c - src); + pi_destroy(l_pi,l_nb_pocs); + *p_data_read = l_current_data - p_src; + return true; } /* ----------------------------------------------------------------------- */ - -opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) { +/** + * Creates a Tier 2 handle + * + * @param p_image Source or destination image + * @param p_cp Image coding parameters. + * @return a new T2 handle if successful, NULL otherwise. +*/ +opj_t2_t* t2_create( + opj_image_t *p_image, + opj_cp_t *p_cp) +{ /* create the tcd structure */ - opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t)); - if(!t2) return NULL; - t2->cinfo = cinfo; - t2->image = image; - t2->cp = cp; - - return t2; + opj_t2_t *l_t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t)); + if + (!l_t2) + { + return 00; + } + memset(l_t2,0,sizeof(opj_t2_t)); + l_t2->image = p_image; + l_t2->cp = p_cp; + return l_t2; } -void t2_destroy(opj_t2_t *t2) { - if(t2) { - opj_free(t2); +/** + * Destroys a Tier 2 handle. + * + * @param p_t2 the Tier 2 handle to destroy +*/ +void t2_destroy(opj_t2_t *p_t2) +{ + if + (p_t2) + { + opj_free(p_t2); } } - diff --git a/libopenjpeg/t2.h b/libopenjpeg/t2.h index b15b7520..770a1df5 100644 --- a/libopenjpeg/t2.h +++ b/libopenjpeg/t2.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,21 +36,36 @@ @brief Implementation of a tier-2 coding (packetization of code-block data) (T2) */ +#include "openjpeg.h" + +struct opj_common_struct; +struct opj_image; +struct opj_cp; +struct opj_tcd_tile; +struct opj_codestream_info; /** @defgroup T2 T2 - Implementation of a tier-2 coding */ /*@{*/ +/** +T2 encoding mode +*/ +typedef enum T2_MODE +{ + THRESH_CALC = 0, /** Function called in Rate allocation process*/ + FINAL_PASS = 1 /** Function called in Tier 2 process*/ +} +J2K_T2_MODE; + /** Tier-2 coding */ -typedef struct opj_t2 { - /** codec context */ - opj_common_ptr cinfo; +typedef struct opj_t2 { /** Encoding: pointer to the src image. Decoding: pointer to the dst image. */ - opj_image_t *image; + struct opj_image *image; /** pointer to the image coding parameters */ - opj_cp_t *cp; + struct opj_cp *cp; } opj_t2_t; /** @name Exported functions */ @@ -68,9 +84,8 @@ Encode the packets of a tile to a destination buffer @param tpnum Tile part number of the current tile @param tppos The position of the tile part flag in the progression order @param t2_mode If == 0 In Threshold calculation ,If == 1 Final pass -@param cur_totnum_tp The total number of tile parts in the current tile */ -int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino,J2K_T2_MODE t2_mode,int cur_totnum_tp); +bool t2_encode_packets(opj_t2_t* t2,OPJ_UINT32 tileno, struct opj_tcd_tile *tile, OPJ_UINT32 maxlayers, OPJ_BYTE *dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 len, struct opj_codestream_info *cstr_info,OPJ_UINT32 tpnum, OPJ_INT32 tppos,OPJ_UINT32 pino,J2K_T2_MODE t2_mode); /** Decode the packets of a tile from a source buffer @param t2 T2 handle @@ -79,19 +94,21 @@ Decode the packets of a tile from a source buffer @param tileno number that identifies the tile for which to decode the packets @param tile tile for which to decode the packets */ -int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info); +bool t2_decode_packets(opj_t2_t *t2, OPJ_UINT32 tileno,struct opj_tcd_tile *tile, OPJ_BYTE *src, OPJ_UINT32 * p_data_read, OPJ_UINT32 len, struct opj_codestream_info *cstr_info); /** -Create a T2 handle -@param cinfo Codec context info -@param image Source or destination image -@param cp Image coding parameters -@return Returns a new T2 handle if successful, returns NULL otherwise + * Creates a Tier 2 handle + * + * @param p_image Source or destination image + * @param p_cp Image coding parameters. + * @return a new T2 handle if successful, NULL otherwise. */ -opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp); +opj_t2_t* t2_create(struct opj_image *p_image, struct opj_cp *p_cp); + /** -Destroy a T2 handle -@param t2 T2 handle to destroy + * Destroys a Tier 2 handle. + * + * @param p_t2 the Tier 2 handle to destroy */ void t2_destroy(opj_t2_t *t2); diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index 01ee3950..1cfbc5af 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -6,6 +6,7 @@ * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team * Copyright (c) 2006-2007, Parvatha Elangovan + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,827 +31,743 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "tcd.h" +#include "openjpeg.h" +#include "j2k.h" #include "opj_includes.h" +#include "event.h" +#include "t2.h" +#include "t1.h" +#include "opj_malloc.h" +#include "int.h" +#include "tgt.h" +#include "dwt.h" +#include "mct.h" +#include "j2k_lib.h" +#include "profile.h" -void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) { - int tileno, compno, resno, bandno, precno;//, cblkno; - - fprintf(fd, "image {\n"); - fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", - img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1); - - for (tileno = 0; tileno < img->th * img->tw; tileno++) { - opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno]; - fprintf(fd, " tile {\n"); - fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n", - tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - fprintf(fd, " tilec {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n", - tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions); - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - fprintf(fd, "\n res {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n", - res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands); - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - fprintf(fd, " band {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n", - band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); - for (precno = 0; precno < res->pw * res->ph; precno++) { - opj_tcd_precinct_t *prec = &band->precincts[precno]; - fprintf(fd, " prec {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n", - prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch); - /* - for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prec->cblks[cblkno]; - fprintf(fd, " cblk {\n"); - fprintf(fd, - " x0=%d, y0=%d, x1=%d, y1=%d\n", - cblk->x0, cblk->y0, cblk->x1, cblk->y1); - fprintf(fd, " }\n"); - } - */ - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, " }\n"); - } - fprintf(fd, "}\n"); -} +/** + * Deallocates the encoding data of the given precinct. + */ +static void tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct); +/** + * Allocates memory for an encoding code block. + */ +static bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block); +/** + * Allocates memory for a decoding code block. + */ +static bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block); +/** +Free the memory allocated for encoding +@param tcd TCD handle +*/ +static void tcd_free_tile(opj_tcd_t *tcd); /* ----------------------------------------------------------------------- */ /** Create a new TCD handle */ -opj_tcd_t* tcd_create(opj_common_ptr cinfo) { - /* create the tcd structure */ - opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t)); - if(!tcd) return NULL; - tcd->cinfo = cinfo; - tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t)); - if(!tcd->tcd_image) { - opj_free(tcd); - return NULL; - } +opj_tcd_t* tcd_create(bool p_is_decoder) +{ + opj_tcd_t *l_tcd = 00; - return tcd; + /* create the tcd structure */ + l_tcd = (opj_tcd_t*) opj_malloc(sizeof(opj_tcd_t)); + if + (!l_tcd) + { + return 00; + } + memset(l_tcd,0,sizeof(opj_tcd_t)); + l_tcd->m_is_decoder = p_is_decoder ? 1 : 0; + l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t)); + if + (!l_tcd->tcd_image) + { + opj_free(l_tcd); + return 00; + } + memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t)); + return l_tcd; } /** Destroy a previously created TCD handle */ void tcd_destroy(opj_tcd_t *tcd) { - if(tcd) { - opj_free(tcd->tcd_image); + if + (tcd) + { + tcd_free_tile(tcd); + if + (tcd->tcd_image) + { + opj_free(tcd->tcd_image); + tcd->tcd_image = 00; + } opj_free(tcd); } } /* ----------------------------------------------------------------------- */ +/** + * Initialize the tile coder and may reuse some meory. + * @param p_tcd TCD handle. + * @param p_image raw image. + * @param p_cp coding parameters. + * @param p_tile_no current tile index to encode. + * + * @return true if the encoding values could be set (false otherwise). +*/ +#define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \ +bool FUNCTION \ + ( \ + opj_tcd_t *p_tcd, \ + OPJ_UINT32 p_tile_no \ + ) \ +{ \ + OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \ + OPJ_UINT32 compno, resno, bandno, precno, cblkno; \ + opj_tcp_t * l_tcp = 00; \ + opj_cp_t * l_cp = 00; \ + opj_tcd_tile_t * l_tile = 00; \ + opj_tccp_t *l_tccp = 00; \ + opj_tcd_tilecomp_t *l_tilec = 00; \ + opj_image_comp_t * l_image_comp = 00; \ + opj_tcd_resolution_t *l_res = 00; \ + opj_tcd_band_t *l_band = 00; \ + opj_stepsize_t * l_step_size = 00; \ + opj_tcd_precinct_t *l_current_precinct = 00; \ + TYPE* l_code_block = 00; \ + opj_image_t * l_image = 00; \ + OPJ_UINT32 p,q; \ + OPJ_UINT32 l_level_no; \ + OPJ_UINT32 l_pdx, l_pdy; \ + OPJ_UINT32 l_gain; \ + OPJ_INT32 l_x0b, l_y0b; \ + /* extent of precincts , top left, bottom right**/ \ + OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \ + /* number of precinct for a resolution */ \ + OPJ_UINT32 l_nb_precincts; \ + /* room needed to store l_nb_precinct precinct for a resolution */ \ + OPJ_UINT32 l_nb_precinct_size; \ + /* number of code blocks for a precinct*/ \ + OPJ_UINT32 l_nb_code_blocks; \ + /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \ + OPJ_UINT32 l_nb_code_blocks_size; \ + /* size of data for a tile */ \ + OPJ_UINT32 l_data_size; \ + l_cp = p_tcd->cp; \ + l_tcp = &(l_cp->tcps[p_tile_no]); \ + l_tile = p_tcd->tcd_image->tiles; \ + l_tccp = l_tcp->tccps; \ + l_tilec = l_tile->comps; \ + l_image = p_tcd->image; \ + l_image_comp = p_tcd->image->comps; \ + \ + p = p_tile_no % l_cp->tw; /* tile coordinates */ \ + q = p_tile_no / l_cp->tw; \ + \ + /* 4 borders of the tile rescale on the image if necessary */ \ + l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \ + l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \ + l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \ + l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \ + /*tile->numcomps = image->numcomps; */ \ + for \ + (compno = 0; compno < l_tile->numcomps; ++compno) \ + { \ + /* border of each l_tile component (global) */ \ + l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \ + l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \ + l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \ + l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \ + \ + l_data_size = (l_tilec->x1 - l_tilec->x0) \ + * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \ + l_tilec->numresolutions = l_tccp->numresolutions; \ + if \ + (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce)\ + { \ + l_tilec->minimum_num_resolutions = 1; \ + } \ + else \ + { \ + l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;\ + } \ + if \ + (l_tilec->data == 00) \ + { \ + l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_data_size); \ + if \ + (! l_tilec->data ) \ + { \ + return false; \ + } \ + l_tilec->data_size = l_data_size; \ + } \ + else if \ + (l_data_size > l_tilec->data_size) \ + { \ + l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);\ + if \ + (! l_tilec->data) \ + { \ + return false; \ + } \ + l_tilec->data_size = l_data_size; \ + } \ + l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_t);\ + if \ + (l_tilec->resolutions == 00) \ + { \ + l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);\ + if \ + (! l_tilec->resolutions ) \ + { \ + return false; \ + } \ + l_tilec->resolutions_size = l_data_size; \ + memset(l_tilec->resolutions,0,l_data_size); \ + } \ + else if \ + (l_data_size > l_tilec->resolutions_size) \ + { \ + l_tilec->resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);\ + if \ + (! l_tilec->resolutions) \ + { \ + return false; \ + } \ + memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);\ + l_tilec->resolutions_size = l_data_size; \ + } \ + l_level_no = l_tilec->numresolutions - 1; \ + l_res = l_tilec->resolutions; \ + l_step_size = l_tccp->stepsizes; \ + if \ + (l_tccp->qmfbid == 0) \ + { \ + l_gain_ptr = &dwt_getgain_real; \ + } \ + else \ + { \ + l_gain_ptr = &dwt_getgain; \ + } \ + for \ + (resno = 0; resno < l_tilec->numresolutions; ++resno) \ + { \ + OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \ + OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \ + OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \ + /* border for each resolution level (global) */ \ + l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \ + l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \ + l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \ + l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \ + /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */\ + l_pdx = l_tccp->prcw[resno]; \ + l_pdy = l_tccp->prch[resno]; \ + /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \ + l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \ + l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \ + l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \ + l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \ + \ + l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);\ + l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);\ + l_nb_precincts = l_res->pw * l_res->ph; \ + l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_t);\ + if \ + (resno == 0) \ + { \ + tlcbgxstart = l_tl_prc_x_start; \ + tlcbgystart = l_tl_prc_y_start; \ + brcbgxend = l_br_prc_x_end; \ + brcbgyend = l_br_prc_y_end; \ + cbgwidthexpn = l_pdx; \ + cbgheightexpn = l_pdy; \ + l_res->numbands = 1; \ + } \ + else \ + { \ + tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \ + tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \ + brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \ + brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \ + cbgwidthexpn = l_pdx - 1; \ + cbgheightexpn = l_pdy - 1; \ + l_res->numbands = 3; \ + } \ + \ + cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \ + cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \ + l_band = l_res->bands; \ + for \ + (bandno = 0; bandno < l_res->numbands; ++bandno) \ + { \ + OPJ_INT32 numbps; \ + if \ + (resno == 0) \ + { \ + l_band->bandno = 0 ; \ + l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \ + l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \ + l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \ + l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \ + } \ + else \ + { \ + l_band->bandno = bandno + 1; \ + /* x0b = 1 if bandno = 1 or 3 */ \ + l_x0b = l_band->bandno&1; \ + /* y0b = 1 if bandno = 2 or 3 */ \ + l_y0b = (l_band->bandno)>>1; \ + /* l_band border (global) */ \ + l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1);\ + l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1);\ + l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1);\ + l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1);\ + } \ + /** avoid an if with storing function pointer */ \ + l_gain = (*l_gain_ptr) (l_band->bandno); \ + numbps = l_image_comp->prec + l_gain; \ + l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\ + l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */\ + if \ + (! l_band->precincts) \ + { \ + l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */ l_nb_precinct_size);\ + if \ + (! l_band->precincts) \ + { \ + return false; \ + } \ + memset(l_band->precincts,0,l_nb_precinct_size); \ + l_band->precincts_data_size = l_nb_precinct_size; \ + } \ + else if \ + (l_band->precincts_data_size < l_nb_precinct_size) \ + { \ + l_band->precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\ + if \ + (! l_band->precincts) \ + { \ + return false; \ + } \ + memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\ + l_band->precincts_data_size = l_nb_precinct_size; \ + } \ + l_current_precinct = l_band->precincts; \ + for \ + (precno = 0; precno < l_nb_precincts; ++precno) \ + { \ + OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \ + OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn);\ + OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn);\ + OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \ + OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \ + /* precinct size (global) */ \ + l_current_precinct->x0 = int_max(cbgxstart, l_band->x0);\ + l_current_precinct->y0 = int_max(cbgystart, l_band->y0);\ + l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \ + l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \ + tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn;\ + tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn;\ + brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn;\ + brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn;\ + l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;\ + l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;\ + l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;\ + l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE);\ + if \ + (! l_current_precinct->cblks.ELEMENT) \ + { \ + l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\ + if \ + (! l_current_precinct->cblks.ELEMENT ) \ + { \ + return false; \ + } \ + memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\ + l_current_precinct->block_size = l_nb_code_blocks_size;\ + } \ + else if \ + (l_nb_code_blocks_size > l_current_precinct->block_size)\ + { \ + l_current_precinct->cblks.ELEMENT = (TYPE*) \ + opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);\ + if \ + (! l_current_precinct->cblks.ELEMENT ) \ + { \ + return false; \ + } \ + memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size\ + ,0 \ + ,l_nb_code_blocks_size - l_current_precinct->block_size);\ + l_current_precinct->block_size = l_nb_code_blocks_size;\ + } \ + if \ + (! l_current_precinct->incltree) \ + { \ + l_current_precinct->incltree = tgt_create(l_current_precinct->cw,\ + l_current_precinct->ch);\ + } \ + else \ + { \ + l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,\ + l_current_precinct->cw, \ + l_current_precinct->ch);\ + } \ + if \ + (! l_current_precinct->incltree) \ + { \ + return false; \ + } \ + if \ + (! l_current_precinct->imsbtree) \ + { \ + l_current_precinct->imsbtree = tgt_create( \ + l_current_precinct->cw,\ + l_current_precinct->ch);\ + } \ + else \ + { \ + l_current_precinct->imsbtree = tgt_init( \ + l_current_precinct->imsbtree,\ + l_current_precinct->cw,\ + l_current_precinct->ch);\ + } \ + if \ + (! l_current_precinct->imsbtree) \ + { \ + return false; \ + } \ + l_code_block = l_current_precinct->cblks.ELEMENT; \ + for \ + (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) \ + { \ + OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);\ + OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);\ + OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \ + OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \ + /* code-block size (global) */ \ + l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0);\ + l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0);\ + l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1);\ + l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1);\ + if \ + (! FUNCTION_ELEMENT(l_code_block)) \ + { \ + return false; \ + } \ + ++l_code_block; \ + } \ + ++l_current_precinct; \ + } /* precno */ \ + ++l_band; \ + ++l_step_size; \ + } /* bandno */ \ + ++l_res; \ + --l_level_no; \ + } /* resno */ \ + ++l_tccp; \ + ++l_tilec; \ + ++l_image_comp; \ + } /* compno */ \ + return true; \ +} \ -void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) { - int tileno, compno, resno, bandno, precno, cblkno; +MACRO_TCD_ALLOCATE(tcd_init_encode_tile,opj_tcd_cblk_enc_t,1.f,enc,tcd_code_block_enc_allocate) +MACRO_TCD_ALLOCATE(tcd_init_decode_tile,opj_tcd_cblk_dec_t,0.5f,dec,tcd_code_block_dec_allocate) - tcd->image = image; - tcd->cp = cp; - tcd->tcd_image->tw = cp->tw; - tcd->tcd_image->th = cp->th; - tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t)); - - for (tileno = 0; tileno < 1; tileno++) { - opj_tcp_t *tcp = &cp->tcps[curtileno]; - int j; +#undef MACRO_TCD_ALLOCATE - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - int p = curtileno % cp->tw; /* si numerotation matricielle .. */ - int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ - - /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */ - opj_tcd_tile_t *tile = tcd->tcd_image->tiles; - - /* 4 borders of the tile rescale on the image if necessary */ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); - tile->numcomps = image->numcomps; - /* tile->PPT=image->PPT; */ - - /* Modification of the RATE >> */ - for (j = 0; j < tcp->numlayers; j++) { - tcp->rates[j] = tcp->rates[j] ? - cp->tp_on ? - (((float) (tile->numcomps - * (tile->x1 - tile->x0) - * (tile->y1 - tile->y0) - * image->comps[0].prec)) - /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers) - : - ((float) (tile->numcomps - * (tile->x1 - tile->x0) - * (tile->y1 - tile->y0) - * image->comps[0].prec))/ - (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) - : 0; - - if (tcp->rates[j]) { - if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { - tcp->rates[j] = tcp->rates[j - 1] + 20; - } else { - if (!j && tcp->rates[j] < 30) - tcp->rates[j] = 30; - } - - if(j == (tcp->numlayers-1)){ - tcp->rates[j] = tcp->rates[j]- 2; - } - } +/** + * Allocates memory for an encoding code block. + */ +bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block) +{ + if + (! p_code_block->data) + { + p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1); + if + (! p_code_block->data) + { + return false; } - /* << Modification of the RATE */ - - tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t)); - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); - tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); - - tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); - tilec->numresolutions = tccp->numresolutions; - - tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t)); - - for (resno = 0; resno < tilec->numresolutions; resno++) { - int pdx, pdy; - int levelno = tilec->numresolutions - 1 - resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelno); - res->y0 = int_ceildivpow2(tilec->y0, levelno); - res->x1 = int_ceildivpow2(tilec->x1, levelno); - res->y1 = int_ceildivpow2(tilec->y1, levelno); - - res->numbands = resno == 0 ? 1 : 3; - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ - if (tccp->csty & J2K_CCP_CSTY_PRT) { - pdx = tccp->prcw[resno]; - pdy = tccp->prch[resno]; - } else { - pdx = 15; - pdy = 15; - } - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - - res->pw = (brprcxend - tlprcxstart) >> pdx; - res->ph = (brprcyend - tlprcystart) >> pdy; - - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - } - - cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); - - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b, i; - int gain, numbps; - opj_stepsize_t *ss = NULL; - - opj_tcd_band_t *band = &res->bands[bandno]; - - band->bandno = resno == 0 ? 0 : bandno + 1; - x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; - - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelno); - band->y0 = int_ceildivpow2(tilec->y0, levelno); - band->x1 = int_ceildivpow2(tilec->x1, levelno); - band->y1 = int_ceildivpow2(tilec->y1, levelno); - } else { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); - } - - ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); - numbps = image->comps[compno].prec + gain; - - band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t)); - - for (i = 0; i < res->pw * res->ph * 3; i++) { - band->precincts[i].imsbtree = NULL; - band->precincts[i].incltree = NULL; - } - - for (precno = 0; precno < res->pw * res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - - int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); - int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; - - prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t)); - prc->incltree = tgt_create(prc->cw, prc->ch); - prc->imsbtree = tgt_create(prc->cw, prc->ch); - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char)); - /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */ - cblk->data += 2; - cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t)); - cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t)); - } - } - } - } + p_code_block->data+=1; + /* no memset since data */ + p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t)); + if + (! p_code_block->layers) + { + return false; + } + p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t)); + if + (! p_code_block->passes) + { + return false; } } - - /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ + memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t)); + memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t)); + return true; } -void tcd_free_encode(opj_tcd_t *tcd) { - int tileno, compno, resno, bandno, precno, cblkno; +/** + * Allocates memory for a decoding code block. + */ +bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block) +{ + OPJ_UINT32 l_seg_size; - for (tileno = 0; tileno < 1; tileno++) { - opj_tcd_tile_t *tile = tcd->tcd_image->tiles; - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - - for (precno = 0; precno < res->pw * res->ph; precno++) { - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - if (prc->incltree != NULL) { - tgt_destroy(prc->incltree); - prc->incltree = NULL; - } - if (prc->imsbtree != NULL) { - tgt_destroy(prc->imsbtree); - prc->imsbtree = NULL; - } - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_free(prc->cblks.enc[cblkno].data - 2); - opj_free(prc->cblks.enc[cblkno].layers); - opj_free(prc->cblks.enc[cblkno].passes); - } - opj_free(prc->cblks.enc); - } /* for (precno */ - opj_free(band->precincts); - band->precincts = NULL; - } /* for (bandno */ - } /* for (resno */ - opj_free(tilec->resolutions); - tilec->resolutions = NULL; - } /* for (compno */ - opj_free(tile->comps); - tile->comps = NULL; - } /* for (tileno */ - opj_free(tcd->tcd_image->tiles); - tcd->tcd_image->tiles = NULL; -} - -void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) { - int tileno, compno, resno, bandno, precno, cblkno; - - for (tileno = 0; tileno < 1; tileno++) { - opj_tcp_t *tcp = &cp->tcps[curtileno]; - int j; - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - int p = curtileno % cp->tw; - int q = curtileno / cp->tw; - - opj_tcd_tile_t *tile = tcd->tcd_image->tiles; - - /* 4 borders of the tile rescale on the image if necessary */ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); - - tile->numcomps = image->numcomps; - /* tile->PPT=image->PPT; */ - - /* Modification of the RATE >> */ - for (j = 0; j < tcp->numlayers; j++) { - tcp->rates[j] = tcp->rates[j] ? - cp->tp_on ? - (((float) (tile->numcomps - * (tile->x1 - tile->x0) - * (tile->y1 - tile->y0) - * image->comps[0].prec)) - /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers) - : - ((float) (tile->numcomps - * (tile->x1 - tile->x0) - * (tile->y1 - tile->y0) - * image->comps[0].prec))/ - (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) - : 0; - - if (tcp->rates[j]) { - if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) { - tcp->rates[j] = tcp->rates[j - 1] + 20; - } else { - if (!j && tcp->rates[j] < 30) - tcp->rates[j] = 30; - } - } + if + (! p_code_block->data) + { + p_code_block->data = (OPJ_BYTE*) opj_malloc(8192); + if + (! p_code_block->data) + { + return false; } - /* << Modification of the RATE */ - - /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */ - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); - tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); - - tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int)); - tilec->numresolutions = tccp->numresolutions; - /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */ - for (resno = 0; resno < tilec->numresolutions; resno++) { - int pdx, pdy; - - int levelno = tilec->numresolutions - 1 - resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelno); - res->y0 = int_ceildivpow2(tilec->y0, levelno); - res->x1 = int_ceildivpow2(tilec->x1, levelno); - res->y1 = int_ceildivpow2(tilec->y1, levelno); - res->numbands = resno == 0 ? 1 : 3; - - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ - if (tccp->csty & J2K_CCP_CSTY_PRT) { - pdx = tccp->prcw[resno]; - pdy = tccp->prch[resno]; - } else { - pdx = 15; - pdy = 15; - } - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - - res->pw = (brprcxend - tlprcxstart) >> pdx; - res->ph = (brprcyend - tlprcystart) >> pdy; - - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; - } - - cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); - - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b; - int gain, numbps; - opj_stepsize_t *ss = NULL; - - opj_tcd_band_t *band = &res->bands[bandno]; - - band->bandno = resno == 0 ? 0 : bandno + 1; - x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; - - if (band->bandno == 0) { - /* band border */ - band->x0 = int_ceildivpow2(tilec->x0, levelno); - band->y0 = int_ceildivpow2(tilec->y0, levelno); - band->x1 = int_ceildivpow2(tilec->x1, levelno); - band->y1 = int_ceildivpow2(tilec->y1, levelno); - } else { - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); - } - - ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); - numbps = image->comps[compno].prec + gain; - band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - for (precno = 0; precno < res->pw * res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - - int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); - int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - - opj_tcd_precinct_t *prc = &band->precincts[precno]; - - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; - - opj_free(prc->cblks.enc); - prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t)); - - if (prc->incltree != NULL) { - tgt_destroy(prc->incltree); - } - if (prc->imsbtree != NULL) { - tgt_destroy(prc->imsbtree); - } - - prc->incltree = tgt_create(prc->cw, prc->ch); - prc->imsbtree = tgt_create(prc->cw, prc->ch); - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - - opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; - - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char)); - /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */ - cblk->data += 2; - cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t)); - cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t)); - } - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ - } /* tileno */ - - /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ -} - -void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) { - int i, j, tileno, p, q; - unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h; - - tcd->image = image; - tcd->tcd_image->tw = cp->tw; - tcd->tcd_image->th = cp->th; - tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t)); - - /* - Allocate place to store the decoded data = final image - Place limited by the tile really present in the codestream - */ - - for (j = 0; j < cp->tileno_size; j++) { - opj_tcd_tile_t *tile; - - tileno = cp->tileno[j]; - tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]); - tile->numcomps = image->numcomps; - tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t)); + l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t); + p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size); + if + (! p_code_block->segs) + { + return false; + } + memset(p_code_block->segs,0,l_seg_size); + p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS; } + // TODO + //p_code_block->numsegs = 0; + return true; +} - for (i = 0; i < image->numcomps; i++) { - for (j = 0; j < cp->tileno_size; j++) { - opj_tcd_tile_t *tile; - opj_tcd_tilecomp_t *tilec; - - /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - - tileno = cp->tileno[j]; - - tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]); - tilec = &tile->comps[i]; - - p = tileno % cp->tw; /* si numerotation matricielle .. */ - q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */ - - /* 4 borders of the tile rescale on the image if necessary */ - tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0); - tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0); - tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1); - tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1); +/** + * Deallocates the encoding data of the given precinct. + */ +void tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct) +{ + OPJ_UINT32 cblkno , l_nb_code_blocks; - tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx); - tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy); - tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx); - tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy); - - x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0); - y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->x0); - x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1); - y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1); + opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc; + if + (l_code_block) + { + l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t); + for + (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) + { + if + (l_code_block->data) + { + opj_free(l_code_block->data-1); + l_code_block->data = 00; + } + if + (l_code_block->layers) + { + opj_free(l_code_block->layers ); + l_code_block->layers = 00; + } + if + (l_code_block->passes) + { + opj_free(l_code_block->passes ); + l_code_block->passes = 00; + } + ++l_code_block; } - - w = int_ceildivpow2(x1 - x0, image->comps[i].factor); - h = int_ceildivpow2(y1 - y0, image->comps[i].factor); - - image->comps[i].w = w; - image->comps[i].h = h; - image->comps[i].x0 = x0; - image->comps[i].y0 = y0; + opj_free(p_precinct->cblks.enc); + p_precinct->cblks.enc = 00; } } -void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) { - int compno, resno, bandno, precno, cblkno; - opj_tcp_t *tcp; - opj_tcd_tile_t *tile; +/** + * Deallocates the encoding data of the given precinct. + */ +void tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct) +{ + OPJ_UINT32 cblkno , l_nb_code_blocks; - tcd->cp = cp; - - tcp = &(cp->tcps[cp->tileno[tileno]]); - tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]); - - tileno = cp->tileno[tileno]; - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - - /* border of each tile component (global) */ - tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx); - tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy); - tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx); - tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy); - - tilec->numresolutions = tccp->numresolutions; - tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t)); - - for (resno = 0; resno < tilec->numresolutions; resno++) { - int pdx, pdy; - int levelno = tilec->numresolutions - 1 - resno; - int tlprcxstart, tlprcystart, brprcxend, brprcyend; - int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; - int cbgwidthexpn, cbgheightexpn; - int cblkwidthexpn, cblkheightexpn; - - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - - /* border for each resolution level (global) */ - res->x0 = int_ceildivpow2(tilec->x0, levelno); - res->y0 = int_ceildivpow2(tilec->y0, levelno); - res->x1 = int_ceildivpow2(tilec->x1, levelno); - res->y1 = int_ceildivpow2(tilec->y1, levelno); - res->numbands = resno == 0 ? 1 : 3; - - /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ - if (tccp->csty & J2K_CCP_CSTY_PRT) { - pdx = tccp->prcw[resno]; - pdy = tccp->prch[resno]; - } else { - pdx = 15; - pdy = 15; - } - - /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ - tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx; - tlprcystart = int_floordivpow2(res->y0, pdy) << pdy; - brprcxend = int_ceildivpow2(res->x1, pdx) << pdx; - brprcyend = int_ceildivpow2(res->y1, pdy) << pdy; - - res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx); - res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy); - - if (resno == 0) { - tlcbgxstart = tlprcxstart; - tlcbgystart = tlprcystart; - brcbgxend = brprcxend; - brcbgyend = brprcyend; - cbgwidthexpn = pdx; - cbgheightexpn = pdy; - } else { - tlcbgxstart = int_ceildivpow2(tlprcxstart, 1); - tlcbgystart = int_ceildivpow2(tlprcystart, 1); - brcbgxend = int_ceildivpow2(brprcxend, 1); - brcbgyend = int_ceildivpow2(brprcyend, 1); - cbgwidthexpn = pdx - 1; - cbgheightexpn = pdy - 1; + opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec; + if + (l_code_block) + { + l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t); + for + (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) + { + if + (l_code_block->data) + { + opj_free(l_code_block->data); + l_code_block->data = 00; } - - cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn); - cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn); - - for (bandno = 0; bandno < res->numbands; bandno++) { - int x0b, y0b; - int gain, numbps; - opj_stepsize_t *ss = NULL; - - opj_tcd_band_t *band = &res->bands[bandno]; - band->bandno = resno == 0 ? 0 : bandno + 1; - x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0; - y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0; - - if (band->bandno == 0) { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0, levelno); - band->y0 = int_ceildivpow2(tilec->y0, levelno); - band->x1 = int_ceildivpow2(tilec->x1, levelno); - band->y1 = int_ceildivpow2(tilec->y1, levelno); - } else { - /* band border (global) */ - band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1); - band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1); - band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1); - band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1); - } - - ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); - numbps = image->comps[compno].prec + gain; - band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5); - band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ - - band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t)); - - for (precno = 0; precno < res->pw * res->ph; precno++) { - int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; - int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn); - int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn); - int cbgxend = cbgxstart + (1 << cbgwidthexpn); - int cbgyend = cbgystart + (1 << cbgheightexpn); - - opj_tcd_precinct_t *prc = &band->precincts[precno]; - /* precinct size (global) */ - prc->x0 = int_max(cbgxstart, band->x0); - prc->y0 = int_max(cbgystart, band->y0); - prc->x1 = int_min(cbgxend, band->x1); - prc->y1 = int_min(cbgyend, band->y1); - - tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn; - tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn; - brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn; - brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn; - prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; - prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; - - prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t)); - - prc->incltree = tgt_create(prc->cw, prc->ch); - prc->imsbtree = tgt_create(prc->cw, prc->ch); - - for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn); - int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn); - int cblkxend = cblkxstart + (1 << cblkwidthexpn); - int cblkyend = cblkystart + (1 << cblkheightexpn); - - opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; - cblk->data = NULL; - cblk->segs = NULL; - /* code-block size (global) */ - cblk->x0 = int_max(cblkxstart, prc->x0); - cblk->y0 = int_max(cblkystart, prc->y0); - cblk->x1 = int_min(cblkxend, prc->x1); - cblk->y1 = int_min(cblkyend, prc->y1); - cblk->numsegs = 0; - } - } /* precno */ - } /* bandno */ - } /* resno */ - } /* compno */ - /* tcd_dump(stdout, tcd, &tcd->tcd_image); */ + if + (l_code_block->segs) + { + opj_free(l_code_block->segs ); + l_code_block->segs = 00; + } + ++l_code_block; + } + opj_free(p_precinct->cblks.dec); + p_precinct->cblks.dec = 00; + } } -void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) { - int compno, resno, bandno, precno, cblkno; - int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */ - int matrice[10][10][3]; - int i, j, k; +void tcd_free_tile(opj_tcd_t *p_tcd) +{ + OPJ_UINT32 compno, resno, bandno, precno; + opj_tcd_tile_t *l_tile = 00; + opj_tcd_tilecomp_t *l_tile_comp = 00; + opj_tcd_resolution_t *l_res = 00; + opj_tcd_band_t *l_band = 00; + opj_tcd_precinct_t *l_precinct = 00; + OPJ_UINT32 l_nb_resolutions, l_nb_precincts; + void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00; + + if + (! p_tcd) + { + return; + } + if + (! p_tcd->tcd_image) + { + return; + } + if + (p_tcd->m_is_decoder) + { + l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate; + } + else + { + l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate; + } + + + l_tile = p_tcd->tcd_image->tiles; + if + (! l_tile) + { + return; + } + l_tile_comp = l_tile->comps; + + for + (compno = 0; compno < l_tile->numcomps; ++compno) + { + l_res = l_tile_comp->resolutions; + if + (l_res) + { + l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t); + for + (resno = 0; resno < l_nb_resolutions; ++resno) + { + l_band = l_res->bands; + for + (bandno = 0; bandno < 3; ++bandno) + { + l_precinct = l_band->precincts; + if + (l_precinct) + { + l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t); + for + (precno = 0; precno < l_nb_precincts; ++precno) + { + tgt_destroy(l_precinct->incltree); + l_precinct->incltree = 00; + tgt_destroy(l_precinct->imsbtree); + l_precinct->imsbtree = 00; + (*l_tcd_code_block_deallocate) (l_precinct); + ++l_precinct; + } + opj_free(l_band->precincts); + l_band->precincts = 00; + } + ++l_band; + } /* for (resno */ + ++l_res; + } + opj_free(l_tile_comp->resolutions); + l_tile_comp->resolutions = 00; + } + if + (l_tile_comp->data) + { + opj_aligned_free(l_tile_comp->data); + l_tile_comp->data = 00; + } + ++l_tile_comp; + } + opj_free(l_tile->comps); + l_tile->comps = 00; + opj_free(p_tcd->tcd_image->tiles); + p_tcd->tcd_image->tiles = 00; +} + +bool tcd_init( + opj_tcd_t *p_tcd, + opj_image_t * p_image, + opj_cp_t * p_cp + ) +{ + OPJ_UINT32 l_tile_comp_size; + + p_tcd->image = p_image; + p_tcd->cp = p_cp; + p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t)); + + if + (! p_tcd->tcd_image->tiles) + { + return false; + } + memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t)); + + l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_t); + p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size); + if + (! p_tcd->tcd_image->tiles->comps ) + { + return false; + } + memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size); + p_tcd->tcd_image->tiles->numcomps = p_image->numcomps; + p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos; + return true; +} + +void tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) { + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */ + OPJ_INT32 matrice[10][10][3]; + OPJ_UINT32 i, j, k; opj_cp_t *cp = tcd->cp; - opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; + opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles; opj_tcp_t *tcd_tcp = tcd->tcp; - /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */ - for (compno = 0; compno < tcd_tile->numcomps; compno++) { opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno]; for (i = 0; i < tcd_tcp->numlayers; i++) { for (j = 0; j < tilec->numresolutions; j++) { for (k = 0; k < 3; k++) { matrice[i][j][k] = - (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] - * (float) (tcd->image->comps[compno].prec / 16.0)); + (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k] + * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0)); } } } @@ -864,8 +781,8 @@ void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) { for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno]; opj_tcd_layer_t *layer = &cblk->layers[layno]; - int n; - int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ + OPJ_UINT32 n; + OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */ /* Correction of the matrix of coefficient to include the IMSB information */ if (layno == 0) { value = matrice[layno][resno][bandno]; @@ -921,16 +838,17 @@ void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) { } void tcd_rateallocate_fixed(opj_tcd_t *tcd) { - int layno; + OPJ_UINT32 layno; for (layno = 0; layno < tcd->tcp->numlayers; layno++) { tcd_makelayer_fixed(tcd, layno, 1); } } -void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) { - int compno, resno, bandno, precno, cblkno, passno; +void tcd_makelayer(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_FLOAT64 thresh, OPJ_UINT32 final) { + OPJ_UINT32 compno, resno, bandno, precno, cblkno; + OPJ_UINT32 passno; - opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; + opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles; tcd_tile->distolayer[layno] = 0; /* fixed_quality */ @@ -946,14 +864,14 @@ void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) { opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno]; opj_tcd_layer_t *layer = &cblk->layers[layno]; - int n; + OPJ_UINT32 n; if (layno == 0) { cblk->numpassesinlayers = 0; } n = cblk->numpassesinlayers; for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) { - int dr; - double dd; + OPJ_INT32 dr; + OPJ_FLOAT64 dd; opj_tcd_pass_t *pass = &cblk->passes[passno]; if (n == 0) { dr = pass->rate; @@ -997,15 +915,16 @@ void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) { } } -bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) { - int compno, resno, bandno, precno, cblkno, passno, layno; - double min, max; - double cumdisto[100]; /* fixed_quality */ - const double K = 1; /* 1.1; fixed_quality */ - double maxSE = 0; +bool tcd_rateallocate(opj_tcd_t *tcd, OPJ_BYTE *dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 len, opj_codestream_info_t *cstr_info) { + OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno; + OPJ_UINT32 passno; + OPJ_FLOAT64 min, max; + OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */ + const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */ + OPJ_FLOAT64 maxSE = 0; opj_cp_t *cp = tcd->cp; - opj_tcd_tile_t *tcd_tile = tcd->tcd_tile; + opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles; opj_tcp_t *tcd_tcp = tcd->tcp; min = DBL_MAX; @@ -1031,8 +950,8 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre for (passno = 0; passno < cblk->totalpasses; passno++) { opj_tcd_pass_t *pass = &cblk->passes[passno]; - int dr; - double dd, rdslope; + OPJ_INT32 dr; + OPJ_FLOAT64 dd, rdslope; if (passno == 0) { dr = pass->rate; dd = pass->distortiondec; @@ -1060,9 +979,9 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre } /* bandno */ } /* resno */ - maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) - * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) - * ((double)(tilec->numpix)); + maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0) + * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0)) + * ((OPJ_FLOAT64)(tilec->numpix)); } /* compno */ /* index file */ @@ -1070,44 +989,53 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno]; tile_info->numpix = tcd_tile->numpix; tile_info->distotile = tcd_tile->distotile; - tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double)); + tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64)); } for (layno = 0; layno < tcd_tcp->numlayers; layno++) { - double lo = min; - double hi = max; - int success = 0; - int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len; - double goodthresh = 0; - double stable_thresh = 0; - int i; - double distotarget; /* fixed_quality */ + OPJ_FLOAT64 lo = min; + OPJ_FLOAT64 hi = max; + bool success = false; + OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len; + OPJ_FLOAT64 goodthresh = 0; + OPJ_FLOAT64 stable_thresh = 0; + OPJ_UINT32 i; + OPJ_FLOAT64 distotarget; /* fixed_quality */ /* fixed_quality */ - distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10)); + distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10)); /* Don't try to find an optimal threshold but rather take everything not included yet, if -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0) -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0) ==> possible to have some lossy layers and the last layer for sure lossless */ - if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) { - opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp); - double thresh = 0; + if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) { + opj_t2_t *t2 = t2_create(tcd->image, cp); + OPJ_FLOAT64 thresh = 0; + if + (t2 == 00) + { + return false; + } - for (i = 0; i < 32; i++) { - int l = 0; - double distoachieved = 0; /* fixed_quality */ + for + (i = 0; i < 32; ++i) + { + OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */ thresh = (lo + hi) / 2; tcd_makelayer(tcd, layno, thresh, 0); - if (cp->fixed_quality) { /* fixed_quality */ - if(cp->cinema){ - l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp); - if (l == -999) { + if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */ + if(cp->m_specific_param.m_enc.m_cinema){ + if + (! t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) + { lo = thresh; continue; - }else{ + } + else + { distoachieved = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; if (distoachieved < distotarget) { @@ -1129,10 +1057,11 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre lo = thresh; } } else { - l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp); - /* TODO: what to do with l ??? seek / tell ??? */ - /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ - if (l == -999) { + if + (! t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) + { + /* TODO: what to do with l ??? seek / tell ??? */ + /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */ lo = thresh; continue; } @@ -1140,11 +1069,11 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre stable_thresh = thresh; } } - success = 1; + success = true; goodthresh = stable_thresh == 0? thresh : stable_thresh; t2_destroy(t2); } else { - success = 1; + success = true; goodthresh = min; } @@ -1164,353 +1093,1029 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre return true; } -int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) { - int compno; - int l, i, numpacks = 0; - opj_tcd_tile_t *tile = NULL; - opj_tcp_t *tcd_tcp = NULL; - opj_cp_t *cp = NULL; +OPJ_UINT32 tcd_get_encoded_tile_size ( + opj_tcd_t *p_tcd + ) +{ + OPJ_UINT32 i,l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + OPJ_UINT32 l_size_comp, l_remaining; - opj_tcp_t *tcp = &tcd->cp->tcps[0]; - opj_tccp_t *tccp = &tcp->tccps[0]; - opj_image_t *image = tcd->image; - - opj_t1_t *t1 = NULL; /* T1 component */ - opj_t2_t *t2 = NULL; /* T2 component */ + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for + (i=0;iimage->numcomps;++i) + { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + if + (l_remaining) + { + ++l_size_comp; + } + if + (l_size_comp == 3) + { + l_size_comp = 4; + } + l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0); + ++l_img_comp; + ++l_tilec; + } + return l_data_size; +} - tcd->tcd_tileno = tileno; - tcd->tcd_tile = tcd->tcd_image->tiles; - tcd->tcp = &tcd->cp->tcps[tileno]; +bool tcd_copy_tile_data ( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_src, + OPJ_UINT32 p_src_length + ) +{ + OPJ_UINT32 i,j,l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + OPJ_UINT32 l_size_comp, l_remaining; + OPJ_UINT32 l_nb_elem; - tile = tcd->tcd_tile; - tcd_tcp = tcd->tcp; - cp = tcd->cp; + l_data_size = tcd_get_encoded_tile_size(p_tcd); + if + (l_data_size != p_src_length) + { + return false; + } + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for + (i=0;iimage->numcomps;++i) + { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0); + if + (l_remaining) + { + ++l_size_comp; + } + if + (l_size_comp == 3) + { + l_size_comp = 4; + } + switch + (l_size_comp) + { + case 1: + { + OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src; + OPJ_INT32 * l_dest_ptr = l_tilec->data; + if + (l_img_comp->sgnd) + { + for + (j=0;jdata; + OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src; + if + (l_img_comp->sgnd) + { + for + (j=0;jdata; + for + (j=0;jcur_tp_num == 0){ - tcd->encoding_time = opj_clock(); /* time needed to encode a tile */ - /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ - if(cstr_info) { - opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */ - for (i = 0; i < tilec_idx->numresolutions; i++) { - opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i]; - - cstr_info->tile[tileno].pw[i] = res_idx->pw; - cstr_info->tile[tileno].ph[i] = res_idx->ph; - - numpacks += res_idx->pw * res_idx->ph; - - cstr_info->tile[tileno].pdx[i] = tccp->prcw[i]; - cstr_info->tile[tileno].pdy[i] = tccp->prch[i]; +bool tcd_update_tile_data ( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest, + OPJ_UINT32 p_dest_length + ) +{ + OPJ_UINT32 i,j,k,l_data_size = 0; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_tilecomp_t * l_tilec = 00; + opj_tcd_resolution_t * l_res; + OPJ_UINT32 l_size_comp, l_remaining; + OPJ_UINT32 l_stride, l_width,l_height; + + l_data_size = tcd_get_decoded_tile_size(p_tcd); + if + (l_data_size > p_dest_length) + { + return false; + } + + l_tilec = p_tcd->tcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for + (i=0;iimage->numcomps;++i) + { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + l_res = l_tilec->resolutions + l_img_comp->resno_decoded; + l_width = (l_res->x1 - l_res->x0); + l_height = (l_res->y1 - l_res->y0); + l_stride = (l_tilec->x1 - l_tilec->x0) - l_width; + if + (l_remaining) + { + ++l_size_comp; + } + if + (l_size_comp == 3) + { + l_size_comp = 4; + } + switch + (l_size_comp) + { + case 1: + { + OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest; + const OPJ_INT32 * l_src_ptr = l_tilec->data; + if + (l_img_comp->sgnd) + { + for + (j=0;jdata; + OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest; + if + (l_img_comp->sgnd) + { + for + (j=0;jdata; + for + (j=0;jtcd_image->tiles->comps; + l_img_comp = p_tcd->image->comps; + for + (i=0;iimage->numcomps;++i) + { + l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/ + l_remaining = l_img_comp->prec & 7; /* (%8) */ + if + (l_remaining) + { + ++l_size_comp; + } + if + (l_size_comp == 3) + { + l_size_comp = 4; + } + l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1; + l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0); + ++l_img_comp; + ++l_tile_comp; + } + return l_data_size; +} + +bool tcd_dc_level_shift_encode ( + opj_tcd_t *p_tcd + ) +{ + OPJ_UINT32 compno; + opj_tcd_tilecomp_t * l_tile_comp = 00; + opj_tccp_t * l_tccp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcp_t * l_tcp = 00; + opj_tcd_tile_t * l_tile; + OPJ_UINT32 l_nb_elem,i; + OPJ_INT32 * l_current_ptr; + + l_tile = p_tcd->tcd_image->tiles; + l_tile_comp = l_tile->comps; + l_tcp = p_tcd->tcp; + l_tccp = p_tcd->tcp->tccps; + l_img_comp = p_tcd->image->comps; + for + (compno = 0; compno < l_tile->numcomps; compno++) + { + l_current_ptr = l_tile_comp->data; + l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); + if + (l_tccp->qmfbid == 1) + { + for + (i = 0; i < l_nb_elem; ++i) + { + *l_current_ptr -= l_tccp->m_dc_level_shift ; + ++l_current_ptr; } - cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t)); + } + else + { + for + (i = 0; i < l_nb_elem; ++i) + { + *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ; + ++l_current_ptr; + } + } + ++l_img_comp; + ++l_tccp; + ++l_tile_comp; + } + return true; +} + +bool tcd_mct_encode ( + opj_tcd_t *p_tcd + ) +{ + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; + OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); + OPJ_UINT32 i; + OPJ_BYTE ** l_data = 00; + opj_tcp_t * l_tcp = p_tcd->tcp; + if + (!p_tcd->tcp->mct) + { + return true; + } + + if + (p_tcd->tcp->mct == 2) + { + if + (! p_tcd->tcp->m_mct_coding_matrix) + { + return true; + } + l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); + if + (! l_data) + { + return false; + } + for + (i=0;inumcomps;++i) + { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data; + ++l_tile_comp; + } + if + (! mct_encode_custom(// MCT data + (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix, + // size of components + samples, + // components + l_data, + // nb of components (i.e. size of pData) + l_tile->numcomps, + // tells if the data is signed + p_tcd->image->comps->sgnd) + ) + { + opj_free(l_data); + return false; + } + opj_free(l_data); + } + else if (l_tcp->tccps->qmfbid == 0) + { + mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); + } + else + { + mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples); + } + return true; +} + +bool tcd_dwt_encode ( + opj_tcd_t *p_tcd + ) +{ + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + OPJ_UINT32 compno; + + + for + (compno = 0; compno < l_tile->numcomps; ++compno) + { + if + (l_tccp->qmfbid == 1) + { + if + (! dwt_encode(l_tile_comp)) + { + return false; + } + } + else if + (l_tccp->qmfbid == 0) + { + if + (! dwt_encode_real(l_tile_comp)) + { + return false; + } + } + ++l_tile_comp; + ++l_tccp; + } + return true; +} + +bool tcd_t1_encode ( + opj_tcd_t *p_tcd + ) +{ + opj_t1_t * l_t1; + const OPJ_FLOAT64 * l_mct_norms; + opj_tcp_t * l_tcp = p_tcd->tcp; + + l_t1 = t1_create(); + if + (l_t1 == 00) + { + return false; + } + if + (l_tcp->mct == 1) + { + // irreversible encoding + if + (l_tcp->tccps->qmfbid == 0) + { + l_mct_norms = get_mct_norms_real(); + } + else + { + l_mct_norms = get_mct_norms(); + } + } + else + { + l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms); + } + + if + (! t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) + { + t1_destroy(l_t1); + return false; + } + t1_destroy(l_t1); + return true; +} + +bool tcd_t2_encode ( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info + ) +{ + opj_t2_t * l_t2; + + l_t2 = t2_create(p_tcd->image, p_tcd->cp); + if + (l_t2 == 00) + { + return false; + } + + if + (! t2_encode_packets( + l_t2, + p_tcd->tcd_tileno, + p_tcd->tcd_image->tiles, + p_tcd->tcp->numlayers, + p_dest_data, + p_data_written, + p_max_dest_size, + p_cstr_info, + p_tcd->tp_num, + p_tcd->tp_pos, + p_tcd->cur_pino, + FINAL_PASS)) + { + t2_destroy(l_t2); + return false; + } + t2_destroy(l_t2); + + /*---------------CLEAN-------------------*/ + return true; +} + +bool tcd_rate_allocate_encode( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest_data, + OPJ_UINT32 p_max_dest_size, + opj_codestream_info_t *p_cstr_info + ) +{ + opj_cp_t * l_cp = p_tcd->cp; + OPJ_UINT32 l_nb_written = 0; + + if + (p_cstr_info) + { + p_cstr_info->index_write = 0; + } + if + (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) + { /* fixed_quality */ + /* Normal Rate/distortion allocation */ + if + (! tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) + { + return false; + } + } + else + { + /* Fixed layer allocation */ + tcd_rateallocate_fixed(p_tcd); + } + return true; +} + +bool tcd_t2_decode ( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_src_data, + OPJ_UINT32 * p_data_read, + OPJ_UINT32 p_max_src_size, + opj_codestream_info_t *p_cstr_info + ) +{ + opj_t2_t * l_t2; + + l_t2 = t2_create(p_tcd->image, p_tcd->cp); + if + (l_t2 == 00) + { + return false; + } + + if + (! t2_decode_packets( + l_t2, + p_tcd->tcd_tileno, + p_tcd->tcd_image->tiles, + p_src_data, + p_data_read, + p_max_src_size, + p_cstr_info)) + { + t2_destroy(l_t2); + return false; + } + t2_destroy(l_t2); + + /*---------------CLEAN-------------------*/ + return true; +} + +bool tcd_t1_decode ( + opj_tcd_t *p_tcd + ) +{ + OPJ_UINT32 compno; + opj_t1_t * l_t1; + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + + + l_t1 = t1_create(); + if + (l_t1 == 00) + { + return false; + } + for + (compno = 0; compno < l_tile->numcomps; ++compno) + { + /* The +3 is headroom required by the vectorized DWT */ + t1_decode_cblks(l_t1, l_tile_comp, l_tccp); + ++l_tile_comp; + ++l_tccp; + } + t1_destroy(l_t1); + return true; +} + +bool tcd_dwt_decode ( + opj_tcd_t *p_tcd + ) +{ + OPJ_UINT32 compno; + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps; + opj_tccp_t * l_tccp = p_tcd->tcp->tccps; + opj_image_comp_t * l_img_comp = p_tcd->image->comps; + + for + (compno = 0; compno < l_tile->numcomps; compno++) + { + /* + if (tcd->cp->reduce != 0) { + tcd->image->comps[compno].resno_decoded = + tile->comps[compno].numresolutions - tcd->cp->reduce - 1; + if (tcd->image->comps[compno].resno_decoded < 0) + { + return false; + } + } + numres2decode = tcd->image->comps[compno].resno_decoded + 1; + if(numres2decode > 0){ + */ + if + (l_tccp->qmfbid == 1) + { + if + (! dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) + { + return false; + } + } + else + { + if + (! dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) + { + return false; + } + } + ++l_tile_comp; + ++l_img_comp; + ++l_tccp; + } + return true; +} +bool tcd_mct_decode ( + opj_tcd_t *p_tcd + ) +{ + opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles; + opj_tcp_t * l_tcp = p_tcd->tcp; + opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps; + OPJ_UINT32 l_samples,i; + + if + (! l_tcp->mct) + { + return true; + } + l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0); + if + (l_tcp->mct == 2) + { + OPJ_BYTE ** l_data; + if + (! l_tcp->m_mct_decoding_matrix) + { + return true; + } + l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*)); + if + (! l_data) + { + return false; + } + for + (i=0;inumcomps;++i) + { + l_data[i] = (OPJ_BYTE*) l_tile_comp->data; + ++l_tile_comp; + } + if + (! mct_decode_custom( // MCT data + (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix, + // size of components + l_samples, + // components + l_data, + // nb of components (i.e. size of pData) + l_tile->numcomps, + // tells if the data is signed + p_tcd->image->comps->sgnd)) + { + opj_free(l_data); + return false; + } + opj_free(l_data); + } + else + { + if + (l_tcp->tccps->qmfbid == 1) + { + mct_decode( + l_tile->comps[0].data, + l_tile->comps[1].data, + l_tile->comps[2].data, + l_samples); + } + else + { + mct_decode_real( + (float*)l_tile->comps[0].data, + (float*)l_tile->comps[1].data, + (float*)l_tile->comps[2].data, + l_samples); + } + } + return true; +} + +bool tcd_dc_level_shift_decode ( + opj_tcd_t *p_tcd + ) +{ + OPJ_UINT32 compno; + opj_tcd_tilecomp_t * l_tile_comp = 00; + opj_tccp_t * l_tccp = 00; + opj_image_comp_t * l_img_comp = 00; + opj_tcd_resolution_t* l_res = 00; + opj_tcp_t * l_tcp = 00; + opj_tcd_tile_t * l_tile; + OPJ_UINT32 l_width,l_height,i,j; + OPJ_INT32 * l_current_ptr; + OPJ_INT32 l_min, l_max; + OPJ_UINT32 l_stride; + + l_tile = p_tcd->tcd_image->tiles; + l_tile_comp = l_tile->comps; + l_tcp = p_tcd->tcp; + l_tccp = p_tcd->tcp->tccps; + l_img_comp = p_tcd->image->comps; + + for + (compno = 0; compno < l_tile->numcomps; compno++) + { + l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded; + l_width = (l_res->x1 - l_res->x0); + l_height = (l_res->y1 - l_res->y0); + l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width; + if + (l_img_comp->sgnd) + { + l_min = -(1 << (l_img_comp->prec - 1)); + l_max = (1 << (l_img_comp->prec - 1)) - 1; + } + else + { + l_min = 0; + l_max = (1 << l_img_comp->prec) - 1; + } + l_current_ptr = l_tile_comp->data; + if + (l_tccp->qmfbid == 1) + { + for + (j=0;jm_dc_level_shift, l_min, l_max); + ++l_current_ptr; + } + l_current_ptr += l_stride; + } + } + else + { + for + (j=0;jm_dc_level_shift, l_min, l_max); ; + ++l_current_ptr; + } + l_current_ptr += l_stride; + } + } + ++l_img_comp; + ++l_tccp; + ++l_tile_comp; + } + return true; +} + +bool tcd_encode_tile( + opj_tcd_t *p_tcd, + OPJ_UINT32 p_tile_no, + OPJ_BYTE *p_dest, + OPJ_UINT32 * p_data_written, + OPJ_UINT32 p_max_length, + opj_codestream_info_t *p_cstr_info) +{ + + if + (p_tcd->cur_tp_num == 0) + { + p_tcd->tcd_tileno = p_tile_no; + p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no]; + /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */ + if(p_cstr_info) + { + OPJ_UINT32 l_num_packs = 0; + OPJ_UINT32 i; + opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */ + opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */ + for (i = 0; i < l_tilec_idx->numresolutions; i++) { + opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i]; + + p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw; + p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph; + + l_num_packs += l_res_idx->pw * l_res_idx->ph; + p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i]; + p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i]; + } + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(p_cstr_info->numcomps * p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t)); } /* << INDEX */ - + _ProfStart(PGROUP_DC_SHIFT); /*---------------TILE-------------------*/ - - for (compno = 0; compno < tile->numcomps; compno++) { - int x, y; - - int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1); - int offset_x = int_ceildiv(image->x0, image->comps[compno].dx); - int offset_y = int_ceildiv(image->y0, image->comps[compno].dy); - - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - int tw = tilec->x1 - tilec->x0; - int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx); - - /* extract tile data */ - - if (tcd_tcp->tccps[compno].qmfbid == 1) { - for (y = tilec->y0; y < tilec->y1; y++) { - /* start of the src tile scanline */ - int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w]; - /* start of the dst tile scanline */ - int *tile_data = &tilec->data[(y - tilec->y0) * tw]; - for (x = tilec->x0; x < tilec->x1; x++) { - *tile_data++ = *data++ - adjust; - } - } - } else if (tcd_tcp->tccps[compno].qmfbid == 0) { - for (y = tilec->y0; y < tilec->y1; y++) { - /* start of the src tile scanline */ - int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w]; - /* start of the dst tile scanline */ - int *tile_data = &tilec->data[(y - tilec->y0) * tw]; - for (x = tilec->x0; x < tilec->x1; x++) { - *tile_data++ = (*data++ - adjust) << 11; - } - - } - } + if + (! tcd_dc_level_shift_encode(p_tcd)) + { + return false; } + _ProfStop(PGROUP_DC_SHIFT); - /*----------------MCT-------------------*/ - if (tcd_tcp->mct) { - int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0); - if (tcd_tcp->tccps[0].qmfbid == 0) { - mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples); - } else { - mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples); - } + _ProfStart(PGROUP_MCT); + if + (! tcd_mct_encode(p_tcd)) + { + return false; } - - /*----------------DWT---------------------*/ - - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - if (tcd_tcp->tccps[compno].qmfbid == 1) { - dwt_encode(tilec); - } else if (tcd_tcp->tccps[compno].qmfbid == 0) { - dwt_encode_real(tilec); - } + _ProfStop(PGROUP_MCT); + + _ProfStart(PGROUP_DWT); + if + (! tcd_dwt_encode(p_tcd)) + { + return false; } - - /*------------------TIER1-----------------*/ - t1 = t1_create(tcd->cinfo); - t1_encode_cblks(t1, tile, tcd_tcp); - t1_destroy(t1); - - /*-----------RATE-ALLOCATE------------------*/ - - /* INDEX */ - if(cstr_info) { - cstr_info->index_write = 0; + _ProfStop(PGROUP_DWT); + + _ProfStart(PGROUP_T1); + if + (! tcd_t1_encode(p_tcd)) + { + return false; } - if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */ - /* Normal Rate/distortion allocation */ - tcd_rateallocate(tcd, dest, len, cstr_info); - } else { - /* Fixed layer allocation */ - tcd_rateallocate_fixed(tcd); + _ProfStop(PGROUP_T1); + + _ProfStart(PGROUP_RATE); + if + (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) + { + return false; } + _ProfStop(PGROUP_RATE); + } /*--------------TIER2------------------*/ /* INDEX */ - if(cstr_info) { - cstr_info->index_write = 1; + if + (p_cstr_info) + { + p_cstr_info->index_write = 1; } - - t2 = t2_create(tcd->cinfo, image, cp); - l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp); - t2_destroy(t2); - + _ProfStart(PGROUP_T2); + if + (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) + { + return false; + } + _ProfStop(PGROUP_T2); /*---------------CLEAN-------------------*/ - - - if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){ - tcd->encoding_time = opj_clock() - tcd->encoding_time; - opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time); - - /* cleaning memory */ - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - opj_aligned_free(tilec->data); - } - } - - return l; + return true; } -bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) { - int l; - int compno; - int eof = 0; - double tile_time, t1_time, dwt_time; - opj_tcd_tile_t *tile = NULL; - - opj_t1_t *t1 = NULL; /* T1 component */ - opj_t2_t *t2 = NULL; /* T2 component */ +bool tcd_decode_tile( + opj_tcd_t *p_tcd, + OPJ_BYTE *p_src, + OPJ_UINT32 p_max_length, + OPJ_UINT32 p_tile_no, + opj_codestream_info_t *p_cstr_info) +{ + OPJ_UINT32 l_data_read; + p_tcd->tcd_tileno = p_tile_no; + p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]); - tcd->tcd_tileno = tileno; - tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]); - tcd->tcp = &(tcd->cp->tcps[tileno]); - tile = tcd->tcd_tile; - - tile_time = opj_clock(); /* time needed to decode a tile */ - opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th); - /* INDEX >> */ - if(cstr_info) { - int resno, compno, numprec = 0; - for (compno = 0; compno < cstr_info->numcomps; compno++) { - opj_tcp_t *tcp = &tcd->cp->tcps[0]; + if(p_cstr_info) { + OPJ_UINT32 resno, compno, numprec = 0; + for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) { + opj_tcp_t *tcp = &p_tcd->cp->tcps[0]; opj_tccp_t *tccp = &tcp->tccps[compno]; - opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno]; + opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno]; for (resno = 0; resno < tilec_idx->numresolutions; resno++) { opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno]; - cstr_info->tile[tileno].pw[resno] = res_idx->pw; - cstr_info->tile[tileno].ph[resno] = res_idx->ph; + p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw; + p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph; numprec += res_idx->pw * res_idx->ph; - if (tccp->csty & J2K_CP_CSTY_PRT) { - cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno]; - cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno]; - } - else { - cstr_info->tile[tileno].pdx[resno] = 15; - cstr_info->tile[tileno].pdx[resno] = 15; - } + p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno]; + p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno]; } } - cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t)); - cstr_info->packno = 0; + p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t)); + p_cstr_info->packno = 0; } /* << INDEX */ /*--------------TIER2------------------*/ - - t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp); - l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info); - t2_destroy(t2); - - if (l == -999) { - eof = 1; - opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n"); + _ProfStart(PGROUP_T2); + l_data_read = 0; + if + (! tcd_t2_decode(p_tcd,p_src,&l_data_read,p_max_length,p_cstr_info)) + { + return false; } + _ProfStop(PGROUP_T2); /*------------------TIER1-----------------*/ - t1_time = opj_clock(); /* time needed to decode a tile */ - t1 = t1_create(tcd->cinfo); - for (compno = 0; compno < tile->numcomps; ++compno) { - opj_tcd_tilecomp_t* tilec = &tile->comps[compno]; - /* The +3 is headroom required by the vectorized DWT */ - tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int)); - t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]); + _ProfStart(PGROUP_T1); + if + (! tcd_t1_decode(p_tcd)) + { + return false; } - t1_destroy(t1); - t1_time = opj_clock() - t1_time; - opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time); + _ProfStop(PGROUP_T1); /*----------------DWT---------------------*/ - dwt_time = opj_clock(); /* time needed to decode a tile */ - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - int numres2decode; - - if (tcd->cp->reduce != 0) { - tcd->image->comps[compno].resno_decoded = - tile->comps[compno].numresolutions - tcd->cp->reduce - 1; - if (tcd->image->comps[compno].resno_decoded < 0) { - opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number " - " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions); - return false; - } - } - - numres2decode = tcd->image->comps[compno].resno_decoded + 1; - if(numres2decode > 0){ - if (tcd->tcp->tccps[compno].qmfbid == 1) { - dwt_decode(tilec, numres2decode); - } else { - dwt_decode_real(tilec, numres2decode); - } - } - } - dwt_time = opj_clock() - dwt_time; - opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time); - - /*----------------MCT-------------------*/ - - if (tcd->tcp->mct) { - int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0); - if (tcd->tcp->tccps[0].qmfbid == 1) { - mct_decode( - tile->comps[0].data, - tile->comps[1].data, - tile->comps[2].data, - n); - } else { - mct_decode_real( - (float*)tile->comps[0].data, - (float*)tile->comps[1].data, - (float*)tile->comps[2].data, - n); - } - } - - /*---------------TILE-------------------*/ - - for (compno = 0; compno < tile->numcomps; ++compno) { - opj_tcd_tilecomp_t* tilec = &tile->comps[compno]; - opj_image_comp_t* imagec = &tcd->image->comps[compno]; - opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded]; - int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1); - int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0; - int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1; - - int tw = tilec->x1 - tilec->x0; - int w = imagec->w; - - int offset_x = int_ceildivpow2(imagec->x0, imagec->factor); - int offset_y = int_ceildivpow2(imagec->y0, imagec->factor); - - int i, j; - if(!imagec->data){ - imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int)); - } - if(tcd->tcp->tccps[compno].qmfbid == 1) { - for(j = res->y0; j < res->y1; ++j) { - for(i = res->x0; i < res->x1; ++i) { - int v = tilec->data[i - res->x0 + (j - res->y0) * tw]; - v += adjust; - imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max); - } - } - }else{ - for(j = res->y0; j < res->y1; ++j) { - for(i = res->x0; i < res->x1; ++i) { - float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw]; - int v = lrintf(tmp); - v += adjust; - imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max); - } - } - } - opj_aligned_free(tilec->data); - } - - tile_time = opj_clock() - tile_time; /* time needed to decode a tile */ - opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time); - - if (eof) { + _ProfStart(PGROUP_DWT); + if + (! tcd_dwt_decode(p_tcd)) + { return false; } + _ProfStop(PGROUP_DWT); + + /*----------------MCT-------------------*/ + _ProfStart(PGROUP_MCT); + if + (! tcd_mct_decode(p_tcd)) + { + return false; + } + _ProfStop(PGROUP_MCT); + + _ProfStart(PGROUP_DC_SHIFT); + if + (! tcd_dc_level_shift_decode(p_tcd)) + { + return false; + } + _ProfStop(PGROUP_DC_SHIFT); + + /*---------------TILE-------------------*/ return true; } - -void tcd_free_decode(opj_tcd_t *tcd) { - opj_tcd_image_t *tcd_image = tcd->tcd_image; - opj_free(tcd_image->tiles); -} - -void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) { - int compno,resno,bandno,precno; - - opj_tcd_image_t *tcd_image = tcd->tcd_image; - - opj_tcd_tile_t *tile = &tcd_image->tiles[tileno]; - for (compno = 0; compno < tile->numcomps; compno++) { - opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; - for (resno = 0; resno < tilec->numresolutions; resno++) { - opj_tcd_resolution_t *res = &tilec->resolutions[resno]; - for (bandno = 0; bandno < res->numbands; bandno++) { - opj_tcd_band_t *band = &res->bands[bandno]; - for (precno = 0; precno < res->ph * res->pw; precno++) { - opj_tcd_precinct_t *prec = &band->precincts[precno]; - if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree); - if (prec->incltree != NULL) tgt_destroy(prec->incltree); - } - opj_free(band->precincts); - } - } - opj_free(tilec->resolutions); - } - opj_free(tile->comps); -} - - - diff --git a/libopenjpeg/tcd.h b/libopenjpeg/tcd.h index f0ac5619..b18b682b 100644 --- a/libopenjpeg/tcd.h +++ b/libopenjpeg/tcd.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,167 +38,180 @@ The functions in TCD.C have for goal to encode or decode each tile independently from each other. The functions in TCD.C are used by some function in J2K.C. */ - +#include "openjpeg.h" /** @defgroup TCD TCD - Implementation of a tile coder/decoder */ /*@{*/ +struct opj_common_struct; +struct opj_codestream_info; +struct opj_image; +struct opj_tcp; +struct opj_cp; +struct opj_tgt_tree; /** FIXME: documentation */ typedef struct opj_tcd_seg { - unsigned char** data; - int dataindex; - int numpasses; - int len; - int maxpasses; - int numnewpasses; - int newlen; + OPJ_BYTE ** data; + OPJ_UINT32 dataindex; + OPJ_UINT32 numpasses; + OPJ_UINT32 real_num_passes; + OPJ_UINT32 len; + OPJ_UINT32 maxpasses; + OPJ_UINT32 numnewpasses; + OPJ_UINT32 newlen; } opj_tcd_seg_t; /** FIXME: documentation */ typedef struct opj_tcd_pass { - int rate; - double distortiondec; - int term, len; + OPJ_UINT32 rate; + OPJ_FLOAT64 distortiondec; + OPJ_UINT32 len; + OPJ_UINT32 term : 1; } opj_tcd_pass_t; /** FIXME: documentation */ typedef struct opj_tcd_layer { - int numpasses; /* Number of passes in the layer */ - int len; /* len of information */ - double disto; /* add for index (Cfr. Marcela) */ - unsigned char *data; /* data */ + OPJ_UINT32 numpasses; /* Number of passes in the layer */ + OPJ_UINT32 len; /* len of information */ + OPJ_FLOAT64 disto; /* add for index (Cfr. Marcela) */ + OPJ_BYTE *data; /* data */ } opj_tcd_layer_t; /** FIXME: documentation */ typedef struct opj_tcd_cblk_enc { - unsigned char* data; /* Data */ + OPJ_BYTE* data; /* Data */ opj_tcd_layer_t* layers; /* layer information */ opj_tcd_pass_t* passes; /* information about the passes */ - int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ - int numbps; - int numlenbits; - int numpasses; /* number of pass already done for the code-blocks */ - int numpassesinlayers; /* number of passes in the layer */ - int totalpasses; /* total number of passes */ + OPJ_INT32 x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 numbps; + OPJ_UINT32 numlenbits; + OPJ_UINT32 numpasses; /* number of pass already done for the code-blocks */ + OPJ_UINT32 numpassesinlayers; /* number of passes in the layer */ + OPJ_UINT32 totalpasses; /* total number of passes */ } opj_tcd_cblk_enc_t; typedef struct opj_tcd_cblk_dec { - unsigned char* data; /* Data */ + OPJ_BYTE * data; /* Data */ opj_tcd_seg_t* segs; /* segments informations */ - int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ - int numbps; - int numlenbits; - int len; /* length */ - int numnewpasses; /* number of pass added to the code-blocks */ - int numsegs; /* number of segments */ + OPJ_INT32 x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 numbps; + OPJ_UINT32 numlenbits; + OPJ_UINT32 len; /* length */ + OPJ_UINT32 numnewpasses; /* number of pass added to the code-blocks */ + OPJ_UINT32 numsegs; /* number of segments */ + OPJ_UINT32 real_num_segs; + OPJ_UINT32 m_current_max_segs; } opj_tcd_cblk_dec_t; /** FIXME: documentation */ typedef struct opj_tcd_precinct { - int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */ - int cw, ch; /* number of precinct in width and heigth */ + OPJ_INT32 x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 cw, ch; /* number of precinct in width and heigth */ union{ /* code-blocks informations */ opj_tcd_cblk_enc_t* enc; opj_tcd_cblk_dec_t* dec; } cblks; - opj_tgt_tree_t *incltree; /* inclusion tree */ - opj_tgt_tree_t *imsbtree; /* IMSB tree */ + OPJ_UINT32 block_size; /* size taken by cblks (in bytes) */ + struct opj_tgt_tree *incltree; /* inclusion tree */ + struct opj_tgt_tree *imsbtree; /* IMSB tree */ } opj_tcd_precinct_t; /** FIXME: documentation */ typedef struct opj_tcd_band { - int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */ - int bandno; + OPJ_INT32 x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 bandno; opj_tcd_precinct_t *precincts; /* precinct information */ - int numbps; - float stepsize; + OPJ_UINT32 precincts_data_size; /* size of data taken by precincts */ + OPJ_INT32 numbps; + OPJ_FLOAT32 stepsize; } opj_tcd_band_t; /** FIXME: documentation */ typedef struct opj_tcd_resolution { - int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */ - int pw, ph; - int numbands; /* number sub-band for the resolution level */ + OPJ_INT32 x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 pw, ph; + OPJ_UINT32 numbands; /* number sub-band for the resolution level */ opj_tcd_band_t bands[3]; /* subband information */ } opj_tcd_resolution_t; /** FIXME: documentation */ -typedef struct opj_tcd_tilecomp { - int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */ - int numresolutions; /* number of resolutions level */ +typedef struct opj_tcd_tilecomp +{ + OPJ_INT32 x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 numresolutions; /* number of resolutions level */ + OPJ_UINT32 minimum_num_resolutions; /* number of resolutions level to decode (at max)*/ opj_tcd_resolution_t *resolutions; /* resolutions information */ - int *data; /* data of the component */ - int numpix; /* add fixed_quality */ + OPJ_UINT32 resolutions_size; /* size of data for resolutions (in bytes) */ + OPJ_INT32 *data; /* data of the component */ + OPJ_UINT32 data_size; /* size of the data of the component */ + OPJ_INT32 numpix; /* add fixed_quality */ } opj_tcd_tilecomp_t; /** FIXME: documentation */ typedef struct opj_tcd_tile { - int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */ - int numcomps; /* number of components in tile */ + OPJ_INT32 x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */ + OPJ_UINT32 numcomps; /* number of components in tile */ opj_tcd_tilecomp_t *comps; /* Components information */ - int numpix; /* add fixed_quality */ - double distotile; /* add fixed_quality */ - double distolayer[100]; /* add fixed_quality */ + OPJ_INT32 numpix; /* add fixed_quality */ + OPJ_FLOAT64 distotile; /* add fixed_quality */ + OPJ_FLOAT64 distolayer[100]; /* add fixed_quality */ /** packet number */ - int packno; + OPJ_UINT32 packno; } opj_tcd_tile_t; /** FIXME: documentation */ -typedef struct opj_tcd_image { - int tw, th; /* number of tiles in width and heigth */ +typedef struct opj_tcd_image +{ opj_tcd_tile_t *tiles; /* Tiles information */ -} opj_tcd_image_t; +} +opj_tcd_image_t; /** Tile coder/decoder */ -typedef struct opj_tcd { +typedef struct opj_tcd +{ /** Position of the tilepart flag in Progression order*/ - int tp_pos; + OPJ_INT32 tp_pos; /** Tile part number*/ - int tp_num; + OPJ_UINT32 tp_num; /** Current tile part number*/ - int cur_tp_num; + OPJ_UINT32 cur_tp_num; /** Total number of tileparts of the current tile*/ - int cur_totnum_tp; + OPJ_UINT32 cur_totnum_tp; /** Current Packet iterator number */ - int cur_pino; - /** codec context */ - opj_common_ptr cinfo; - + OPJ_UINT32 cur_pino; /** info on each image tile */ - opj_tcd_image_t *tcd_image; + struct opj_tcd_image *tcd_image; /** image */ - opj_image_t *image; + struct opj_image *image; /** coding parameters */ - opj_cp_t *cp; - /** pointer to the current encoded/decoded tile */ - opj_tcd_tile_t *tcd_tile; + struct opj_cp *cp; /** coding/decoding parameters common to all tiles */ - opj_tcp_t *tcp; + struct opj_tcp *tcp; /** current encoded/decoded tile */ - int tcd_tileno; - /** Time taken to encode a tile*/ - double encoding_time; + OPJ_UINT32 tcd_tileno; + /** tell if the tcd is a decoder. */ + OPJ_UINT32 m_is_decoder : 1; } opj_tcd_t; /** @name Exported functions */ @@ -207,61 +221,86 @@ typedef struct opj_tcd { /** Dump the content of a tcd structure */ -void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img); +//void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t *img); /** Create a new TCD handle @param cinfo Codec context info @return Returns a new TCD handle if successful returns NULL otherwise */ -opj_tcd_t* tcd_create(opj_common_ptr cinfo); +opj_tcd_t* tcd_create(bool p_is_decoder); + /** Destroy a previously created TCD handle @param tcd TCD handle to destroy */ void tcd_destroy(opj_tcd_t *tcd); + + + + /** -Initialize the tile coder (allocate the memory) -@param tcd TCD handle -@param image Raw image -@param cp Coding parameters -@param curtileno Number that identifies the tile that will be encoded + * Initialize the tile coder and may reuse some meory. + * @param p_tcd TCD handle. + * @param p_image raw image. + * @param p_cp coding parameters. + * @param p_tile_no current tile index to encode. + * + * @return true if the encoding values could be set (false otherwise). */ -void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno); +bool tcd_init( + opj_tcd_t *p_tcd, + struct opj_image * p_image, + struct opj_cp * p_cp + ); + + /** -Free the memory allocated for encoding -@param tcd TCD handle -*/ -void tcd_free_encode(opj_tcd_t *tcd); + * Allocates memory for decoding a specific tile. + * + * @param p_tcd the tile decoder. + * @param p_image the image to decode. + * @param p_cp the decoding parameters. + * @param p_tile_no the index of the tile received in sequence. This not necesseraly lead to the + * tile at index p_tile_no. + * @param p_cstr_info codestream info (if any). + * + * @return true if the remaining data is sufficient.s + */ +bool tcd_init_decode_tile( + opj_tcd_t *p_tcd, + OPJ_UINT32 p_tile_no + ); /** -Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode) -@param tcd TCD handle -@param image Raw image -@param cp Coding parameters -@param curtileno Number that identifies the tile that will be encoded + * Initialize the tile coder and may reuse some meory. + * @param p_tcd TCD handle. + * @param p_image raw image. + * @param p_cp coding parameters. + * @param p_tile_no current tile index to encode. + * + * @return true if the encoding values could be set (false otherwise). */ -void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno); -/** -Initialize the tile decoder -@param tcd TCD handle -@param image Raw image -@param cp Coding parameters -*/ -void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp); -void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info); -void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final); +bool tcd_init_encode_tile + ( + opj_tcd_t *p_tcd, + OPJ_UINT32 p_tile_no + ); + +void tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final); void tcd_rateallocate_fixed(opj_tcd_t *tcd); -void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final); -bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info); +void tcd_makelayer(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_FLOAT64 thresh, OPJ_UINT32 final); +bool tcd_rateallocate(opj_tcd_t *tcd, OPJ_BYTE *dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 len, struct opj_codestream_info *cstr_info); /** -Encode a tile from the raw image into a buffer -@param tcd TCD handle -@param tileno Number that identifies one of the tiles to be encoded -@param dest Destination buffer -@param len Length of destination buffer -@param cstr_info Codestream information structure -@return + * Encodes a tile from the raw image into the given buffer. + * @param p_tcd Tile Coder handle + * @param p_tile_no Index of the tile to encode. + * @param p_dest Destination buffer + * @param p_data_written pointer to an int that is incremented by the number of bytes really written on p_dest + * @param p_len Maximum length of the destination buffer + * @param p_cstr_info Codestream information structure + * @return true if the coding is successfull. */ -int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info); +bool tcd_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BYTE *p_dest, OPJ_UINT32 * p_data_written, OPJ_UINT32 p_len, struct opj_codestream_info *p_cstr_info); + /** Decode a tile from a buffer into a raw image @param tcd TCD handle @@ -269,14 +308,34 @@ Decode a tile from a buffer into a raw image @param len Length of source buffer @param tileno Number that identifies one of the tiles to be decoded */ -bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info); -/** -Free the memory allocated for decoding -@param tcd TCD handle -*/ -void tcd_free_decode(opj_tcd_t *tcd); -void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno); +bool tcd_decode_tile(opj_tcd_t *tcd, OPJ_BYTE *src, OPJ_UINT32 len, OPJ_UINT32 tileno, struct opj_codestream_info *cstr_info); +/** + * Copies tile data from the given memory block onto the system. + */ +bool tcd_copy_tile_data ( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_src, + OPJ_UINT32 p_src_length + ); +/** + * Copies tile data from the system onto the given memory block. + */ +bool tcd_update_tile_data ( + opj_tcd_t *p_tcd, + OPJ_BYTE * p_dest, + OPJ_UINT32 p_dest_length + ); +/** + * Gets the maximum tile size that will be taken by the tile once decoded. + */ +OPJ_UINT32 tcd_get_decoded_tile_size ( + opj_tcd_t *p_tcd + ); + +OPJ_UINT32 tcd_get_encoded_tile_size ( + opj_tcd_t *p_tcd + ); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libopenjpeg/tgt.c b/libopenjpeg/tgt.c index a5dbcd3c..70147b38 100644 --- a/libopenjpeg/tgt.c +++ b/libopenjpeg/tgt.c @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,7 +30,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_includes.h" +#include "tgt.h" +#include "bio.h" +#include "opj_malloc.h" /* ========================================================== @@ -37,19 +40,22 @@ ========================================================== */ -opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) { - int nplh[32]; - int nplv[32]; - opj_tgt_node_t *node = NULL; - opj_tgt_node_t *parentnode = NULL; - opj_tgt_node_t *parentnode0 = NULL; - opj_tgt_tree_t *tree = NULL; - int i, j, k; - int numlvls; - int n; +opj_tgt_tree_t *tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv) { + OPJ_INT32 nplh[32]; + OPJ_INT32 nplv[32]; + opj_tgt_node_t *node = 00; + opj_tgt_node_t *l_parent_node = 00; + opj_tgt_node_t *l_parent_node0 = 00; + opj_tgt_tree_t *tree = 00; + OPJ_UINT32 i; + OPJ_INT32 j,k; + OPJ_UINT32 numlvls; + OPJ_UINT32 n; tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t)); - if(!tree) return NULL; + if(!tree) return 00; + memset(tree,0,sizeof(opj_tgt_tree_t)); + tree->numleafsh = numleafsh; tree->numleafsv = numleafsv; @@ -68,65 +74,190 @@ opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) { /* ADD */ if (tree->numnodes == 0) { opj_free(tree); - return NULL; + return 00; } tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t)); if(!tree->nodes) { opj_free(tree); - return NULL; + return 00; } + memset(tree->nodes,0,tree->numnodes * sizeof(opj_tgt_node_t)); + tree->nodes_size = tree->numnodes * sizeof(opj_tgt_node_t); node = tree->nodes; - parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv]; - parentnode0 = parentnode; + l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv]; + l_parent_node0 = l_parent_node; for (i = 0; i < numlvls - 1; ++i) { for (j = 0; j < nplv[i]; ++j) { k = nplh[i]; while (--k >= 0) { - node->parent = parentnode; + node->parent = l_parent_node; ++node; if (--k >= 0) { - node->parent = parentnode; + node->parent = l_parent_node; ++node; } - ++parentnode; + ++l_parent_node; } if ((j & 1) || j == nplv[i] - 1) { - parentnode0 = parentnode; + l_parent_node0 = l_parent_node; } else { - parentnode = parentnode0; - parentnode0 += nplh[i]; + l_parent_node = l_parent_node0; + l_parent_node0 += nplh[i]; } } } node->parent = 0; - tgt_reset(tree); - return tree; } +/** + * Reinitialises a tag-tree from an exixting one. + * + * @param p_tree the tree to reinitialize. + * @param p_num_leafs_h the width of the array of leafs of the tree + * @param p_num_leafs_v the height of the array of leafs of the tree + * @return a new tag-tree if successful, NULL otherwise +*/ +opj_tgt_tree_t *tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, OPJ_UINT32 p_num_leafs_v) +{ + OPJ_INT32 l_nplh[32]; + OPJ_INT32 l_nplv[32]; + opj_tgt_node_t *l_node = 00; + opj_tgt_node_t *l_parent_node = 00; + opj_tgt_node_t *l_parent_node0 = 00; + OPJ_UINT32 i; + OPJ_INT32 j,k; + OPJ_UINT32 l_num_levels; + OPJ_UINT32 n; + OPJ_UINT32 l_node_size; -void tgt_destroy(opj_tgt_tree_t *tree) { - opj_free(tree->nodes); - opj_free(tree); + if + (! p_tree) + { + return 00; + } + if + ((p_tree->numleafsh != p_num_leafs_h) || (p_tree->numleafsv != p_num_leafs_v)) + { + p_tree->numleafsh = p_num_leafs_h; + p_tree->numleafsv = p_num_leafs_v; + + l_num_levels = 0; + l_nplh[0] = p_num_leafs_h; + l_nplv[0] = p_num_leafs_v; + p_tree->numnodes = 0; + do + { + n = l_nplh[l_num_levels] * l_nplv[l_num_levels]; + l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2; + l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2; + p_tree->numnodes += n; + ++l_num_levels; + } + while (n > 1); + + /* ADD */ + if + (p_tree->numnodes == 0) + { + tgt_destroy(p_tree); + return 00; + } + l_node_size = p_tree->numnodes * sizeof(opj_tgt_node_t); + if + (l_node_size > p_tree->nodes_size) + { + p_tree->nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, l_node_size); + if + (! p_tree->nodes) + { + tgt_destroy(p_tree); + return 00; + } + memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0 , l_node_size - p_tree->nodes_size); + p_tree->nodes_size = l_node_size; + } + l_node = p_tree->nodes; + l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv]; + l_parent_node0 = l_parent_node; + + for + (i = 0; i < l_num_levels - 1; ++i) + { + for + (j = 0; j < l_nplv[i]; ++j) + { + k = l_nplh[i]; + while + (--k >= 0) + { + l_node->parent = l_parent_node; + ++l_node; + if (--k >= 0) + { + l_node->parent = l_parent_node; + ++l_node; + } + ++l_parent_node; + } + if ((j & 1) || j == l_nplv[i] - 1) + { + l_parent_node0 = l_parent_node; + } + else + { + l_parent_node = l_parent_node0; + l_parent_node0 += l_nplh[i]; + } + } + } + l_node->parent = 0; + } + tgt_reset(p_tree); + + return p_tree; } -void tgt_reset(opj_tgt_tree_t *tree) { - int i; - - if (NULL == tree) +void tgt_destroy(opj_tgt_tree_t *p_tree) +{ + if + (! p_tree) + { return; - - for (i = 0; i < tree->numnodes; i++) { - tree->nodes[i].value = 999; - tree->nodes[i].low = 0; - tree->nodes[i].known = 0; + } + if + (p_tree->nodes) + { + opj_free(p_tree->nodes); + p_tree->nodes = 00; + } + opj_free(p_tree); +} + +void tgt_reset(opj_tgt_tree_t *p_tree) { + OPJ_UINT32 i; + opj_tgt_node_t * l_current_node = 00;; + + if + (! p_tree) + { + return; + } + l_current_node = p_tree->nodes; + for + (i = 0; i < p_tree->numnodes; ++i) + { + l_current_node->value = 999; + l_current_node->low = 0; + l_current_node->known = 0; + ++l_current_node; } } -void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) { +void tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value) { opj_tgt_node_t *node; node = &tree->nodes[leafno]; while (node && node->value > value) { @@ -135,11 +266,11 @@ void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) { } } -void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) { +void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) { opj_tgt_node_t *stk[31]; opj_tgt_node_t **stkptr; opj_tgt_node_t *node; - int low; + OPJ_INT32 low; stkptr = stk; node = &tree->nodes[leafno]; @@ -175,11 +306,11 @@ void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) } } -int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) { +OPJ_UINT32 tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) { opj_tgt_node_t *stk[31]; opj_tgt_node_t **stkptr; opj_tgt_node_t *node; - int low; + OPJ_INT32 low; stkptr = stk; node = &tree->nodes[leafno]; diff --git a/libopenjpeg/tgt.h b/libopenjpeg/tgt.h index c08c8da0..fe1b85f5 100644 --- a/libopenjpeg/tgt.h +++ b/libopenjpeg/tgt.h @@ -5,6 +5,7 @@ * Copyright (c) 2002-2003, Yannick Verschueren * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe * Copyright (c) 2005, Herve Drolon, FreeImage Team + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,28 +39,32 @@ The functions in TGT.C have for goal to realize a tag-tree coder. The functions in TGT.C are used by some function in T2.C. */ - +#include "openjpeg.h" /** @defgroup TGT TGT - Implementation of a tag-tree coder */ /*@{*/ +struct opj_bio; /** Tag node */ -typedef struct opj_tgt_node { +typedef struct opj_tgt_node +{ struct opj_tgt_node *parent; - int value; - int low; - int known; + OPJ_INT32 value; + OPJ_INT32 low; + OPJ_UINT32 known : 1; } opj_tgt_node_t; /** Tag tree */ -typedef struct opj_tgt_tree { - int numleafsh; - int numleafsv; - int numnodes; +typedef struct opj_tgt_tree +{ + OPJ_UINT32 numleafsh; + OPJ_UINT32 numleafsv; + OPJ_UINT32 numnodes; opj_tgt_node_t *nodes; + OPJ_UINT32 nodes_size; /* maximum size taken by nodes */ } opj_tgt_tree_t; /** @name Exported functions */ @@ -71,7 +76,18 @@ Create a tag-tree @param numleafsv Height of the array of leafs of the tree @return Returns a new tag-tree if successful, returns NULL otherwise */ -opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv); +opj_tgt_tree_t *tgt_create(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv); + +/** + * Reinitialises a tag-tree from an exixting one. + * + * @param p_tree the tree to reinitialize. + * @param p_num_leafs_h the width of the array of leafs of the tree + * @param p_num_leafs_v the height of the array of leafs of the tree + * @return a new tag-tree if successful, NULL otherwise +*/ +opj_tgt_tree_t *tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, OPJ_UINT32 p_num_leafs_v); + /** Destroy a tag-tree, liberating memory @param tree Tag-tree to destroy @@ -88,7 +104,7 @@ Set the value of a leaf of a tag-tree @param leafno Number that identifies the leaf to modify @param value New value of the leaf */ -void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value); +void tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value); /** Encode the value of a leaf of the tag-tree up to a given threshold @param bio Pointer to a BIO handle @@ -96,7 +112,7 @@ Encode the value of a leaf of the tag-tree up to a given threshold @param leafno Number that identifies the leaf to encode @param threshold Threshold to use when encoding value of the leaf */ -void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold); +void tgt_encode(struct opj_bio *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold); /** Decode the value of a leaf of the tag-tree up to a given threshold @param bio Pointer to a BIO handle @@ -105,7 +121,7 @@ Decode the value of a leaf of the tag-tree up to a given threshold @param threshold Threshold to use when decoding value of the leaf @return Returns 1 if the node's value < threshold, returns 0 otherwise */ -int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold); +OPJ_UINT32 tgt_decode(struct opj_bio *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold); /* ----------------------------------------------------------------------- */ /*@}*/ diff --git a/libs/FreeImage/FreeImage.h b/libs/FreeImage/FreeImage.h new file mode 100644 index 00000000..b2dfaa41 --- /dev/null +++ b/libs/FreeImage/FreeImage.h @@ -0,0 +1,1046 @@ +// ========================================================== +// FreeImage 3 +// +// Design and implementation by +// - Floris van den Berg (flvdberg@wxs.nl) +// - Hervé Drolon (drolon@infonie.fr) +// +// Contributors: +// - Adam Gates (radad@xoasis.com) +// - Alex Kwak +// - Alexander Dymerets (sashad@te.net.ua) +// - Detlev Vendt (detlev.vendt@brillit.de) +// - Jan L. Nauta (jln@magentammt.com) +// - Jani Kajala (janik@remedy.fi) +// - Juergen Riecker (j.riecker@gmx.de) +// - Karl-Heinz Bussian (khbussian@moss.de) +// - Laurent Rocher (rocherl@club-internet.fr) +// - Luca Piergentili (l.pierge@terra.es) +// - Machiel ten Brinke (brinkem@uni-one.nl) +// - Markus Loibl (markus.loibl@epost.de) +// - Martin Weber (martweb@gmx.net) +// - Matthias Wandel (mwandel@rim.net) +// - Michal Novotny (michal@etc.cz) +// - Petr Pytelka (pyta@lightcomp.com) +// - Riley McNiff (rmcniff@marexgroup.com) +// - Ryan Rubley (ryan@lostreality.org) +// - Volker Gärtner (volkerg@gmx.at) +// +// This file is part of FreeImage 3 +// +// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY +// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES +// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE +// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED +// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT +// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY +// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL +// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER +// THIS DISCLAIMER. +// +// Use at your own risk! +// ========================================================== + +#ifndef FREEIMAGE_H +#define FREEIMAGE_H + +// Version information ------------------------------------------------------ + +#define FREEIMAGE_MAJOR_VERSION 3 +#define FREEIMAGE_MINOR_VERSION 10 +#define FREEIMAGE_RELEASE_SERIAL 0 + +// Compiler options --------------------------------------------------------- + +#include // needed for UNICODE functions + +#if defined(FREEIMAGE_LIB) + #define DLL_API + #define DLL_CALLCONV +#else + #if defined(_WIN32) || defined(__WIN32__) + #define DLL_CALLCONV __stdcall + // The following ifdef block is the standard way of creating macros which make exporting + // from a DLL simpler. All files within this DLL are compiled with the FREEIMAGE_EXPORTS + // symbol defined on the command line. this symbol should not be defined on any project + // that uses this DLL. This way any other project whose source files include this file see + // DLL_API functions as being imported from a DLL, wheras this DLL sees symbols + // defined with this macro as being exported. + #ifdef FREEIMAGE_EXPORTS + #define DLL_API __declspec(dllexport) + #else + #define DLL_API __declspec(dllimport) + #endif // FREEIMAGE_EXPORTS + #else + // try the gcc visibility support (see http://gcc.gnu.org/wiki/Visibility) + #if defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) + #ifndef GCC_HASCLASSVISIBILITY + #define GCC_HASCLASSVISIBILITY + #endif + #endif // __GNUC__ + #define DLL_CALLCONV + #if defined(GCC_HASCLASSVISIBILITY) + #define DLL_API __attribute__ ((visibility("default"))) + #else + #define DLL_API + #endif + #endif // WIN32 / !WIN32 +#endif // FREEIMAGE_LIB + +// Some versions of gcc may have BYTE_ORDER or __BYTE_ORDER defined +// If your big endian system isn't being detected, add an OS specific check +#if (defined(BYTE_ORDER) && BYTE_ORDER==BIG_ENDIAN) || \ + (defined(__BYTE_ORDER) && __BYTE_ORDER==__BIG_ENDIAN) || \ + defined(__BIG_ENDIAN__) +#define FREEIMAGE_BIGENDIAN +#endif // BYTE_ORDER + +// This really only affects 24 and 32 bit formats, the rest are always RGB order. +#define FREEIMAGE_COLORORDER_BGR 0 +#define FREEIMAGE_COLORORDER_RGB 1 +#if defined(__APPLE__) || defined(FREEIMAGE_BIGENDIAN) +#define FREEIMAGE_COLORORDER FREEIMAGE_COLORORDER_RGB +#else +#define FREEIMAGE_COLORORDER FREEIMAGE_COLORORDER_BGR +#endif + +// Ensure 4-byte enums if we're using Borland C++ compilers +#if defined(__BORLANDC__) +#pragma option push -b +#endif + +// For C compatibility -------------------------------------------------------- + +#ifdef __cplusplus +#define FI_DEFAULT(x) = x +#define FI_ENUM(x) enum x +#define FI_STRUCT(x) struct x +#else +#define FI_DEFAULT(x) +#define FI_ENUM(x) typedef int x; enum x +#define FI_STRUCT(x) typedef struct x x; struct x +#endif + +// Bitmap types ------------------------------------------------------------- + +FI_STRUCT (FIBITMAP) { void *data; }; +FI_STRUCT (FIMULTIBITMAP) { void *data; }; + +// Types used in the library (directly copied from Windows) ----------------- + +#ifndef _WINDOWS_ +#define _WINDOWS_ + +#ifndef FALSE +#define FALSE 0 +#endif +#ifndef TRUE +#define TRUE 1 +#endif +#ifndef NULL +#define NULL 0 +#endif + +#ifndef SEEK_SET +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 +#endif + +#ifndef _MSC_VER +// define portable types for 32-bit / 64-bit OS +#include +typedef int32_t BOOL; +typedef uint8_t BYTE; +typedef uint16_t WORD; +typedef uint32_t DWORD; +typedef int32_t LONG; +#else +// MS is not C99 ISO compliant +typedef long BOOL; +typedef unsigned char BYTE; +typedef unsigned short WORD; +typedef unsigned long DWORD; +typedef long LONG; +#endif // _MSC_VER + +#if (defined(_WIN32) || defined(__WIN32__)) +#pragma pack(push, 1) +#else +#pragma pack(1) +#endif // WIN32 + +typedef struct tagRGBQUAD { +#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR + BYTE rgbBlue; + BYTE rgbGreen; + BYTE rgbRed; +#else + BYTE rgbRed; + BYTE rgbGreen; + BYTE rgbBlue; +#endif // FREEIMAGE_COLORORDER + BYTE rgbReserved; +} RGBQUAD; + +typedef struct tagRGBTRIPLE { +#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR + BYTE rgbtBlue; + BYTE rgbtGreen; + BYTE rgbtRed; +#else + BYTE rgbtRed; + BYTE rgbtGreen; + BYTE rgbtBlue; +#endif // FREEIMAGE_COLORORDER +} RGBTRIPLE; + +#if (defined(_WIN32) || defined(__WIN32__)) +#pragma pack(pop) +#else +#pragma pack() +#endif // WIN32 + +typedef struct tagBITMAPINFOHEADER{ + DWORD biSize; + LONG biWidth; + LONG biHeight; + WORD biPlanes; + WORD biBitCount; + DWORD biCompression; + DWORD biSizeImage; + LONG biXPelsPerMeter; + LONG biYPelsPerMeter; + DWORD biClrUsed; + DWORD biClrImportant; +} BITMAPINFOHEADER, *PBITMAPINFOHEADER; + +typedef struct tagBITMAPINFO { + BITMAPINFOHEADER bmiHeader; + RGBQUAD bmiColors[1]; +} BITMAPINFO, *PBITMAPINFO; + +#endif // _WINDOWS_ + +// Types used in the library (specific to FreeImage) ------------------------ + +#if (defined(_WIN32) || defined(__WIN32__)) +#pragma pack(push, 1) +#else +#pragma pack(1) +#endif // WIN32 + +/** 48-bit RGB +*/ +typedef struct tagFIRGB16 { + WORD red; + WORD green; + WORD blue; +} FIRGB16; + +/** 64-bit RGBA +*/ +typedef struct tagFIRGBA16 { + WORD red; + WORD green; + WORD blue; + WORD alpha; +} FIRGBA16; + +/** 96-bit RGB Float +*/ +typedef struct tagFIRGBF { + float red; + float green; + float blue; +} FIRGBF; + +/** 128-bit RGBA Float +*/ +typedef struct tagFIRGBAF { + float red; + float green; + float blue; + float alpha; +} FIRGBAF; + +/** Data structure for COMPLEX type (complex number) +*/ +typedef struct tagFICOMPLEX { + /// real part + double r; + /// imaginary part + double i; +} FICOMPLEX; + +#if (defined(_WIN32) || defined(__WIN32__)) +#pragma pack(pop) +#else +#pragma pack() +#endif // WIN32 + +// Indexes for byte arrays, masks and shifts for treating pixels as words --- +// These coincide with the order of RGBQUAD and RGBTRIPLE ------------------- + +#ifndef FREEIMAGE_BIGENDIAN +#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR +// Little Endian (x86 / MS Windows, Linux) : BGR(A) order +#define FI_RGBA_RED 2 +#define FI_RGBA_GREEN 1 +#define FI_RGBA_BLUE 0 +#define FI_RGBA_ALPHA 3 +#define FI_RGBA_RED_MASK 0x00FF0000 +#define FI_RGBA_GREEN_MASK 0x0000FF00 +#define FI_RGBA_BLUE_MASK 0x000000FF +#define FI_RGBA_ALPHA_MASK 0xFF000000 +#define FI_RGBA_RED_SHIFT 16 +#define FI_RGBA_GREEN_SHIFT 8 +#define FI_RGBA_BLUE_SHIFT 0 +#define FI_RGBA_ALPHA_SHIFT 24 +#else +// Little Endian (x86 / MaxOSX) : RGB(A) order +#define FI_RGBA_RED 0 +#define FI_RGBA_GREEN 1 +#define FI_RGBA_BLUE 2 +#define FI_RGBA_ALPHA 3 +#define FI_RGBA_RED_MASK 0x000000FF +#define FI_RGBA_GREEN_MASK 0x0000FF00 +#define FI_RGBA_BLUE_MASK 0x00FF0000 +#define FI_RGBA_ALPHA_MASK 0xFF000000 +#define FI_RGBA_RED_SHIFT 0 +#define FI_RGBA_GREEN_SHIFT 8 +#define FI_RGBA_BLUE_SHIFT 16 +#define FI_RGBA_ALPHA_SHIFT 24 +#endif // FREEIMAGE_COLORORDER +#else +#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR +// Big Endian (PPC / none) : BGR(A) order +#define FI_RGBA_RED 2 +#define FI_RGBA_GREEN 1 +#define FI_RGBA_BLUE 0 +#define FI_RGBA_ALPHA 3 +#define FI_RGBA_RED_MASK 0x0000FF00 +#define FI_RGBA_GREEN_MASK 0x00FF0000 +#define FI_RGBA_BLUE_MASK 0xFF000000 +#define FI_RGBA_ALPHA_MASK 0x000000FF +#define FI_RGBA_RED_SHIFT 8 +#define FI_RGBA_GREEN_SHIFT 16 +#define FI_RGBA_BLUE_SHIFT 24 +#define FI_RGBA_ALPHA_SHIFT 0 +#else +// Big Endian (PPC / Linux, MaxOSX) : RGB(A) order +#define FI_RGBA_RED 0 +#define FI_RGBA_GREEN 1 +#define FI_RGBA_BLUE 2 +#define FI_RGBA_ALPHA 3 +#define FI_RGBA_RED_MASK 0xFF000000 +#define FI_RGBA_GREEN_MASK 0x00FF0000 +#define FI_RGBA_BLUE_MASK 0x0000FF00 +#define FI_RGBA_ALPHA_MASK 0x000000FF +#define FI_RGBA_RED_SHIFT 24 +#define FI_RGBA_GREEN_SHIFT 16 +#define FI_RGBA_BLUE_SHIFT 8 +#define FI_RGBA_ALPHA_SHIFT 0 +#endif // FREEIMAGE_COLORORDER +#endif // FREEIMAGE_BIGENDIAN + +#define FI_RGBA_RGB_MASK (FI_RGBA_RED_MASK|FI_RGBA_GREEN_MASK|FI_RGBA_BLUE_MASK) + +// The 16bit macros only include masks and shifts, since each color element is not byte aligned + +#define FI16_555_RED_MASK 0x7C00 +#define FI16_555_GREEN_MASK 0x03E0 +#define FI16_555_BLUE_MASK 0x001F +#define FI16_555_RED_SHIFT 10 +#define FI16_555_GREEN_SHIFT 5 +#define FI16_555_BLUE_SHIFT 0 +#define FI16_565_RED_MASK 0xF800 +#define FI16_565_GREEN_MASK 0x07E0 +#define FI16_565_BLUE_MASK 0x001F +#define FI16_565_RED_SHIFT 11 +#define FI16_565_GREEN_SHIFT 5 +#define FI16_565_BLUE_SHIFT 0 + +// ICC profile support ------------------------------------------------------ + +#define FIICC_DEFAULT 0x00 +#define FIICC_COLOR_IS_CMYK 0x01 + +FI_STRUCT (FIICCPROFILE) { + WORD flags; // info flag + DWORD size; // profile's size measured in bytes + void *data; // points to a block of contiguous memory containing the profile +}; + +// Important enums ---------------------------------------------------------- + +/** I/O image format identifiers. +*/ +FI_ENUM(FREE_IMAGE_FORMAT) { + FIF_UNKNOWN = -1, + FIF_BMP = 0, + FIF_ICO = 1, + FIF_JPEG = 2, + FIF_JNG = 3, + FIF_KOALA = 4, + FIF_LBM = 5, + FIF_IFF = FIF_LBM, + FIF_MNG = 6, + FIF_PBM = 7, + FIF_PBMRAW = 8, + FIF_PCD = 9, + FIF_PCX = 10, + FIF_PGM = 11, + FIF_PGMRAW = 12, + FIF_PNG = 13, + FIF_PPM = 14, + FIF_PPMRAW = 15, + FIF_RAS = 16, + FIF_TARGA = 17, + FIF_TIFF = 18, + FIF_WBMP = 19, + FIF_PSD = 20, + FIF_CUT = 21, + FIF_XBM = 22, + FIF_XPM = 23, + FIF_DDS = 24, + FIF_GIF = 25, + FIF_HDR = 26, + FIF_FAXG3 = 27, + FIF_SGI = 28, + FIF_EXR = 29, + FIF_J2K = 30, + FIF_JP2 = 31 +}; + +/** Image type used in FreeImage. +*/ +FI_ENUM(FREE_IMAGE_TYPE) { + FIT_UNKNOWN = 0, // unknown type + FIT_BITMAP = 1, // standard image : 1-, 4-, 8-, 16-, 24-, 32-bit + FIT_UINT16 = 2, // array of unsigned short : unsigned 16-bit + FIT_INT16 = 3, // array of short : signed 16-bit + FIT_UINT32 = 4, // array of unsigned long : unsigned 32-bit + FIT_INT32 = 5, // array of long : signed 32-bit + FIT_FLOAT = 6, // array of float : 32-bit IEEE floating point + FIT_DOUBLE = 7, // array of double : 64-bit IEEE floating point + FIT_COMPLEX = 8, // array of FICOMPLEX : 2 x 64-bit IEEE floating point + FIT_RGB16 = 9, // 48-bit RGB image : 3 x 16-bit + FIT_RGBA16 = 10, // 64-bit RGBA image : 4 x 16-bit + FIT_RGBF = 11, // 96-bit RGB float image : 3 x 32-bit IEEE floating point + FIT_RGBAF = 12 // 128-bit RGBA float image : 4 x 32-bit IEEE floating point +}; + +/** Image color type used in FreeImage. +*/ +FI_ENUM(FREE_IMAGE_COLOR_TYPE) { + FIC_MINISWHITE = 0, // min value is white + FIC_MINISBLACK = 1, // min value is black + FIC_RGB = 2, // RGB color model + FIC_PALETTE = 3, // color map indexed + FIC_RGBALPHA = 4, // RGB color model with alpha channel + FIC_CMYK = 5 // CMYK color model +}; + +/** Color quantization algorithms. +Constants used in FreeImage_ColorQuantize. +*/ +FI_ENUM(FREE_IMAGE_QUANTIZE) { + FIQ_WUQUANT = 0, // Xiaolin Wu color quantization algorithm + FIQ_NNQUANT = 1 // NeuQuant neural-net quantization algorithm by Anthony Dekker +}; + +/** Dithering algorithms. +Constants used in FreeImage_Dither. +*/ +FI_ENUM(FREE_IMAGE_DITHER) { + FID_FS = 0, // Floyd & Steinberg error diffusion + FID_BAYER4x4 = 1, // Bayer ordered dispersed dot dithering (order 2 dithering matrix) + FID_BAYER8x8 = 2, // Bayer ordered dispersed dot dithering (order 3 dithering matrix) + FID_CLUSTER6x6 = 3, // Ordered clustered dot dithering (order 3 - 6x6 matrix) + FID_CLUSTER8x8 = 4, // Ordered clustered dot dithering (order 4 - 8x8 matrix) + FID_CLUSTER16x16= 5, // Ordered clustered dot dithering (order 8 - 16x16 matrix) + FID_BAYER16x16 = 6 // Bayer ordered dispersed dot dithering (order 4 dithering matrix) +}; + +/** Lossless JPEG transformations +Constants used in FreeImage_JPEGTransform +*/ +FI_ENUM(FREE_IMAGE_JPEG_OPERATION) { + FIJPEG_OP_NONE = 0, // no transformation + FIJPEG_OP_FLIP_H = 1, // horizontal flip + FIJPEG_OP_FLIP_V = 2, // vertical flip + FIJPEG_OP_TRANSPOSE = 3, // transpose across UL-to-LR axis + FIJPEG_OP_TRANSVERSE = 4, // transpose across UR-to-LL axis + FIJPEG_OP_ROTATE_90 = 5, // 90-degree clockwise rotation + FIJPEG_OP_ROTATE_180 = 6, // 180-degree rotation + FIJPEG_OP_ROTATE_270 = 7 // 270-degree clockwise (or 90 ccw) +}; + +/** Tone mapping operators. +Constants used in FreeImage_ToneMapping. +*/ +FI_ENUM(FREE_IMAGE_TMO) { + FITMO_DRAGO03 = 0, // Adaptive logarithmic mapping (F. Drago, 2003) + FITMO_REINHARD05 = 1, // Dynamic range reduction inspired by photoreceptor physiology (E. Reinhard, 2005) + FITMO_FATTAL02 = 2 // Gradient domain high dynamic range compression (R. Fattal, 2002) +}; + +/** Upsampling / downsampling filters. +Constants used in FreeImage_Rescale. +*/ +FI_ENUM(FREE_IMAGE_FILTER) { + FILTER_BOX = 0, // Box, pulse, Fourier window, 1st order (constant) b-spline + FILTER_BICUBIC = 1, // Mitchell & Netravali's two-param cubic filter + FILTER_BILINEAR = 2, // Bilinear filter + FILTER_BSPLINE = 3, // 4th order (cubic) b-spline + FILTER_CATMULLROM = 4, // Catmull-Rom spline, Overhauser spline + FILTER_LANCZOS3 = 5 // Lanczos3 filter +}; + +/** Color channels. +Constants used in color manipulation routines. +*/ +FI_ENUM(FREE_IMAGE_COLOR_CHANNEL) { + FICC_RGB = 0, // Use red, green and blue channels + FICC_RED = 1, // Use red channel + FICC_GREEN = 2, // Use green channel + FICC_BLUE = 3, // Use blue channel + FICC_ALPHA = 4, // Use alpha channel + FICC_BLACK = 5, // Use black channel + FICC_REAL = 6, // Complex images: use real part + FICC_IMAG = 7, // Complex images: use imaginary part + FICC_MAG = 8, // Complex images: use magnitude + FICC_PHASE = 9 // Complex images: use phase +}; + +// Metadata support --------------------------------------------------------- + +/** + Tag data type information (based on TIFF specifications) + + Note: RATIONALs are the ratio of two 32-bit integer values. +*/ +FI_ENUM(FREE_IMAGE_MDTYPE) { + FIDT_NOTYPE = 0, // placeholder + FIDT_BYTE = 1, // 8-bit unsigned integer + FIDT_ASCII = 2, // 8-bit bytes w/ last byte null + FIDT_SHORT = 3, // 16-bit unsigned integer + FIDT_LONG = 4, // 32-bit unsigned integer + FIDT_RATIONAL = 5, // 64-bit unsigned fraction + FIDT_SBYTE = 6, // 8-bit signed integer + FIDT_UNDEFINED = 7, // 8-bit untyped data + FIDT_SSHORT = 8, // 16-bit signed integer + FIDT_SLONG = 9, // 32-bit signed integer + FIDT_SRATIONAL = 10, // 64-bit signed fraction + FIDT_FLOAT = 11, // 32-bit IEEE floating point + FIDT_DOUBLE = 12, // 64-bit IEEE floating point + FIDT_IFD = 13, // 32-bit unsigned integer (offset) + FIDT_PALETTE = 14 // 32-bit RGBQUAD +}; + +/** + Metadata models supported by FreeImage +*/ +FI_ENUM(FREE_IMAGE_MDMODEL) { + FIMD_NODATA = -1, + FIMD_COMMENTS = 0, // single comment or keywords + FIMD_EXIF_MAIN = 1, // Exif-TIFF metadata + FIMD_EXIF_EXIF = 2, // Exif-specific metadata + FIMD_EXIF_GPS = 3, // Exif GPS metadata + FIMD_EXIF_MAKERNOTE = 4, // Exif maker note metadata + FIMD_EXIF_INTEROP = 5, // Exif interoperability metadata + FIMD_IPTC = 6, // IPTC/NAA metadata + FIMD_XMP = 7, // Abobe XMP metadata + FIMD_GEOTIFF = 8, // GeoTIFF metadata + FIMD_ANIMATION = 9, // Animation metadata + FIMD_CUSTOM = 10 // Used to attach other metadata types to a dib +}; + +/** + Handle to a metadata model +*/ +FI_STRUCT (FIMETADATA) { void *data; }; + +/** + Handle to a FreeImage tag +*/ +FI_STRUCT (FITAG) { void *data; }; + +// File IO routines --------------------------------------------------------- + +#ifndef FREEIMAGE_IO +#define FREEIMAGE_IO + +typedef void* fi_handle; +typedef unsigned (DLL_CALLCONV *FI_ReadProc) (void *buffer, unsigned size, unsigned count, fi_handle handle); +typedef unsigned (DLL_CALLCONV *FI_WriteProc) (void *buffer, unsigned size, unsigned count, fi_handle handle); +typedef int (DLL_CALLCONV *FI_SeekProc) (fi_handle handle, long offset, int origin); +typedef long (DLL_CALLCONV *FI_TellProc) (fi_handle handle); + +#if (defined(_WIN32) || defined(__WIN32__)) +#pragma pack(push, 1) +#else +#pragma pack(1) +#endif // WIN32 + +FI_STRUCT(FreeImageIO) { + FI_ReadProc read_proc; // pointer to the function used to read data + FI_WriteProc write_proc; // pointer to the function used to write data + FI_SeekProc seek_proc; // pointer to the function used to seek + FI_TellProc tell_proc; // pointer to the function used to aquire the current position +}; + +#if (defined(_WIN32) || defined(__WIN32__)) +#pragma pack(pop) +#else +#pragma pack() +#endif // WIN32 + +/** +Handle to a memory I/O stream +*/ +FI_STRUCT (FIMEMORY) { void *data; }; + +#endif // FREEIMAGE_IO + +// Plugin routines ---------------------------------------------------------- + +#ifndef PLUGINS +#define PLUGINS + +typedef const char *(DLL_CALLCONV *FI_FormatProc) (); +typedef const char *(DLL_CALLCONV *FI_DescriptionProc) (); +typedef const char *(DLL_CALLCONV *FI_ExtensionListProc) (); +typedef const char *(DLL_CALLCONV *FI_RegExprProc) (); +typedef void *(DLL_CALLCONV *FI_OpenProc)(FreeImageIO *io, fi_handle handle, BOOL read); +typedef void (DLL_CALLCONV *FI_CloseProc)(FreeImageIO *io, fi_handle handle, void *data); +typedef int (DLL_CALLCONV *FI_PageCountProc)(FreeImageIO *io, fi_handle handle, void *data); +typedef int (DLL_CALLCONV *FI_PageCapabilityProc)(FreeImageIO *io, fi_handle handle, void *data); +typedef FIBITMAP *(DLL_CALLCONV *FI_LoadProc)(FreeImageIO *io, fi_handle handle, int page, int flags, void *data); +typedef BOOL (DLL_CALLCONV *FI_SaveProc)(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data); +typedef BOOL (DLL_CALLCONV *FI_ValidateProc)(FreeImageIO *io, fi_handle handle); +typedef const char *(DLL_CALLCONV *FI_MimeProc) (); +typedef BOOL (DLL_CALLCONV *FI_SupportsExportBPPProc)(int bpp); +typedef BOOL (DLL_CALLCONV *FI_SupportsExportTypeProc)(FREE_IMAGE_TYPE type); +typedef BOOL (DLL_CALLCONV *FI_SupportsICCProfilesProc)(); + +FI_STRUCT (Plugin) { + FI_FormatProc format_proc; + FI_DescriptionProc description_proc; + FI_ExtensionListProc extension_proc; + FI_RegExprProc regexpr_proc; + FI_OpenProc open_proc; + FI_CloseProc close_proc; + FI_PageCountProc pagecount_proc; + FI_PageCapabilityProc pagecapability_proc; + FI_LoadProc load_proc; + FI_SaveProc save_proc; + FI_ValidateProc validate_proc; + FI_MimeProc mime_proc; + FI_SupportsExportBPPProc supports_export_bpp_proc; + FI_SupportsExportTypeProc supports_export_type_proc; + FI_SupportsICCProfilesProc supports_icc_profiles_proc; +}; + +typedef void (DLL_CALLCONV *FI_InitProc)(Plugin *plugin, int format_id); + +#endif // PLUGINS + + +// Load / Save flag constants ----------------------------------------------- + +#define BMP_DEFAULT 0 +#define BMP_SAVE_RLE 1 +#define CUT_DEFAULT 0 +#define DDS_DEFAULT 0 +#define EXR_DEFAULT 0 // save data as half with piz-based wavelet compression +#define EXR_FLOAT 0x0001 // save data as float instead of as half (not recommended) +#define EXR_NONE 0x0002 // save with no compression +#define EXR_ZIP 0x0004 // save with zlib compression, in blocks of 16 scan lines +#define EXR_PIZ 0x0008 // save with piz-based wavelet compression +#define EXR_PXR24 0x0010 // save with lossy 24-bit float compression +#define EXR_B44 0x0020 // save with lossy 44% float compression - goes to 22% when combined with EXR_LC +#define EXR_LC 0x0040 // save images with one luminance and two chroma channels, rather than as RGB (lossy compression) +#define FAXG3_DEFAULT 0 +#define GIF_DEFAULT 0 +#define GIF_LOAD256 1 // Load the image as a 256 color image with ununsed palette entries, if it's 16 or 2 color +#define GIF_PLAYBACK 2 // 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading +#define HDR_DEFAULT 0 +#define ICO_DEFAULT 0 +#define ICO_MAKEALPHA 1 // convert to 32bpp and create an alpha channel from the AND-mask when loading +#define IFF_DEFAULT 0 +#define J2K_DEFAULT 0 // save with a 16:1 rate +#define JP2_DEFAULT 0 // save with a 16:1 rate +#define JPEG_DEFAULT 0 // loading (see JPEG_FAST); saving (see JPEG_QUALITYGOOD) +#define JPEG_FAST 0x0001 // load the file as fast as possible, sacrificing some quality +#define JPEG_ACCURATE 0x0002 // load the file with the best quality, sacrificing some speed +#define JPEG_CMYK 0x0004 // load separated CMYK "as is" (use | to combine with other load flags) +#define JPEG_QUALITYSUPERB 0x80 // save with superb quality (100:1) +#define JPEG_QUALITYGOOD 0x0100 // save with good quality (75:1) +#define JPEG_QUALITYNORMAL 0x0200 // save with normal quality (50:1) +#define JPEG_QUALITYAVERAGE 0x0400 // save with average quality (25:1) +#define JPEG_QUALITYBAD 0x0800 // save with bad quality (10:1) +#define JPEG_PROGRESSIVE 0x2000 // save as a progressive-JPEG (use | to combine with other save flags) +#define KOALA_DEFAULT 0 +#define LBM_DEFAULT 0 +#define MNG_DEFAULT 0 +#define PCD_DEFAULT 0 +#define PCD_BASE 1 // load the bitmap sized 768 x 512 +#define PCD_BASEDIV4 2 // load the bitmap sized 384 x 256 +#define PCD_BASEDIV16 3 // load the bitmap sized 192 x 128 +#define PCX_DEFAULT 0 +#define PNG_DEFAULT 0 +#define PNG_IGNOREGAMMA 1 // avoid gamma correction +#define PNM_DEFAULT 0 +#define PNM_SAVE_RAW 0 // If set the writer saves in RAW format (i.e. P4, P5 or P6) +#define PNM_SAVE_ASCII 1 // If set the writer saves in ASCII format (i.e. P1, P2 or P3) +#define PSD_DEFAULT 0 +#define RAS_DEFAULT 0 +#define SGI_DEFAULT 0 +#define TARGA_DEFAULT 0 +#define TARGA_LOAD_RGB888 1 // If set the loader converts RGB555 and ARGB8888 -> RGB888. +#define TIFF_DEFAULT 0 +#define TIFF_CMYK 0x0001 // reads/stores tags for separated CMYK (use | to combine with compression flags) +#define TIFF_PACKBITS 0x0100 // save using PACKBITS compression +#define TIFF_DEFLATE 0x0200 // save using DEFLATE compression (a.k.a. ZLIB compression) +#define TIFF_ADOBE_DEFLATE 0x0400 // save using ADOBE DEFLATE compression +#define TIFF_NONE 0x0800 // save without any compression +#define TIFF_CCITTFAX3 0x1000 // save using CCITT Group 3 fax encoding +#define TIFF_CCITTFAX4 0x2000 // save using CCITT Group 4 fax encoding +#define TIFF_LZW 0x4000 // save using LZW compression +#define TIFF_JPEG 0x8000 // save using JPEG compression +#define WBMP_DEFAULT 0 +#define XBM_DEFAULT 0 +#define XPM_DEFAULT 0 + + +#ifdef __cplusplus +extern "C" { +#endif + +// Init / Error routines ---------------------------------------------------- + +DLL_API void DLL_CALLCONV FreeImage_Initialise(BOOL load_local_plugins_only FI_DEFAULT(FALSE)); +DLL_API void DLL_CALLCONV FreeImage_DeInitialise(void); + +// Version routines --------------------------------------------------------- + +DLL_API const char *DLL_CALLCONV FreeImage_GetVersion(void); +DLL_API const char *DLL_CALLCONV FreeImage_GetCopyrightMessage(void); + +// Message output functions ------------------------------------------------- + +typedef void (*FreeImage_OutputMessageFunction)(FREE_IMAGE_FORMAT fif, const char *msg); +typedef void (DLL_CALLCONV *FreeImage_OutputMessageFunctionStdCall)(FREE_IMAGE_FORMAT fif, const char *msg); + +DLL_API void DLL_CALLCONV FreeImage_SetOutputMessageStdCall(FreeImage_OutputMessageFunctionStdCall omf); +DLL_API void DLL_CALLCONV FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf); +DLL_API void DLL_CALLCONV FreeImage_OutputMessageProc(int fif, const char *fmt, ...); + +// Allocate / Clone / Unload routines --------------------------------------- + +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Allocate(int width, int height, int bpp, unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_AllocateT(FREE_IMAGE_TYPE type, int width, int height, int bpp FI_DEFAULT(8), unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0)); +DLL_API FIBITMAP * DLL_CALLCONV FreeImage_Clone(FIBITMAP *dib); +DLL_API void DLL_CALLCONV FreeImage_Unload(FIBITMAP *dib); + +// Load / Save routines ----------------------------------------------------- + +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Load(FREE_IMAGE_FORMAT fif, const char *filename, int flags FI_DEFAULT(0)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadU(FREE_IMAGE_FORMAT fif, const wchar_t *filename, int flags FI_DEFAULT(0)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0)); +DLL_API BOOL DLL_CALLCONV FreeImage_Save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const char *filename, int flags FI_DEFAULT(0)); +DLL_API BOOL DLL_CALLCONV FreeImage_SaveU(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const wchar_t *filename, int flags FI_DEFAULT(0)); +DLL_API BOOL DLL_CALLCONV FreeImage_SaveToHandle(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0)); + +// Memory I/O stream routines ----------------------------------------------- + +DLL_API FIMEMORY *DLL_CALLCONV FreeImage_OpenMemory(BYTE *data FI_DEFAULT(0), DWORD size_in_bytes FI_DEFAULT(0)); +DLL_API void DLL_CALLCONV FreeImage_CloseMemory(FIMEMORY *stream); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0)); +DLL_API BOOL DLL_CALLCONV FreeImage_SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FIMEMORY *stream, int flags FI_DEFAULT(0)); +DLL_API long DLL_CALLCONV FreeImage_TellMemory(FIMEMORY *stream); +DLL_API BOOL DLL_CALLCONV FreeImage_SeekMemory(FIMEMORY *stream, long offset, int origin); +DLL_API BOOL DLL_CALLCONV FreeImage_AcquireMemory(FIMEMORY *stream, BYTE **data, DWORD *size_in_bytes); +DLL_API unsigned DLL_CALLCONV FreeImage_ReadMemory(void *buffer, unsigned size, unsigned count, FIMEMORY *stream); +DLL_API unsigned DLL_CALLCONV FreeImage_WriteMemory(const void *buffer, unsigned size, unsigned count, FIMEMORY *stream); +DLL_API FIMULTIBITMAP *DLL_CALLCONV FreeImage_LoadMultiBitmapFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0)); + +// Plugin Interface --------------------------------------------------------- + +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_RegisterLocalPlugin(FI_InitProc proc_address, const char *format FI_DEFAULT(0), const char *description FI_DEFAULT(0), const char *extension FI_DEFAULT(0), const char *regexpr FI_DEFAULT(0)); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_RegisterExternalPlugin(const char *path, const char *format FI_DEFAULT(0), const char *description FI_DEFAULT(0), const char *extension FI_DEFAULT(0), const char *regexpr FI_DEFAULT(0)); +DLL_API int DLL_CALLCONV FreeImage_GetFIFCount(void); +DLL_API int DLL_CALLCONV FreeImage_SetPluginEnabled(FREE_IMAGE_FORMAT fif, BOOL enable); +DLL_API int DLL_CALLCONV FreeImage_IsPluginEnabled(FREE_IMAGE_FORMAT fif); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFormat(const char *format); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromMime(const char *mime); +DLL_API const char *DLL_CALLCONV FreeImage_GetFormatFromFIF(FREE_IMAGE_FORMAT fif); +DLL_API const char *DLL_CALLCONV FreeImage_GetFIFExtensionList(FREE_IMAGE_FORMAT fif); +DLL_API const char *DLL_CALLCONV FreeImage_GetFIFDescription(FREE_IMAGE_FORMAT fif); +DLL_API const char *DLL_CALLCONV FreeImage_GetFIFRegExpr(FREE_IMAGE_FORMAT fif); +DLL_API const char *DLL_CALLCONV FreeImage_GetFIFMimeType(FREE_IMAGE_FORMAT fif); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFilename(const char *filename); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFilenameU(const wchar_t *filename); +DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsReading(FREE_IMAGE_FORMAT fif); +DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsWriting(FREE_IMAGE_FORMAT fif); +DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsExportBPP(FREE_IMAGE_FORMAT fif, int bpp); +DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsExportType(FREE_IMAGE_FORMAT fif, FREE_IMAGE_TYPE type); +DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsICCProfiles(FREE_IMAGE_FORMAT fif); + +// Multipaging interface ---------------------------------------------------- + +DLL_API FIMULTIBITMAP * DLL_CALLCONV FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory FI_DEFAULT(FALSE), int flags FI_DEFAULT(0)); +DLL_API BOOL DLL_CALLCONV FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags FI_DEFAULT(0)); +DLL_API int DLL_CALLCONV FreeImage_GetPageCount(FIMULTIBITMAP *bitmap); +DLL_API void DLL_CALLCONV FreeImage_AppendPage(FIMULTIBITMAP *bitmap, FIBITMAP *data); +DLL_API void DLL_CALLCONV FreeImage_InsertPage(FIMULTIBITMAP *bitmap, int page, FIBITMAP *data); +DLL_API void DLL_CALLCONV FreeImage_DeletePage(FIMULTIBITMAP *bitmap, int page); +DLL_API FIBITMAP * DLL_CALLCONV FreeImage_LockPage(FIMULTIBITMAP *bitmap, int page); +DLL_API void DLL_CALLCONV FreeImage_UnlockPage(FIMULTIBITMAP *bitmap, FIBITMAP *data, BOOL changed); +DLL_API BOOL DLL_CALLCONV FreeImage_MovePage(FIMULTIBITMAP *bitmap, int target, int source); +DLL_API BOOL DLL_CALLCONV FreeImage_GetLockedPageNumbers(FIMULTIBITMAP *bitmap, int *pages, int *count); + +// Filetype request routines ------------------------------------------------ + +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileType(const char *filename, int size FI_DEFAULT(0)); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeU(const wchar_t *filename, int size FI_DEFAULT(0)); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromHandle(FreeImageIO *io, fi_handle handle, int size FI_DEFAULT(0)); +DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromMemory(FIMEMORY *stream, int size FI_DEFAULT(0)); + +// Image type request routine ----------------------------------------------- + +DLL_API FREE_IMAGE_TYPE DLL_CALLCONV FreeImage_GetImageType(FIBITMAP *dib); + +// FreeImage helper routines ------------------------------------------------ + +DLL_API BOOL DLL_CALLCONV FreeImage_IsLittleEndian(void); +DLL_API BOOL DLL_CALLCONV FreeImage_LookupX11Color(const char *szColor, BYTE *nRed, BYTE *nGreen, BYTE *nBlue); +DLL_API BOOL DLL_CALLCONV FreeImage_LookupSVGColor(const char *szColor, BYTE *nRed, BYTE *nGreen, BYTE *nBlue); + + +// Pixel access routines ---------------------------------------------------- + +DLL_API BYTE *DLL_CALLCONV FreeImage_GetBits(FIBITMAP *dib); +DLL_API BYTE *DLL_CALLCONV FreeImage_GetScanLine(FIBITMAP *dib, int scanline); + +DLL_API BOOL DLL_CALLCONV FreeImage_GetPixelIndex(FIBITMAP *dib, unsigned x, unsigned y, BYTE *value); +DLL_API BOOL DLL_CALLCONV FreeImage_GetPixelColor(FIBITMAP *dib, unsigned x, unsigned y, RGBQUAD *value); +DLL_API BOOL DLL_CALLCONV FreeImage_SetPixelIndex(FIBITMAP *dib, unsigned x, unsigned y, BYTE *value); +DLL_API BOOL DLL_CALLCONV FreeImage_SetPixelColor(FIBITMAP *dib, unsigned x, unsigned y, RGBQUAD *value); + +// DIB info routines -------------------------------------------------------- + +DLL_API unsigned DLL_CALLCONV FreeImage_GetColorsUsed(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetBPP(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetWidth(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetHeight(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetLine(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetPitch(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetDIBSize(FIBITMAP *dib); +DLL_API RGBQUAD *DLL_CALLCONV FreeImage_GetPalette(FIBITMAP *dib); + +DLL_API unsigned DLL_CALLCONV FreeImage_GetDotsPerMeterX(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetDotsPerMeterY(FIBITMAP *dib); +DLL_API void DLL_CALLCONV FreeImage_SetDotsPerMeterX(FIBITMAP *dib, unsigned res); +DLL_API void DLL_CALLCONV FreeImage_SetDotsPerMeterY(FIBITMAP *dib, unsigned res); + +DLL_API BITMAPINFOHEADER *DLL_CALLCONV FreeImage_GetInfoHeader(FIBITMAP *dib); +DLL_API BITMAPINFO *DLL_CALLCONV FreeImage_GetInfo(FIBITMAP *dib); +DLL_API FREE_IMAGE_COLOR_TYPE DLL_CALLCONV FreeImage_GetColorType(FIBITMAP *dib); + +DLL_API unsigned DLL_CALLCONV FreeImage_GetRedMask(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetGreenMask(FIBITMAP *dib); +DLL_API unsigned DLL_CALLCONV FreeImage_GetBlueMask(FIBITMAP *dib); + +DLL_API unsigned DLL_CALLCONV FreeImage_GetTransparencyCount(FIBITMAP *dib); +DLL_API BYTE * DLL_CALLCONV FreeImage_GetTransparencyTable(FIBITMAP *dib); +DLL_API void DLL_CALLCONV FreeImage_SetTransparent(FIBITMAP *dib, BOOL enabled); +DLL_API void DLL_CALLCONV FreeImage_SetTransparencyTable(FIBITMAP *dib, BYTE *table, int count); +DLL_API BOOL DLL_CALLCONV FreeImage_IsTransparent(FIBITMAP *dib); +DLL_API void DLL_CALLCONV FreeImage_SetTransparentIndex(FIBITMAP *dib, int index); +DLL_API int DLL_CALLCONV FreeImage_GetTransparentIndex(FIBITMAP *dib); + +DLL_API BOOL DLL_CALLCONV FreeImage_HasBackgroundColor(FIBITMAP *dib); +DLL_API BOOL DLL_CALLCONV FreeImage_GetBackgroundColor(FIBITMAP *dib, RGBQUAD *bkcolor); +DLL_API BOOL DLL_CALLCONV FreeImage_SetBackgroundColor(FIBITMAP *dib, RGBQUAD *bkcolor); + + +// ICC profile routines ----------------------------------------------------- + +DLL_API FIICCPROFILE *DLL_CALLCONV FreeImage_GetICCProfile(FIBITMAP *dib); +DLL_API FIICCPROFILE *DLL_CALLCONV FreeImage_CreateICCProfile(FIBITMAP *dib, void *data, long size); +DLL_API void DLL_CALLCONV FreeImage_DestroyICCProfile(FIBITMAP *dib); + +// Line conversion routines ------------------------------------------------- + +DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To4(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To4(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To4_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To4_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To4(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To4(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To8(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To8(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To8_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To8_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To8(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To8(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To16_555(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To16_555(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To16_555(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16_565_To16_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To16_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To16_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To16_565(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To16_565(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To16_565(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16_555_To16_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To16_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To16_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To24(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To24(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To24(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To24_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To24_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine32To24(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine1To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine4To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine8To32(BYTE *target, BYTE *source, int width_in_pixels, RGBQUAD *palette); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To32_555(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine16To32_565(BYTE *target, BYTE *source, int width_in_pixels); +DLL_API void DLL_CALLCONV FreeImage_ConvertLine24To32(BYTE *target, BYTE *source, int width_in_pixels); + +// Smart conversion routines ------------------------------------------------ + +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo4Bits(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo8Bits(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToGreyscale(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo16Bits555(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo16Bits565(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo24Bits(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo32Bits(FIBITMAP *dib); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ColorQuantize(FIBITMAP *dib, FREE_IMAGE_QUANTIZE quantize); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ColorQuantizeEx(FIBITMAP *dib, FREE_IMAGE_QUANTIZE quantize FI_DEFAULT(FIQ_WUQUANT), int PaletteSize FI_DEFAULT(256), int ReserveSize FI_DEFAULT(0), RGBQUAD *ReservePalette FI_DEFAULT(NULL)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Threshold(FIBITMAP *dib, BYTE T); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Dither(FIBITMAP *dib, FREE_IMAGE_DITHER algorithm); + +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertFromRawBits(BYTE *bits, int width, int height, int pitch, unsigned bpp, unsigned red_mask, unsigned green_mask, unsigned blue_mask, BOOL topdown FI_DEFAULT(FALSE)); +DLL_API void DLL_CALLCONV FreeImage_ConvertToRawBits(BYTE *bits, FIBITMAP *dib, int pitch, unsigned bpp, unsigned red_mask, unsigned green_mask, unsigned blue_mask, BOOL topdown FI_DEFAULT(FALSE)); + +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToRGBF(FIBITMAP *dib); + +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToStandardType(FIBITMAP *src, BOOL scale_linear FI_DEFAULT(TRUE)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToType(FIBITMAP *src, FREE_IMAGE_TYPE dst_type, BOOL scale_linear FI_DEFAULT(TRUE)); + +// tone mapping operators +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ToneMapping(FIBITMAP *dib, FREE_IMAGE_TMO tmo, double first_param FI_DEFAULT(0), double second_param FI_DEFAULT(0)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoDrago03(FIBITMAP *src, double gamma FI_DEFAULT(2.2), double exposure FI_DEFAULT(0)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoReinhard05(FIBITMAP *src, double intensity FI_DEFAULT(0), double contrast FI_DEFAULT(0)); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_TmoFattal02(FIBITMAP *src, double color_saturation FI_DEFAULT(0.5), double attenuation FI_DEFAULT(0.85)); + +// ZLib interface ----------------------------------------------------------- + +DLL_API DWORD DLL_CALLCONV FreeImage_ZLibCompress(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size); +DLL_API DWORD DLL_CALLCONV FreeImage_ZLibUncompress(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size); +DLL_API DWORD DLL_CALLCONV FreeImage_ZLibGZip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size); +DLL_API DWORD DLL_CALLCONV FreeImage_ZLibGUnzip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size); +DLL_API DWORD DLL_CALLCONV FreeImage_ZLibCRC32(DWORD crc, BYTE *source, DWORD source_size); + +// -------------------------------------------------------------------------- +// Metadata routines -------------------------------------------------------- +// -------------------------------------------------------------------------- + +// tag creation / destruction +DLL_API FITAG *DLL_CALLCONV FreeImage_CreateTag(); +DLL_API void DLL_CALLCONV FreeImage_DeleteTag(FITAG *tag); +DLL_API FITAG *DLL_CALLCONV FreeImage_CloneTag(FITAG *tag); + +// tag getters and setters +DLL_API const char *DLL_CALLCONV FreeImage_GetTagKey(FITAG *tag); +DLL_API const char *DLL_CALLCONV FreeImage_GetTagDescription(FITAG *tag); +DLL_API WORD DLL_CALLCONV FreeImage_GetTagID(FITAG *tag); +DLL_API FREE_IMAGE_MDTYPE DLL_CALLCONV FreeImage_GetTagType(FITAG *tag); +DLL_API DWORD DLL_CALLCONV FreeImage_GetTagCount(FITAG *tag); +DLL_API DWORD DLL_CALLCONV FreeImage_GetTagLength(FITAG *tag); +DLL_API const void *DLL_CALLCONV FreeImage_GetTagValue(FITAG *tag); + +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagKey(FITAG *tag, const char *key); +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagDescription(FITAG *tag, const char *description); +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagID(FITAG *tag, WORD id); +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagType(FITAG *tag, FREE_IMAGE_MDTYPE type); +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagCount(FITAG *tag, DWORD count); +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagLength(FITAG *tag, DWORD length); +DLL_API BOOL DLL_CALLCONV FreeImage_SetTagValue(FITAG *tag, const void *value); + +// iterator +DLL_API FIMETADATA *DLL_CALLCONV FreeImage_FindFirstMetadata(FREE_IMAGE_MDMODEL model, FIBITMAP *dib, FITAG **tag); +DLL_API BOOL DLL_CALLCONV FreeImage_FindNextMetadata(FIMETADATA *mdhandle, FITAG **tag); +DLL_API void DLL_CALLCONV FreeImage_FindCloseMetadata(FIMETADATA *mdhandle); + +// metadata setter and getter +DLL_API BOOL DLL_CALLCONV FreeImage_SetMetadata(FREE_IMAGE_MDMODEL model, FIBITMAP *dib, const char *key, FITAG *tag); +DLL_API BOOL DLL_CALLCONV FreeImage_GetMetadata(FREE_IMAGE_MDMODEL model, FIBITMAP *dib, const char *key, FITAG **tag); + +// helpers +DLL_API unsigned DLL_CALLCONV FreeImage_GetMetadataCount(FREE_IMAGE_MDMODEL model, FIBITMAP *dib); + +// tag to C string conversion +DLL_API const char* DLL_CALLCONV FreeImage_TagToString(FREE_IMAGE_MDMODEL model, FITAG *tag, char *Make FI_DEFAULT(NULL)); + +// -------------------------------------------------------------------------- +// Image manipulation toolkit ----------------------------------------------- +// -------------------------------------------------------------------------- + +// rotation and flipping +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_RotateClassic(FIBITMAP *dib, double angle); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_RotateEx(FIBITMAP *dib, double angle, double x_shift, double y_shift, double x_origin, double y_origin, BOOL use_mask); +DLL_API BOOL DLL_CALLCONV FreeImage_FlipHorizontal(FIBITMAP *dib); +DLL_API BOOL DLL_CALLCONV FreeImage_FlipVertical(FIBITMAP *dib); +DLL_API BOOL DLL_CALLCONV FreeImage_JPEGTransform(const char *src_file, const char *dst_file, FREE_IMAGE_JPEG_OPERATION operation, BOOL perfect FI_DEFAULT(FALSE)); + +// upsampling / downsampling +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Rescale(FIBITMAP *dib, int dst_width, int dst_height, FREE_IMAGE_FILTER filter); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_MakeThumbnail(FIBITMAP *dib, int max_pixel_size, BOOL convert FI_DEFAULT(TRUE)); + +// color manipulation routines (point operations) +DLL_API BOOL DLL_CALLCONV FreeImage_AdjustCurve(FIBITMAP *dib, BYTE *LUT, FREE_IMAGE_COLOR_CHANNEL channel); +DLL_API BOOL DLL_CALLCONV FreeImage_AdjustGamma(FIBITMAP *dib, double gamma); +DLL_API BOOL DLL_CALLCONV FreeImage_AdjustBrightness(FIBITMAP *dib, double percentage); +DLL_API BOOL DLL_CALLCONV FreeImage_AdjustContrast(FIBITMAP *dib, double percentage); +DLL_API BOOL DLL_CALLCONV FreeImage_Invert(FIBITMAP *dib); +DLL_API BOOL DLL_CALLCONV FreeImage_GetHistogram(FIBITMAP *dib, DWORD *histo, FREE_IMAGE_COLOR_CHANNEL channel FI_DEFAULT(FICC_BLACK)); +DLL_API int DLL_CALLCONV FreeImage_GetAdjustColorsLookupTable(BYTE *LUT, double brightness, double contrast, double gamma, BOOL invert); +DLL_API BOOL DLL_CALLCONV FreeImage_AdjustColors(FIBITMAP *dib, double brightness, double contrast, double gamma, BOOL invert FI_DEFAULT(FALSE)); +DLL_API unsigned DLL_CALLCONV FreeImage_ApplyColorMapping(FIBITMAP *dib, RGBQUAD *srccolors, RGBQUAD *dstcolors, unsigned count, BOOL ignore_alpha, BOOL swap); +DLL_API unsigned DLL_CALLCONV FreeImage_SwapColors(FIBITMAP *dib, RGBQUAD *color_a, RGBQUAD *color_b, BOOL ignore_alpha); +DLL_API unsigned DLL_CALLCONV FreeImage_ApplyPaletteIndexMapping(FIBITMAP *dib, BYTE *srcindices, BYTE *dstindices, unsigned count, BOOL swap); +DLL_API unsigned DLL_CALLCONV FreeImage_SwapPaletteIndices(FIBITMAP *dib, BYTE *index_a, BYTE *index_b); + +// channel processing routines +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_GetChannel(FIBITMAP *dib, FREE_IMAGE_COLOR_CHANNEL channel); +DLL_API BOOL DLL_CALLCONV FreeImage_SetChannel(FIBITMAP *dib, FIBITMAP *dib8, FREE_IMAGE_COLOR_CHANNEL channel); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_GetComplexChannel(FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel); +DLL_API BOOL DLL_CALLCONV FreeImage_SetComplexChannel(FIBITMAP *dst, FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel); + +// copy / paste / composite routines +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Copy(FIBITMAP *dib, int left, int top, int right, int bottom); +DLL_API BOOL DLL_CALLCONV FreeImage_Paste(FIBITMAP *dst, FIBITMAP *src, int left, int top, int alpha); +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Composite(FIBITMAP *fg, BOOL useFileBkg FI_DEFAULT(FALSE), RGBQUAD *appBkColor FI_DEFAULT(NULL), FIBITMAP *bg FI_DEFAULT(NULL)); +DLL_API BOOL DLL_CALLCONV FreeImage_JPEGCrop(const char *src_file, const char *dst_file, int left, int top, int right, int bottom); +DLL_API BOOL DLL_CALLCONV FreeImage_PreMultiplyWithAlpha(FIBITMAP *dib); + +// miscellaneous algorithms +DLL_API FIBITMAP *DLL_CALLCONV FreeImage_MultigridPoissonSolver(FIBITMAP *Laplacian, int ncycle FI_DEFAULT(3)); + +// restore the borland-specific enum size option +#if defined(__BORLANDC__) +#pragma option pop +#endif + +#ifdef __cplusplus +} +#endif + +#endif // FREEIMAGE_H diff --git a/libs/FreeImage/freeimage.s.lib b/libs/FreeImage/freeimage.s.lib new file mode 100755 index 00000000..ca3c5002 Binary files /dev/null and b/libs/FreeImage/freeimage.s.lib differ diff --git a/libs/FreeImage/libfreeimage.a b/libs/FreeImage/libfreeimage.a new file mode 100644 index 00000000..1b0c1ec5 Binary files /dev/null and b/libs/FreeImage/libfreeimage.a differ diff --git a/libs/FreeImage/libfreeimage.s.a b/libs/FreeImage/libfreeimage.s.a new file mode 100755 index 00000000..e6863c3f Binary files /dev/null and b/libs/FreeImage/libfreeimage.s.a differ diff --git a/opj_configure.h.in b/opj_configure.h.in new file mode 100644 index 00000000..316e2f67 --- /dev/null +++ b/opj_configure.h.in @@ -0,0 +1,16 @@ +/* + * here is where system comupted values get stored these values should only + * change when the target compile platform changes + */ + +/* what byte order */ +#ifndef __OPJ_CONFIGURE_H +#define __OPJ_CONFIGURE_H +#cmakedefine CMAKE_WORDS_BIGENDIAN +#ifdef CMAKE_WORDS_BIGENDIAN + #define OPJ_BIG_ENDIAN +#else + #define OPJ_LITTLE_ENDIAN +#endif + +#endif /* __OPJ_CONFIGURE_H */ diff --git a/test_Free_image_V2_tile_handling/CMakeLists.txt b/test_Free_image_V2_tile_handling/CMakeLists.txt new file mode 100644 index 00000000..426271f9 --- /dev/null +++ b/test_Free_image_V2_tile_handling/CMakeLists.txt @@ -0,0 +1,29 @@ +# Build the demo app, small examples + +# First thing define the common source: + +# Then check if getopt is present: +INCLUDE (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake) + +# Headers file are located here: +INCLUDE_DIRECTORIES( + ${OPENJPEG_SOURCE_DIR}/libopenjpeg + ) + +# Do the proper thing when building static...if only there was configured +# headers or def files instead +IF(NOT BUILD_SHARED_LIBS) + ADD_DEFINITIONS(-DOPJ_STATIC) +ENDIF(NOT BUILD_SHARED_LIBS) + +INCLUDE(${OPENJPEG_SOURCE_DIR}/Free_CMakeImport.cmake) + +# Loop over all executables: +FOREACH(exe test2_encoder test2_decoder) + + ADD_EXECUTABLE(${exe} ${exe}.c) + TARGET_LINK_LIBRARIES(${exe} ${OPJ_PREFIX}openjpeg) + # Install exe + INSTALL_TARGETS(/bin/ ${exe}) +ENDFOREACH(exe) + diff --git a/test_Free_image_V2_tile_handling/test2_decoder.c b/test_Free_image_V2_tile_handling/test2_decoder.c new file mode 100644 index 00000000..aee4949a --- /dev/null +++ b/test_Free_image_V2_tile_handling/test2_decoder.c @@ -0,0 +1,367 @@ +/* + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define USE_OPJ_DEPRECATED +/* set this macro to enable profiling for the given test */ +/* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */ +//#define _PROFILE +#include "openjpeg.h" +#include "FreeImage.h" +#ifdef WIN32 +#include +#endif +#include +#include + +#define NB_EXTENSIONS 2 +/* -------------------------------------------------------------------------- */ +struct opj_format +{ + const char * m_extension; + OPJ_CODEC_FORMAT m_format; +}; + +const struct opj_format c_extensions[] = +{ + {".j2k",CODEC_J2K}, + {".jp2",CODEC_JP2} +}; + +OPJ_CODEC_FORMAT get_format (const char * l_file_name) +{ + OPJ_INT32 i; + const struct opj_format * l_current = c_extensions; + for + (i=0;im_extension,l_file_name + strlen(l_file_name)-4,4)) + { + return l_current->m_format; + } + ++l_current; + } + return CODEC_UNKNOWN; +} + +/** +sample error callback expecting a FILE* client object +*/ +void error_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[ERROR] %s", msg); +} +/** +sample warning callback expecting a FILE* client object +*/ +void warning_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[WARNING] %s", msg); +} +/** +sample error debug callback expecting no client object +*/ +void error_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); +} +/** +sample warning debug callback expecting no client object +*/ +void warning_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); +} +/** +sample debug callback expecting no client object +*/ +void info_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); +} + +/* -------------------------------------------------------------------------- */ + +int main (int argc,char * argv []) +{ + opj_dparameters_t l_param; + opj_codec_t * l_codec; + opj_image_t * l_image; + FILE * l_file; + opj_stream_t * l_stream; + OPJ_UINT32 l_data_size; + OPJ_UINT32 l_max_data_size = 1000; + OPJ_UINT32 l_tile_index; + OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000); + bool l_go_on = true; + OPJ_INT32 l_tile_x0,l_tile_y0; + OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps; + OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1; + char * l_input_file,* l_output_file; + OPJ_INT32 l_min_x, l_min_y, l_max_x, l_max_y; + OPJ_CODEC_FORMAT l_codec_format; + FIBITMAP * l_bitmap; + char * l_image_data; + OPJ_INT32 l_req_x,l_req_y; + OPJ_UINT32 l_image_width,l_image_height,l_image_boundary,l_offset; + char * l_tile_ptr [3]; + char * l_line_ptr, * l_current_ptr; + OPJ_UINT32 i,j; + + if + (argc != 8) + { + printf("usage : ... \n"); + return 1; + } + + PROFINIT(); + FreeImage_Initialise(0); + + l_input_file = argv[1]; + l_output_file = argv[2]; + l_min_x = atoi(argv[3]); + l_min_y = atoi(argv[4]); + l_max_x = atoi(argv[5]); + l_max_y = atoi(argv[6]); + + l_codec_format = get_format(l_input_file); + + if + ((! l_data) || (l_codec_format == CODEC_UNKNOWN)) + { + return 1; + } + + + opj_set_default_decoder_parameters(&l_param); + + /** you may here add custom decoding parameters */ + /* do not use layer decoding limitations */ + l_param.cp_layer = 0; + + /* do not use resolutions reductions */ + l_param.cp_reduce = atoi(argv[7]); + + /* to decode only a part of the image data */ + //opj_restrict_decoding(&l_param,0,0,1000,1000); + + l_codec = opj_create_decompress(l_codec_format); + if + (! l_codec) + { + free(l_data); + return 1; + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + opj_restrict_decoding(&l_param,l_min_x, l_min_y, l_max_x, l_max_y); + + if + (! opj_setup_decoder(l_codec,&l_param)) + { + free(l_data); + opj_destroy_codec(l_codec); + return 1; + } + + l_file = fopen(l_input_file,"rb"); + if + (! l_file) + { + fprintf(stdout, "Error opening input file\n"); + free(l_data); + opj_destroy_codec(l_codec); + return 1; + } + + l_stream = opj_stream_create_default_file_stream(l_file,true); + + if + (! opj_read_header(l_codec, + &l_image, + &l_tile_x0, + &l_tile_y0, + &l_tile_width, + &l_tile_height, + &l_nb_tiles_x, + &l_nb_tiles_y, + l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + return 1; + } + + if + ((l_tile_x0 != 0) || (l_tile_y0 != 0)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + return 1; + } + + l_min_x -= l_min_x % l_tile_width; + l_min_y -= l_min_y % l_tile_height; + + l_req_x = l_max_x % l_tile_width; + if + (l_req_x) + { + l_max_x += l_tile_width - l_req_x; + } + + l_req_y = l_max_y % l_tile_height; + if + (l_req_y) + { + l_max_y += l_tile_height - l_req_y; + } + + l_min_x = l_min_x < l_image->x0 ? l_image->x0 : l_min_x; + l_min_y = l_min_y < l_image->y0 ? l_image->y0 : l_min_y; + l_max_x = l_max_x > l_image->x1 ? l_image->x1 : l_max_x; + l_max_y = l_max_y > l_image->y1 ? l_image->y1 : l_max_y; + + l_image_width = (l_max_x - l_min_x + (1 << l_param.cp_reduce) - 1) >> l_param.cp_reduce; + l_image_height = (l_max_y - l_min_y + (1 << l_param.cp_reduce) - 1) >> l_param.cp_reduce; + l_image_boundary = 3 * l_image_width; + l_req_x = l_image_boundary % 4; + if + (l_req_x) + { + l_image_boundary += 4-l_req_x; + } + + l_bitmap = FreeImage_Allocate(l_image_width, l_image_height, 24, 0, 0, 0); + l_image_data = FreeImage_GetBits(l_bitmap); + + while + (l_go_on) + { + if + (! opj_read_tile_header( + l_codec, + &l_tile_index, + &l_data_size, + &l_current_tile_x0, + &l_current_tile_y0, + &l_current_tile_x1, + &l_current_tile_y1, + &l_nb_comps, + &l_go_on, + l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + if + (l_go_on) + { + if + (l_data_size > l_max_data_size) + { + l_data = (OPJ_BYTE *) realloc(l_data,l_data_size); + if + (! l_data) + { + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + l_max_data_size = l_data_size; + } + + if + (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + /** now should inspect image to know the reduction factor and then how to behave with data */ + l_offset = (((l_max_y - l_current_tile_y0 + (1 << l_param.cp_reduce) - 1)>>l_param.cp_reduce) - 1) * l_image_boundary + ((l_current_tile_x0 - l_min_x + (1 << l_param.cp_reduce) - 1)>> l_param.cp_reduce )* 3; + + l_tile_width = (l_current_tile_x1 - l_current_tile_x0 + (1 << l_param.cp_reduce) - 1)>>l_param.cp_reduce; + l_tile_height = (l_current_tile_y1 - l_current_tile_y0 + (1 << l_param.cp_reduce) - 1)>>l_param.cp_reduce; + l_tile_ptr[0] = l_data + 2 * l_tile_width * l_tile_height; + l_tile_ptr[1] = l_data + l_tile_width * l_tile_height; + l_tile_ptr[2] = l_data ; + l_line_ptr = l_image_data + l_offset; + for + (i=0;i + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#define USE_OPJ_DEPRECATED +/* set this macro to enable profiling for the given test */ +/* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */ +//#define _PROFILE + +#include "openjpeg.h" +#include +#include +#include + +/* -------------------------------------------------------------------------- */ + +/** +sample error callback expecting a FILE* client object +*/ +void error_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[ERROR] %s", msg); +} +/** +sample warning callback expecting a FILE* client object +*/ +void warning_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[WARNING] %s", msg); +} +/** +sample error debug callback expecting no client object +*/ +void error_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); +} +/** +sample warning debug callback expecting no client object +*/ +void warning_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); +} +/** +sample debug callback expecting no client object +*/ +void info_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); +} + +struct opj_format +{ + const char * m_extension; + OPJ_CODEC_FORMAT m_format; +}; + +const struct opj_format c_extensions[] = +{ + {".j2k",CODEC_J2K}, + {".jp2",CODEC_JP2} +}; + +#define NB_EXTENSIONS 2 +OPJ_CODEC_FORMAT get_format (const char * l_file_name) +{ + OPJ_INT32 i; + const struct opj_format * l_current = c_extensions; + for + (i=0;im_extension,l_file_name + strlen(l_file_name)-4,4)) + { + return l_current->m_format; + } + ++l_current; + } + return CODEC_UNKNOWN; +} + +/* -------------------------------------------------------------------------- */ + +int main (int argc, char * argv []) +{ + opj_cparameters_t l_param; + opj_codec_t * l_codec; + opj_image_t * l_image; + opj_image_cmptparm_t l_params [3]; + FILE * l_file; + opj_stream_t * l_stream; + opj_image_cmptparm_t * l_current_param_ptr; + OPJ_UINT32 i,j,k,l; + OPJ_BYTE *l_tile_data,*l_line_ptr,*l_current_ptr; + OPJ_BYTE * l_tile_ptr [3]; + OPJ_UINT32 l_tile_width,l_image_width,l_image_height,l_chunk_size,l_image_boundary,l_req_x,l_req_y,l_nb_tiles_x,l_nb_tiles_y,l_current_tile_nb,l_data_size; + OPJ_UINT32 l_offset; + + OPJ_CODEC_FORMAT l_codec_format; + FIBITMAP * l_bitmap; + FREE_IMAGE_FORMAT l_input_format; + char * l_image_data; + char * l_input_file,*l_output_file; + if + (argc != 6) + { + printf("usage \n"); + return 1; + } + + l_input_file = argv[1]; + l_output_file = argv[2]; + l_tile_width = atoi(argv[3]); + + FreeImage_Initialise(0); + + l_codec_format = get_format(l_output_file); + if + (l_codec_format == CODEC_UNKNOWN) + { + return 1; + } + + l_input_format = FreeImage_GetFileType(l_input_file,0); + if + (l_input_format == -1) + { + return 1; + } + l_bitmap = FreeImage_Load(l_input_format,l_input_file,0); + l_image_data = FreeImage_GetBits(l_bitmap); + l_image_width = FreeImage_GetWidth(l_bitmap); + l_image_height = FreeImage_GetHeight(l_bitmap); + l_chunk_size = FreeImage_GetBPP(l_bitmap); + l_chunk_size /= 8; + + if + (l_chunk_size < 3) + { + return 1; + } + l_image_boundary = l_image_width * l_chunk_size; + + l_req_x = l_image_boundary % 4; + if + (l_req_x) + { + l_image_boundary += 4 - l_req_x; + } + + l_tile_data = (OPJ_BYTE*) malloc(l_tile_width * l_tile_width * 3); + + l_nb_tiles_x = l_image_width / l_tile_width; + l_req_x = l_image_width % l_tile_width; + l_nb_tiles_y = l_image_height / l_tile_width; + l_req_y = l_image_height % l_tile_width; + + opj_set_default_encoder_parameters(&l_param); + /** you may here add custom encoding parameters */ + /* rate specifications */ + /** number of quality layers in the stream */ + l_param.tcp_numlayers = 1; + l_param.cp_fixed_quality = 1; + /* is using others way of calculation */ + /* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */ + /* l_param.tcp_rates[0] = ... */ + + + /* tile definitions parameters */ + /* position of the tile grid aligned with the image */ + l_param.cp_tx0 = 0; + l_param.cp_ty0 = 0; + /* tile size, we are using tile based encoding */ + l_param.tile_size_on = true; + l_param.cp_tdx = l_tile_width; + l_param.cp_tdy = l_tile_width; + + /* use irreversible encoding ?*/ + l_param.irreversible = atoi(argv[5]); + + /* do not bother with mct, the rsiz is set when calling opj_set_MCT*/ + /*l_param.cp_rsiz = STD_RSIZ;*/ + + /* no cinema */ + /*l_param.cp_cinema = 0;*/ + + /* no not bother using SOP or EPH markers, do not use custom size precinct */ + /* number of precincts to specify */ + /* l_param.csty = 0;*/ + /* l_param.res_spec = ... */ + /* l_param.prch_init[i] = .. */ + /* l_param.prcw_init[i] = .. */ + + + /* do not use progression order changes */ + /*l_param.numpocs = 0;*/ + /* l_param.POC[i].... */ + + /* do not restrain the size for a component.*/ + /* l_param.max_comp_size = 0; */ + + /** block encoding style for each component, do not use at the moment */ + /** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */ + /* l_param.mode = 0;*/ + + /** number of resolutions */ + l_param.numresolution = atoi(argv[4]); + + /** progression order to use*/ + /** LRCP, RLCP, RPCL, PCRL, CPRL */ + l_param.prog_order = LRCP; + + /** no "region" of interest, more precisally component */ + /* l_param.roi_compno = -1; */ + /* l_param.roi_shift = 0; */ + + /* we are not using multiple tile parts for a tile. */ + /* l_param.tp_on = 0; */ + /* l_param.tp_flag = 0; */ + + l_param.tcp_mct = 1; + /* if we are using mct */ + /* opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS); */ + + + /* image definition */ + l_current_param_ptr = l_params; + for + (i=0;i<3;++i) + { + /* do not bother bpp useless */ + /*l_current_param_ptr->bpp = COMP_PREC;*/ + l_current_param_ptr->dx = 1; + l_current_param_ptr->dy = 1; + l_current_param_ptr->h = l_image_height; + l_current_param_ptr->sgnd = 0; + l_current_param_ptr->prec = 8; + l_current_param_ptr->w = l_image_width; + l_current_param_ptr->x0 = 0; + l_current_param_ptr->y0 = 0; + ++l_current_param_ptr; + } + + l_codec = opj_create_compress(l_codec_format); + if + (! l_codec) + { + return 1; + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + + l_image = opj_image_tile_create(3,l_params,CLRSPC_SRGB); + if + (! l_image) + { + opj_destroy_codec(l_codec); + return 1; + } + l_image->x0 = 0; + l_image->y0 = 0; + l_image->x1 = l_image_width; + l_image->y1 = l_image_height; + l_image->color_space = CLRSPC_SRGB; + + if + (! opj_setup_encoder(l_codec,&l_param,l_image)) + { + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + + l_file = fopen(l_output_file,"wb"); + if + (! l_file) + { + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + + l_stream = opj_stream_create_default_file_stream(l_file,false); + + if + (! opj_start_compress(l_codec,l_image,l_stream)) + { + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + + l_current_tile_nb = 0; + + for + (i=0;i + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#define USE_OPJ_DEPRECATED +/* set this macro to enable profiling for the given test */ +/* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */ +//#define _PROFILE + +#include "openjpeg.h" +#ifdef WIN32 +#include +#endif +#define DA_X0 0 +#define DA_Y0 0 +#define DA_X1 1000 +#define DA_Y1 1000 +#define INPUT_FILE "test.j2k" + +/* -------------------------------------------------------------------------- */ + +/** +sample error callback expecting a FILE* client object +*/ +void error_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[ERROR] %s", msg); +} +/** +sample warning callback expecting a FILE* client object +*/ +void warning_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[WARNING] %s", msg); +} +/** +sample error debug callback expecting no client object +*/ +void error_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); +} +/** +sample warning debug callback expecting no client object +*/ +void warning_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); +} +/** +sample debug callback expecting no client object +*/ +void info_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); +} + +/* -------------------------------------------------------------------------- */ + +int main () +{ + opj_dparameters_t l_param; + opj_codec_t * l_codec; + opj_image_t * l_image; + FILE * l_file; + opj_stream_t * l_stream; + OPJ_UINT32 l_data_size; + OPJ_UINT32 l_max_data_size = 1000; + OPJ_UINT32 l_tile_index; + OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000); + bool l_go_on = true; + OPJ_INT32 l_tile_x0,l_tile_y0; + OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps; + OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1; + + PROFINIT(); + + + if + (! l_data) + { + return 1; + } + opj_set_default_decoder_parameters(&l_param); + + /** you may here add custom decoding parameters */ + /* do not use layer decoding limitations */ + l_param.cp_layer = 0; + + /* do not use resolutions reductions */ + l_param.cp_reduce = 0; + + /* to decode only a part of the image data */ + //opj_restrict_decoding(&l_param,0,0,1000,1000); + + l_codec = opj_create_decompress(CODEC_J2K); + if + (! l_codec) + { + free(l_data); + return 1; + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + + if + (! opj_setup_decoder(l_codec,&l_param)) + { + free(l_data); + opj_destroy_codec(l_codec); + return 1; + } + + l_file = fopen(INPUT_FILE,"rb"); + if + (! l_file) + { + fprintf(stdout, "Error opening input file\n"); + free(l_data); + opj_destroy_codec(l_codec); + return 1; + } + + l_stream = opj_stream_create_default_file_stream(l_file,true); + + if + (! opj_read_header(l_codec, + &l_image, + &l_tile_x0, + &l_tile_y0, + &l_tile_width, + &l_tile_height, + &l_nb_tiles_x, + &l_nb_tiles_y, + l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + return 1; + } + printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1); + opj_set_decode_area(l_codec, DA_X0, DA_Y0, DA_X1, DA_Y1); + while + (l_go_on) + { + if + (! opj_read_tile_header( + l_codec, + &l_tile_index, + &l_data_size, + &l_current_tile_x0, + &l_current_tile_y0, + &l_current_tile_x1, + &l_current_tile_y1, + &l_nb_comps, + &l_go_on, + l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + if + (l_go_on) + { + if + (l_data_size > l_max_data_size) + { + l_data = (OPJ_BYTE *) realloc(l_data,l_data_size); + if + (! l_data) + { + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + l_max_data_size = l_data_size; + } + + if + (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + /** now should inspect image to know the reduction factor and then how to behave with data */ + } + } + if + (! opj_end_decompress(l_codec,l_stream)) + { + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + free(l_data); + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + + // Print profiling + PROFPRINT(); + + return 0; +} diff --git a/test_V2_tile_handling/test_encoder.c b/test_V2_tile_handling/test_encoder.c new file mode 100644 index 00000000..f96a7886 --- /dev/null +++ b/test_V2_tile_handling/test_encoder.c @@ -0,0 +1,299 @@ +/* + * Copyright (c) 2008, Jerome Fimes, Communications & Systemes + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#define USE_OPJ_DEPRECATED + +#include "openjpeg.h" +/* set this macro to enable profiling for the given test */ +/* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */ +//#define _PROFILE + +#ifdef WIN32 +#include "windows.h" // needed for rand() function +#endif + + +#define NUM_COMPS 3 +#define IMAGE_WIDTH 2000 +#define IMAGE_HEIGHT 2000 +#define TILE_WIDTH 1000 +#define TILE_HEIGHT 1000 +#define COMP_PREC 8 +#define OUTPUT_FILE "test.j2k" + +/* -------------------------------------------------------------------------- */ + +/** +sample error callback expecting a FILE* client object +*/ +void error_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[ERROR] %s", msg); +} +/** +sample warning callback expecting a FILE* client object +*/ +void warning_callback_file(const char *msg, void *client_data) { + FILE *stream = (FILE*)client_data; + fprintf(stream, "[WARNING] %s", msg); +} +/** +sample error debug callback expecting no client object +*/ +void error_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); +} +/** +sample warning debug callback expecting no client object +*/ +void warning_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); +} +/** +sample debug callback expecting no client object +*/ +void info_callback(const char *msg, void *client_data) { + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); +} + +/* -------------------------------------------------------------------------- */ + +int main () +{ + opj_cparameters_t l_param; + opj_codec_t * l_codec; + opj_image_t * l_image; + opj_image_cmptparm_t l_params [NUM_COMPS]; + FILE * l_file; + opj_stream_t * l_stream; + OPJ_UINT32 l_nb_tiles = (IMAGE_WIDTH/TILE_WIDTH) * (IMAGE_HEIGHT/TILE_HEIGHT); + OPJ_UINT32 l_data_size = TILE_WIDTH * TILE_HEIGHT * NUM_COMPS * (COMP_PREC/8); + + const OPJ_FLOAT32 l_mct [] = + { + 1 , 0 , 0 , + 0 , 1 , 0 , + 0 , 0 , 1 + }; + + const OPJ_INT32 l_offsets [] = + { + 128 , 128 , 128 + }; + + opj_image_cmptparm_t * l_current_param_ptr; + OPJ_UINT32 i; + OPJ_BYTE *l_data; + + PROFINIT(); + l_data = (OPJ_BYTE*) malloc(TILE_WIDTH * TILE_HEIGHT * NUM_COMPS * (COMP_PREC/8) * sizeof(OPJ_BYTE)); + + fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n"); + for + (i=0;ibpp = COMP_PREC;*/ + l_current_param_ptr->dx = 1; + l_current_param_ptr->dy = 1; + l_current_param_ptr->h = IMAGE_HEIGHT; + l_current_param_ptr->sgnd = 0; + l_current_param_ptr->prec = COMP_PREC; + l_current_param_ptr->w = IMAGE_WIDTH; + l_current_param_ptr->x0 = 0; + l_current_param_ptr->y0 = 0; + ++l_current_param_ptr; + } + + l_codec = opj_create_compress(CODEC_J2K); + if + (! l_codec) + { + return 1; + } + + /* catch events using our callbacks and give a local context */ + opj_set_info_handler(l_codec, info_callback,00); + opj_set_warning_handler(l_codec, warning_callback,00); + opj_set_error_handler(l_codec, error_callback,00); + + l_image = opj_image_tile_create(NUM_COMPS,l_params,CLRSPC_SRGB); + if + (! l_image) + { + opj_destroy_codec(l_codec); + return 1; + } + l_image->x0 = 0; + l_image->y0 = 0; + l_image->x1 = IMAGE_WIDTH; + l_image->y1 = IMAGE_HEIGHT; + l_image->color_space = CLRSPC_SRGB; + + if + (! opj_setup_encoder(l_codec,&l_param,l_image)) + { + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + + l_file = fopen(OUTPUT_FILE,"wb"); + if + (! l_file) + { + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + + l_stream = opj_stream_create_default_file_stream(l_file,false); + + if + (! opj_start_compress(l_codec,l_image,l_stream)) + { + opj_stream_destroy(l_stream); + fclose(l_file); + opj_destroy_codec(l_codec); + opj_image_destroy(l_image); + return 1; + } + for + (i=0;i