JavaOpenJPEGDecoder.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2014, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  7. * Copyright (c) 2003-2014, Antonin Descampe
  8. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  9. * Copyright (c) 2006-2007, Parvatha Elangovan
  10. * Copyright (c) 2007, Patrick Piscaglia (Telemis)
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <jni.h>
  38. #include <math.h>
  39. #include "openjpeg.h"
  40. #include "opj_includes.h"
  41. #include "opj_getopt.h"
  42. #include "convert.h"
  43. #include "dirent.h"
  44. #include "org_openJpeg_OpenJPEGJavaDecoder.h"
  45. #ifndef _WIN32
  46. #define stricmp strcasecmp
  47. #define strnicmp strncasecmp
  48. #endif
  49. #include "format_defs.h"
  50. typedef struct callback_variables {
  51. JNIEnv *env;
  52. /** 'jclass' object used to call a Java method from the C */
  53. jobject *jobj;
  54. /** 'jclass' object used to call a Java method from the C */
  55. jmethodID message_mid;
  56. jmethodID error_mid;
  57. } callback_variables_t;
  58. typedef struct dircnt{
  59. /** Buffer for holding images read from Directory*/
  60. char *filename_buf;
  61. /** Pointer to the buffer*/
  62. char **filename;
  63. }dircnt_t;
  64. typedef struct img_folder{
  65. /** The directory path of the folder containing input images*/
  66. char *imgdirpath;
  67. /** Output format*/
  68. char *out_format;
  69. /** Enable option*/
  70. char set_imgdir;
  71. /** Enable Cod Format for output*/
  72. char set_out_format;
  73. }img_fol_t;
  74. void decode_help_display() {
  75. fprintf(stdout,"HELP\n----\n\n");
  76. fprintf(stdout,"- the -h option displays this help information on screen\n\n");
  77. /* UniPG>> */
  78. fprintf(stdout,"List of parameters for the JPEG 2000 "
  79. #ifdef USE_JPWL
  80. "+ JPWL "
  81. #endif /* USE_JPWL */
  82. "decoder:\n");
  83. /* <<UniPG */
  84. fprintf(stdout,"\n");
  85. fprintf(stdout,"\n");
  86. fprintf(stdout," -ImgDir \n");
  87. fprintf(stdout," Image file Directory path \n");
  88. fprintf(stdout," -OutFor \n");
  89. fprintf(stdout," REQUIRED only if -ImgDir is used\n");
  90. fprintf(stdout," Need to specify only format without filename <BMP> \n");
  91. fprintf(stdout," Currently accepts PGM, PPM, PNM, PGX, BMP format\n");
  92. fprintf(stdout," -i <compressed file>\n");
  93. fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
  94. fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
  95. fprintf(stdout," is identified based on its suffix.\n");
  96. fprintf(stdout," -o <decompressed file>\n");
  97. fprintf(stdout," REQUIRED\n");
  98. fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
  99. fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
  100. fprintf(stdout," filename is given, there will be as many output files as there are\n");
  101. fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
  102. fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
  103. fprintf(stdout," is given and there are more than one component, only the first component\n");
  104. fprintf(stdout," will be written to the file.\n");
  105. fprintf(stdout," -r <reduce factor>\n");
  106. fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
  107. fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
  108. fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
  109. fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
  110. fprintf(stdout," -l <number of quality layers to decode>\n");
  111. fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
  112. fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
  113. fprintf(stdout," are decoded.\n");
  114. /* UniPG>> */
  115. #ifdef USE_JPWL
  116. fprintf(stdout," -W <options>\n");
  117. fprintf(stdout," Activates the JPWL correction capability, if the codestream complies.\n");
  118. fprintf(stdout," Options can be a comma separated list of <param=val> tokens:\n");
  119. fprintf(stdout," c, c=numcomps\n");
  120. fprintf(stdout," numcomps is the number of expected components in the codestream\n");
  121. fprintf(stdout," (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
  122. #endif /* USE_JPWL */
  123. /* <<UniPG */
  124. fprintf(stdout,"\n");
  125. }
  126. /* -------------------------------------------------------------------------- */
  127. int get_num_images(char *imgdirpath){
  128. DIR *dir;
  129. struct dirent* content;
  130. int num_images = 0;
  131. /*Reading the input images from given input directory*/
  132. dir= opendir(imgdirpath);
  133. if(!dir){
  134. fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
  135. return 0;
  136. }
  137. while((content=readdir(dir))!=NULL){
  138. if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
  139. continue;
  140. num_images++;
  141. }
  142. return num_images;
  143. }
  144. int load_images(dircnt_t *dirptr, char *imgdirpath){
  145. DIR *dir;
  146. struct dirent* content;
  147. int i = 0;
  148. /*Reading the input images from given input directory*/
  149. dir= opendir(imgdirpath);
  150. if(!dir){
  151. fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
  152. return 1;
  153. }else {
  154. fprintf(stderr,"Folder opened successfully\n");
  155. }
  156. while((content=readdir(dir))!=NULL){
  157. if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
  158. continue;
  159. strcpy(dirptr->filename[i],content->d_name);
  160. i++;
  161. }
  162. return 0;
  163. }
  164. int get_file_format(char *filename) {
  165. unsigned int i;
  166. static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "j2k", "jp2", "jpt", "j2c" };
  167. static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT };
  168. char * ext = strrchr(filename, '.');
  169. if (ext == NULL)
  170. return -1;
  171. ext++;
  172. if(ext) {
  173. for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
  174. if(strnicmp(ext, extension[i], 3) == 0) {
  175. return format[i];
  176. }
  177. }
  178. }
  179. return -1;
  180. }
  181. /* -------------------------------------------------------------------------- */
  182. int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
  183. /* parse the command line */
  184. int totlen;
  185. opj_option_t long_option[]={
  186. {"ImgDir",REQ_ARG, NULL ,'y'},
  187. {"OutFor",REQ_ARG, NULL ,'O'},
  188. };
  189. /* UniPG>> */
  190. const char optlist[] = "i:o:r:l:hx:"
  191. #ifdef USE_JPWL
  192. "W:"
  193. #endif /* USE_JPWL */
  194. ;
  195. /*for (i=0; i<argc; i++) {
  196. printf("[%s]",argv[i]);
  197. }
  198. printf("\n");*/
  199. /* <<UniPG */
  200. totlen=sizeof(long_option);
  201. img_fol->set_out_format = 0;
  202. reset_options_reading();
  203. while (1) {
  204. int c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
  205. if (c == -1)
  206. break;
  207. switch (c) {
  208. case 'i': /* input file */
  209. {
  210. char *infile = opj_optarg;
  211. parameters->decod_format = get_file_format(infile);
  212. switch(parameters->decod_format) {
  213. case J2K_CFMT:
  214. case JP2_CFMT:
  215. case JPT_CFMT:
  216. break;
  217. default:
  218. fprintf(stderr,
  219. "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
  220. infile);
  221. return 1;
  222. }
  223. strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
  224. }
  225. break;
  226. /* ----------------------------------------------------- */
  227. case 'o': /* output file */
  228. {
  229. char *outfile = opj_optarg;
  230. parameters->cod_format = get_file_format(outfile);
  231. switch(parameters->cod_format) {
  232. case PGX_DFMT:
  233. case PXM_DFMT:
  234. case BMP_DFMT:
  235. case TIF_DFMT:
  236. case RAW_DFMT:
  237. case TGA_DFMT:
  238. break;
  239. default:
  240. fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
  241. return 1;
  242. }
  243. strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
  244. }
  245. break;
  246. /* ----------------------------------------------------- */
  247. case 'O': /* output format */
  248. {
  249. char outformat[50];
  250. char *of = opj_optarg;
  251. sprintf(outformat,".%s",of);
  252. img_fol->set_out_format = 1;
  253. parameters->cod_format = get_file_format(outformat);
  254. switch(parameters->cod_format) {
  255. case PGX_DFMT:
  256. img_fol->out_format = "pgx";
  257. break;
  258. case PXM_DFMT:
  259. img_fol->out_format = "ppm";
  260. break;
  261. case BMP_DFMT:
  262. img_fol->out_format = "bmp";
  263. break;
  264. case TIF_DFMT:
  265. img_fol->out_format = "tif";
  266. break;
  267. case RAW_DFMT:
  268. img_fol->out_format = "raw";
  269. break;
  270. case TGA_DFMT:
  271. img_fol->out_format = "raw";
  272. break;
  273. default:
  274. fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
  275. return 1;
  276. break;
  277. }
  278. }
  279. break;
  280. /* ----------------------------------------------------- */
  281. case 'r': /* reduce option */
  282. {
  283. sscanf(opj_optarg, "%d", &parameters->cp_reduce);
  284. }
  285. break;
  286. /* ----------------------------------------------------- */
  287. case 'l': /* layering option */
  288. {
  289. sscanf(opj_optarg, "%d", &parameters->cp_layer);
  290. }
  291. break;
  292. /* ----------------------------------------------------- */
  293. case 'h': /* display an help description */
  294. decode_help_display();
  295. return 1;
  296. /* ------------------------------------------------------ */
  297. case 'y': /* Image Directory path */
  298. {
  299. img_fol->imgdirpath = (char*)opj_malloc(strlen(opj_optarg) + 1);
  300. strcpy(img_fol->imgdirpath,opj_optarg);
  301. img_fol->set_imgdir=1;
  302. }
  303. break;
  304. /* ----------------------------------------------------- */
  305. /* UniPG>> */
  306. #ifdef USE_JPWL
  307. case 'W': /* activate JPWL correction */
  308. {
  309. char *token = NULL;
  310. token = strtok(opj_optarg, ",");
  311. while(token != NULL) {
  312. /* search expected number of components */
  313. if (*token == 'c') {
  314. static int compno;
  315. compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
  316. if(sscanf(token, "c=%d", &compno) == 1) {
  317. /* Specified */
  318. if ((compno < 1) || (compno > 256)) {
  319. fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
  320. return 1;
  321. }
  322. parameters->jpwl_exp_comps = compno;
  323. } else if (!strcmp(token, "c")) {
  324. /* default */
  325. parameters->jpwl_exp_comps = compno; /* auto for default size */
  326. } else {
  327. fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
  328. return 1;
  329. };
  330. }
  331. /* search maximum number of tiles */
  332. if (*token == 't') {
  333. static int tileno;
  334. tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
  335. if(sscanf(token, "t=%d", &tileno) == 1) {
  336. /* Specified */
  337. if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
  338. fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
  339. return 1;
  340. }
  341. parameters->jpwl_max_tiles = tileno;
  342. } else if (!strcmp(token, "t")) {
  343. /* default */
  344. parameters->jpwl_max_tiles = tileno; /* auto for default size */
  345. } else {
  346. fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
  347. return 1;
  348. };
  349. }
  350. /* next token or bust */
  351. token = strtok(NULL, ",");
  352. };
  353. parameters->jpwl_correct = true;
  354. fprintf(stdout, "JPWL correction capability activated\n");
  355. fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
  356. }
  357. break;
  358. #endif /* USE_JPWL */
  359. /* <<UniPG */
  360. /* ----------------------------------------------------- */
  361. default:
  362. fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
  363. break;
  364. }
  365. }
  366. /* No check for possible errors before the -i and -o options are of course not mandatory*/
  367. return 0;
  368. }
  369. /* -------------------------------------------------------------------------- */
  370. /**
  371. error callback returning the message to Java andexpecting a callback_variables_t client object
  372. */
  373. void error_callback(const char *msg, void *client_data) {
  374. callback_variables_t* vars = (callback_variables_t*) client_data;
  375. JNIEnv *env = vars->env;
  376. jstring jbuffer;
  377. jbuffer = (*env)->NewStringUTF(env, msg);
  378. (*env)->ExceptionClear(env);
  379. (*env)->CallVoidMethod(env, *(vars->jobj), vars->error_mid, jbuffer);
  380. if ((*env)->ExceptionOccurred(env)) {
  381. fprintf(stderr,"C: Exception during call back method\n");
  382. (*env)->ExceptionDescribe(env);
  383. (*env)->ExceptionClear(env);
  384. }
  385. (*env)->DeleteLocalRef(env, jbuffer);
  386. }
  387. /**
  388. warning callback returning the message to Java andexpecting a callback_variables_t client object
  389. */
  390. void warning_callback(const char *msg, void *client_data) {
  391. callback_variables_t* vars = (callback_variables_t*) client_data;
  392. JNIEnv *env = vars->env;
  393. jstring jbuffer;
  394. jbuffer = (*env)->NewStringUTF(env, msg);
  395. (*env)->ExceptionClear(env);
  396. (*env)->CallVoidMethod(env, *(vars->jobj), vars->message_mid, jbuffer);
  397. if ((*env)->ExceptionOccurred(env)) {
  398. fprintf(stderr,"C: Exception during call back method\n");
  399. (*env)->ExceptionDescribe(env);
  400. (*env)->ExceptionClear(env);
  401. }
  402. (*env)->DeleteLocalRef(env, jbuffer);
  403. }
  404. /**
  405. information callback returning the message to Java andexpecting a callback_variables_t client object
  406. */
  407. void info_callback(const char *msg, void *client_data) {
  408. callback_variables_t* vars = (callback_variables_t*) client_data;
  409. JNIEnv *env = vars->env;
  410. jstring jbuffer;
  411. jbuffer = (*env)->NewStringUTF(env, msg);
  412. (*env)->ExceptionClear(env);
  413. (*env)->CallVoidMethod(env, *(vars->jobj), vars->message_mid, jbuffer);
  414. if ((*env)->ExceptionOccurred(env)) {
  415. fprintf(stderr,"C: Exception during call back method\n");
  416. (*env)->ExceptionDescribe(env);
  417. (*env)->ExceptionClear(env);
  418. }
  419. (*env)->DeleteLocalRef(env, jbuffer);
  420. }
  421. /* --------------------------------------------------------------------------
  422. -------------------- MAIN METHOD, CALLED BY JAVA -----------------------*/
  423. JNIEXPORT jint JNICALL Java_org_openJpeg_OpenJPEGJavaDecoder_internalDecodeJ2KtoImage(JNIEnv *env, jobject obj, jobjectArray javaParameters) {
  424. int argc; /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
  425. char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
  426. opj_dparameters_t parameters; /* decompression parameters */
  427. img_fol_t img_fol;
  428. opj_event_mgr_t event_mgr; /* event manager */
  429. opj_image_t *image = NULL;
  430. FILE *fsrc = NULL;
  431. unsigned char *src = NULL;
  432. int file_length;
  433. int num_images;
  434. int i,j,imageno;
  435. opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
  436. opj_cio_t *cio = NULL;
  437. int w,h;
  438. long min_value, max_value;
  439. short tempS; unsigned char tempUC, tempUC1, tempUC2;
  440. /* ==> Access variables to the Java member variables*/
  441. jsize arraySize;
  442. jclass cls;
  443. jobject object;
  444. jboolean isCopy;
  445. jfieldID fid;
  446. jbyteArray jba;
  447. jshortArray jsa;
  448. jintArray jia;
  449. jbyte *jbBody, *ptrBBody;
  450. jshort *jsBody, *ptrSBody;
  451. jint *jiBody, *ptrIBody;
  452. callback_variables_t msgErrorCallback_vars;
  453. /* <=== access variable to Java member variables */
  454. int *ptr, *ptr1, *ptr2; /* <== To transfer the decoded image to Java*/
  455. /* configure the event callbacks */
  456. memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
  457. event_mgr.error_handler = error_callback;
  458. event_mgr.warning_handler = warning_callback;
  459. event_mgr.info_handler = info_callback;
  460. /* JNI reference to the calling class*/
  461. cls = (*env)->GetObjectClass(env, obj);
  462. /* Pointers to be able to call a Java method for all the info and error messages*/
  463. msgErrorCallback_vars.env = env;
  464. msgErrorCallback_vars.jobj = &obj;
  465. msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage", "(Ljava/lang/String;)V");
  466. msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError", "(Ljava/lang/String;)V");
  467. /* Get the String[] containing the parameters, and converts it into a char** to simulate command line arguments.*/
  468. arraySize = (*env)->GetArrayLength(env, javaParameters);
  469. argc = (int) arraySize +1;
  470. argv = opj_malloc(argc*sizeof(char*));
  471. argv[0] = "ProgramName.exe"; /* The program name: useless*/
  472. j=0;
  473. for (i=1; i<argc; i++) {
  474. object = (*env)->GetObjectArrayElement(env, javaParameters, i-1);
  475. argv[i] = (char*)(*env)->GetStringUTFChars(env, object, &isCopy);
  476. }
  477. /*printf("C: decoder params = ");
  478. for (i=0; i<argc; i++) {
  479. printf("[%s]",argv[i]);
  480. }
  481. printf("\n");*/
  482. /* set decoding parameters to default values */
  483. opj_set_default_decoder_parameters(&parameters);
  484. parameters.decod_format = J2K_CFMT;
  485. /* parse input and get user encoding parameters */
  486. if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
  487. /* Release the Java arguments array*/
  488. for (i=1; i<argc; i++)
  489. (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
  490. return -1;
  491. }
  492. /* Release the Java arguments array*/
  493. for (i=1; i<argc; i++)
  494. (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, javaParameters, i-1), argv[i]);
  495. num_images=1;
  496. /* Get additional information from the Java object variables*/
  497. fid = (*env)->GetFieldID(env, cls,"skippedResolutions", "I");
  498. parameters.cp_reduce = (short) (*env)->GetIntField(env, obj, fid);
  499. /*Decoding image one by one*/
  500. for(imageno = 0; imageno < num_images ; imageno++)
  501. {
  502. image = NULL;
  503. fprintf(stderr,"\n");
  504. /* read the input file and put it in memory into the 'src' object, if the -i option is given in JavaParameters.
  505. Implemented for debug purpose. */
  506. /* -------------------------------------------------------------- */
  507. if (parameters.infile && parameters.infile[0]!='\0') {
  508. /*printf("C: opening [%s]\n", parameters.infile);*/
  509. fsrc = fopen(parameters.infile, "rb");
  510. if (!fsrc) {
  511. fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
  512. return 1;
  513. }
  514. fseek(fsrc, 0, SEEK_END);
  515. file_length = ftell(fsrc);
  516. fseek(fsrc, 0, SEEK_SET);
  517. src = (unsigned char *) opj_malloc(file_length);
  518. fread(src, 1, file_length, fsrc);
  519. fclose(fsrc);
  520. /*printf("C: %d bytes read from file\n",file_length);*/
  521. } else {
  522. /* Preparing the transfer of the codestream from Java to C*/
  523. /*printf("C: before transferring codestream\n");*/
  524. fid = (*env)->GetFieldID(env, cls,"compressedStream", "[B");
  525. jba = (*env)->GetObjectField(env, obj, fid);
  526. file_length = (*env)->GetArrayLength(env, jba);
  527. jbBody = (*env)->GetByteArrayElements(env, jba, &isCopy);
  528. src = (unsigned char*)jbBody;
  529. }
  530. /* decode the code-stream */
  531. /* ---------------------- */
  532. switch(parameters.decod_format) {
  533. case J2K_CFMT:
  534. {
  535. /* JPEG-2000 codestream */
  536. /* get a decoder handle */
  537. dinfo = opj_create_decompress(CODEC_J2K);
  538. /* catch events using our callbacks and give a local context */
  539. opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
  540. /* setup the decoder decoding parameters using user parameters */
  541. opj_setup_decoder(dinfo, &parameters);
  542. /* open a byte stream */
  543. cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
  544. /* decode the stream and fill the image structure */
  545. image = opj_decode(dinfo, cio);
  546. if(!image) {
  547. fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
  548. opj_destroy_decompress(dinfo);
  549. opj_cio_close(cio);
  550. return 1;
  551. }
  552. /* close the byte stream */
  553. opj_cio_close(cio);
  554. }
  555. break;
  556. case JP2_CFMT:
  557. {
  558. /* JPEG 2000 compressed image data */
  559. /* get a decoder handle */
  560. dinfo = opj_create_decompress(CODEC_JP2);
  561. /* catch events using our callbacks and give a local context */
  562. opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
  563. /* setup the decoder decoding parameters using the current image and user parameters */
  564. opj_setup_decoder(dinfo, &parameters);
  565. /* open a byte stream */
  566. cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
  567. /* decode the stream and fill the image structure */
  568. image = opj_decode(dinfo, cio);
  569. if(!image) {
  570. fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
  571. opj_destroy_decompress(dinfo);
  572. opj_cio_close(cio);
  573. return 1;
  574. }
  575. /* close the byte stream */
  576. opj_cio_close(cio);
  577. }
  578. break;
  579. case JPT_CFMT:
  580. {
  581. /* JPEG 2000, JPIP */
  582. /* get a decoder handle */
  583. dinfo = opj_create_decompress(CODEC_JPT);
  584. /* catch events using our callbacks and give a local context */
  585. opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, &msgErrorCallback_vars);
  586. /* setup the decoder decoding parameters using user parameters */
  587. opj_setup_decoder(dinfo, &parameters);
  588. /* open a byte stream */
  589. cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
  590. /* decode the stream and fill the image structure */
  591. image = opj_decode(dinfo, cio);
  592. if(!image) {
  593. fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
  594. opj_destroy_decompress(dinfo);
  595. opj_cio_close(cio);
  596. return 1;
  597. }
  598. /* close the byte stream */
  599. opj_cio_close(cio);
  600. }
  601. break;
  602. default:
  603. fprintf(stderr, "skipping file..\n");
  604. continue;
  605. }
  606. /* free the memory containing the code-stream */
  607. if (parameters.infile && parameters.infile[0]!='\0') {
  608. opj_free(src);
  609. } else {
  610. (*env)->ReleaseByteArrayElements(env, jba, jbBody, 0);
  611. }
  612. src = NULL;
  613. /* create output image.
  614. If the -o parameter is given in the JavaParameters, write the decoded version into a file.
  615. Implemented for debug purpose. */
  616. /* ---------------------------------- */
  617. switch (parameters.cod_format) {
  618. case PXM_DFMT: /* PNM PGM PPM */
  619. if (imagetopnm(image, parameters.outfile)) {
  620. fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
  621. }
  622. else {
  623. fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
  624. }
  625. break;
  626. case PGX_DFMT: /* PGX */
  627. if(imagetopgx(image, parameters.outfile)){
  628. fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
  629. }
  630. else {
  631. fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
  632. }
  633. break;
  634. case BMP_DFMT: /* BMP */
  635. if(imagetobmp(image, parameters.outfile)){
  636. fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
  637. }
  638. else {
  639. fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
  640. }
  641. break;
  642. }
  643. /* ========= Return the image to the Java structure ===============*/
  644. #ifdef CHECK_THRESHOLDS
  645. printf("C: checking thresholds\n");
  646. #endif
  647. /* First compute the real with and height, in function of the resolutions decoded.*/
  648. /*wr = (image->comps[0].w + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;*/
  649. /*hr = (image->comps[0].h + (1 << image->comps[0].factor) -1) >> image->comps[0].factor;*/
  650. w = image->comps[0].w;
  651. h = image->comps[0].h;
  652. if (image->numcomps==3) { /* 3 components color image*/
  653. ptr = image->comps[0].data;
  654. ptr1 = image->comps[1].data;
  655. ptr2 = image->comps[2].data;
  656. #ifdef CHECK_THRESHOLDS
  657. if (image->comps[0].sgnd) {
  658. min_value = -128;
  659. max_value = 127;
  660. } else {
  661. min_value = 0;
  662. max_value = 255;
  663. }
  664. #endif
  665. /* Get the pointer to the Java structure where the data must be copied*/
  666. fid = (*env)->GetFieldID(env, cls,"image24", "[I");
  667. jia = (*env)->GetObjectField(env, obj, fid);
  668. jiBody = (*env)->GetIntArrayElements(env, jia, 0);
  669. ptrIBody = jiBody;
  670. printf("C: transferring image24: %d int to Java pointer=%d\n",image->numcomps*w*h, ptrIBody);
  671. for (i=0; i<w*h; i++) {
  672. tempUC = (unsigned char)(ptr[i]);
  673. tempUC1 = (unsigned char)(ptr1[i]);
  674. tempUC2 = (unsigned char)(ptr2[i]);
  675. #ifdef CHECK_THRESHOLDS
  676. if (tempUC < min_value)
  677. tempUC=min_value;
  678. else if (tempUC > max_value)
  679. tempUC=max_value;
  680. if (tempUC1 < min_value)
  681. tempUC1=min_value;
  682. else if (tempUC1 > max_value)
  683. tempUC1=max_value;
  684. if (tempUC2 < min_value)
  685. tempUC2=min_value;
  686. else if (tempUC2 > max_value)
  687. tempUC2=max_value;
  688. #endif
  689. *(ptrIBody++) = (int) ( (tempUC2<<16) + (tempUC1<<8) + tempUC );
  690. }
  691. (*env)->ReleaseIntArrayElements(env, jia, jiBody, 0);
  692. } else { /* 1 component 8 or 16 bpp image*/
  693. ptr = image->comps[0].data;
  694. printf("C: before transferring a %d bpp image to java (length = %d)\n",image->comps[0].prec ,w*h);
  695. if (image->comps[0].prec<=8) {
  696. fid = (*env)->GetFieldID(env, cls,"image8", "[B");
  697. jba = (*env)->GetObjectField(env, obj, fid);
  698. jbBody = (*env)->GetByteArrayElements(env, jba, 0);
  699. ptrBBody = jbBody;
  700. #ifdef CHECK_THRESHOLDS
  701. if (image->comps[0].sgnd) {
  702. min_value = -128;
  703. max_value = 127;
  704. } else {
  705. min_value = 0;
  706. max_value = 255;
  707. }
  708. #endif
  709. /*printf("C: transferring %d shorts to Java image8 pointer = %d\n", wr*hr,ptrSBody);*/
  710. for (i=0; i<w*h; i++) {
  711. tempUC = (unsigned char) (ptr[i]);
  712. #ifdef CHECK_THRESHOLDS
  713. if (tempUC<min_value)
  714. tempUC = min_value;
  715. else if (tempUC > max_value)
  716. tempUC = max_value;
  717. #endif
  718. *(ptrBBody++) = tempUC;
  719. }
  720. (*env)->ReleaseByteArrayElements(env, jba, jbBody, 0);
  721. printf("C: image8 transferred to Java\n");
  722. } else {
  723. fid = (*env)->GetFieldID(env, cls,"image16", "[S");
  724. jsa = (*env)->GetObjectField(env, obj, fid);
  725. jsBody = (*env)->GetShortArrayElements(env, jsa, 0);
  726. ptrSBody = jsBody;
  727. #ifdef CHECK_THRESHOLDS
  728. if (image->comps[0].sgnd) {
  729. min_value = -32768;
  730. max_value = 32767;
  731. } else {
  732. min_value = 0;
  733. max_value = 65535;
  734. }
  735. printf("C: minValue = %d, maxValue = %d\n", min_value, max_value);
  736. #endif
  737. printf("C: transferring %d shorts to Java image16 pointer = %d\n", w*h,ptrSBody);
  738. for (i=0; i<w*h; i++) {
  739. tempS = (short) (ptr[i]);
  740. #ifdef CHECK_THRESHOLDS
  741. if (tempS<min_value) {
  742. printf("C: value %d truncated to %d\n", tempS, min_value);
  743. tempS = min_value;
  744. } else if (tempS > max_value) {
  745. printf("C: value %d truncated to %d\n", tempS, max_value);
  746. tempS = max_value;
  747. }
  748. #endif
  749. *(ptrSBody++) = tempS;
  750. }
  751. (*env)->ReleaseShortArrayElements(env, jsa, jsBody, 0);
  752. printf("C: image16 completely filled\n");
  753. }
  754. }
  755. /* free remaining structures */
  756. if(dinfo) {
  757. opj_destroy_decompress(dinfo);
  758. }
  759. /* free image data structure */
  760. opj_image_destroy(image);
  761. }
  762. return 1; /* OK */
  763. }
  764. /*end main*/