JavaOpenJPEG.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  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 "index.h"
  44. #include "dirent.h"
  45. #include "org_openJpeg_OpenJPEGJavaEncoder.h"
  46. #ifndef _WIN32
  47. #define stricmp strcasecmp
  48. #define strnicmp strncasecmp
  49. #endif
  50. #include "format_defs.h"
  51. #define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/
  52. #define CINEMA_48_CS 651041 /*Codestream length for 48fps*/
  53. #define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/
  54. #define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/
  55. extern int get_file_format(char *filename);
  56. extern void error_callback(const char *msg, void *client_data);
  57. extern void warning_callback(const char *msg, void *client_data);
  58. extern void info_callback(const char *msg, void *client_data);
  59. typedef struct callback_variables {
  60. JNIEnv *env;
  61. /** 'jclass' object used to call a Java method from the C */
  62. jobject *jobj;
  63. /** 'jclass' object used to call a Java method from the C */
  64. jmethodID message_mid;
  65. jmethodID error_mid;
  66. } callback_variables_t;
  67. typedef struct dircnt {
  68. /** Buffer for holding images read from Directory*/
  69. char *filename_buf;
  70. /** Pointer to the buffer*/
  71. char **filename;
  72. } dircnt_t;
  73. typedef struct img_folder {
  74. /** The directory path of the folder containing input images*/
  75. char *imgdirpath;
  76. /** Output format*/
  77. char *out_format;
  78. /** Enable option*/
  79. char set_imgdir;
  80. /** Enable Cod Format for output*/
  81. char set_out_format;
  82. /** User specified rate stored in case of cinema option*/
  83. float *rates;
  84. } img_fol_t;
  85. static void encode_help_display()
  86. {
  87. fprintf(stdout, "HELP\n----\n\n");
  88. fprintf(stdout, "- the -h option displays this help information on screen\n\n");
  89. /* UniPG>> */
  90. fprintf(stdout, "List of parameters for the JPEG 2000 "
  91. #ifdef USE_JPWL
  92. "+ JPWL "
  93. #endif /* USE_JPWL */
  94. "encoder:\n");
  95. /* <<UniPG */
  96. fprintf(stdout, "\n");
  97. fprintf(stdout, "REMARKS:\n");
  98. fprintf(stdout, "---------\n");
  99. fprintf(stdout, "\n");
  100. fprintf(stdout,
  101. "The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
  102. fprintf(stdout, "COD and QCD never appear in the tile_header.\n");
  103. fprintf(stdout, "\n");
  104. fprintf(stdout,
  105. "- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
  106. fprintf(stdout,
  107. "color image. You need enough disk space memory (twice the original) to encode \n");
  108. fprintf(stdout,
  109. "the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
  110. fprintf(stdout, "\n");
  111. fprintf(stdout, "By default:\n");
  112. fprintf(stdout, "------------\n");
  113. fprintf(stdout, "\n");
  114. fprintf(stdout, " * Lossless\n");
  115. fprintf(stdout, " * 1 tile\n");
  116. fprintf(stdout, " * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
  117. fprintf(stdout, " * Size of code-block : 64 x 64\n");
  118. fprintf(stdout, " * Number of resolutions: 6\n");
  119. fprintf(stdout, " * No SOP marker in the codestream\n");
  120. fprintf(stdout, " * No EPH marker in the codestream\n");
  121. fprintf(stdout, " * No sub-sampling in x or y direction\n");
  122. fprintf(stdout, " * No mode switch activated\n");
  123. fprintf(stdout, " * Progression order: LRCP\n");
  124. fprintf(stdout, " * No index file\n");
  125. fprintf(stdout, " * No ROI upshifted\n");
  126. fprintf(stdout, " * No offset of the origin of the image\n");
  127. fprintf(stdout, " * No offset of the origin of the tiles\n");
  128. fprintf(stdout, " * Reversible DWT 5-3\n");
  129. /* UniPG>> */
  130. #ifdef USE_JPWL
  131. fprintf(stdout, " * No JPWL protection\n");
  132. #endif /* USE_JPWL */
  133. /* <<UniPG */
  134. fprintf(stdout, "\n");
  135. fprintf(stdout, "Parameters:\n");
  136. fprintf(stdout, "------------\n");
  137. fprintf(stdout, "\n");
  138. fprintf(stdout, "Required Parameters (except with -h):\n");
  139. fprintf(stdout, "One of the two options -ImgDir or -i must be used\n");
  140. fprintf(stdout, "\n");
  141. fprintf(stdout,
  142. "-ImgDir : Image file Directory path (example ../Images) \n");
  143. fprintf(stdout, " When using this option -OutFor must be used\n");
  144. fprintf(stdout, "\n");
  145. fprintf(stdout, "-OutFor \n");
  146. fprintf(stdout, " REQUIRED only if -ImgDir is used\n");
  147. fprintf(stdout, " Need to specify only format without filename <BMP> \n");
  148. fprintf(stdout,
  149. " Currently accepts PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA formats\n");
  150. fprintf(stdout, "\n");
  151. fprintf(stdout,
  152. "-i : source file (-i source.pnm also *.pgm, *.ppm, *.bmp, *.tif, *.raw, *.tga) \n");
  153. fprintf(stdout, " When using this option -o must be used\n");
  154. fprintf(stdout, "\n");
  155. fprintf(stdout, "-o : destination file (-o dest.j2k or .jp2) \n");
  156. fprintf(stdout, "\n");
  157. fprintf(stdout, "Optional Parameters:\n");
  158. fprintf(stdout, "\n");
  159. fprintf(stdout, "-h : display the help information \n ");
  160. fprintf(stdout, "\n");
  161. fprintf(stdout,
  162. "-cinema2K : Digital Cinema 2K profile compliant codestream for 2K resolution.(-cinema2k 24 or 48) \n");
  163. fprintf(stdout,
  164. " Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n");
  165. fprintf(stdout, "\n");
  166. fprintf(stdout,
  167. "-cinema4K : Digital Cinema 4K profile compliant codestream for 4K resolution \n");
  168. fprintf(stdout, " Frames per second not required. Default value is 24fps\n");
  169. fprintf(stdout, "\n");
  170. fprintf(stdout,
  171. "-r : different compression ratios for successive layers (-r 20,10,5)\n ");
  172. fprintf(stdout,
  173. " - The rate specified for each quality level is the desired \n");
  174. fprintf(stdout, " compression factor.\n");
  175. fprintf(stdout, " Example: -r 20,10,1 means quality 1: compress 20x, \n");
  176. fprintf(stdout,
  177. " quality 2: compress 10x and quality 3: compress lossless\n");
  178. fprintf(stdout, "\n");
  179. fprintf(stdout,
  180. " (options -r and -q cannot be used together)\n ");
  181. fprintf(stdout, "\n");
  182. fprintf(stdout,
  183. "-q : different psnr for successive layers (-q 30,40,50) \n ");
  184. fprintf(stdout,
  185. " (options -r and -q cannot be used together)\n ");
  186. fprintf(stdout, "\n");
  187. fprintf(stdout, "-n : number of resolutions (-n 3) \n");
  188. fprintf(stdout, "\n");
  189. fprintf(stdout, "-b : size of code block (-b 32,32) \n");
  190. fprintf(stdout, "\n");
  191. fprintf(stdout, "-c : size of precinct (-c 128,128) \n");
  192. fprintf(stdout, "\n");
  193. fprintf(stdout, "-t : size of tile (-t 512,512) \n");
  194. fprintf(stdout, "\n");
  195. fprintf(stdout,
  196. "-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
  197. fprintf(stdout, "\n");
  198. fprintf(stdout, "-s : subsampling factor (-s 2,2) [-s X,Y] \n");
  199. fprintf(stdout, " Remark: subsampling bigger than 2 can produce error\n");
  200. fprintf(stdout, "\n");
  201. fprintf(stdout,
  202. "-POC : Progression order change (-POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL) \n");
  203. fprintf(stdout, " Example: T1=0,0,1,5,3,CPRL \n");
  204. fprintf(stdout,
  205. " : Ttilenumber=Resolution num start,Component num start,Layer num end,Resolution num end,Component num end,Progression order\n");
  206. fprintf(stdout, "\n");
  207. fprintf(stdout, "-SOP : write SOP marker before each packet \n");
  208. fprintf(stdout, "\n");
  209. fprintf(stdout, "-EPH : write EPH marker after each header packet \n");
  210. fprintf(stdout, "\n");
  211. fprintf(stdout,
  212. "-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
  213. fprintf(stdout,
  214. " 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
  215. fprintf(stdout,
  216. " Indicate multiple modes by adding their values. \n");
  217. fprintf(stdout,
  218. " ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
  219. fprintf(stdout, "\n");
  220. fprintf(stdout,
  221. "-TP : divide packets of every tile into tile-parts (-TP R) [R, L, C]\n");
  222. fprintf(stdout, "\n");
  223. fprintf(stdout,
  224. "-x : create an index file *.Idx (-x index_name.Idx) \n");
  225. fprintf(stdout, "\n");
  226. fprintf(stdout,
  227. "-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
  228. fprintf(stdout, " for component c=%%d [%%d = 0,1,2]\n");
  229. fprintf(stdout,
  230. " with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI c=0,U=25) \n");
  231. fprintf(stdout, "\n");
  232. fprintf(stdout,
  233. "-d : offset of the origin of the image (-d 150,300) \n");
  234. fprintf(stdout, "\n");
  235. fprintf(stdout,
  236. "-T : offset of the origin of the tiles (-T 100,75) \n");
  237. fprintf(stdout, "\n");
  238. fprintf(stdout, "-I : use the irreversible DWT 9-7 (-I) \n");
  239. fprintf(stdout, "\n");
  240. fprintf(stdout,
  241. "-jpip : write jpip codestream index box in JP2 output file\n");
  242. fprintf(stdout, " NOTICE: currently supports only RPCL order\n");
  243. fprintf(stdout, "\n");
  244. /* UniPG>> */
  245. #ifdef USE_JPWL
  246. fprintf(stdout,
  247. "-W : adoption of JPWL (Part 11) capabilities (-W params)\n");
  248. fprintf(stdout,
  249. " The parameters can be written and repeated in any order:\n");
  250. fprintf(stdout,
  251. " [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
  252. fprintf(stdout,
  253. " ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
  254. fprintf(stdout, "\n");
  255. fprintf(stdout,
  256. " h selects the header error protection (EPB): 'type' can be\n");
  257. fprintf(stdout,
  258. " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
  259. fprintf(stdout,
  260. " if 'tilepart' is absent, it is for main and tile headers\n");
  261. fprintf(stdout,
  262. " if 'tilepart' is present, it applies from that tile\n");
  263. fprintf(stdout,
  264. " onwards, up to the next h<> spec, or to the last tilepart\n");
  265. fprintf(stdout, " in the codestream (max. %d specs)\n",
  266. JPWL_MAX_NO_TILESPECS);
  267. fprintf(stdout, "\n");
  268. fprintf(stdout,
  269. " p selects the packet error protection (EEP/UEP with EPBs)\n");
  270. fprintf(stdout, " to be applied to raw data: 'type' can be\n");
  271. fprintf(stdout,
  272. " [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
  273. fprintf(stdout,
  274. " if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
  275. fprintf(stdout,
  276. " if 'tilepart:pack' is present, it applies from that tile\n");
  277. fprintf(stdout,
  278. " and that packet onwards, up to the next packet spec\n");
  279. fprintf(stdout,
  280. " or to the last packet in the last tilepart in the stream\n");
  281. fprintf(stdout, " (max. %d specs)\n",
  282. JPWL_MAX_NO_PACKSPECS);
  283. fprintf(stdout, "\n");
  284. fprintf(stdout,
  285. " s enables sensitivity data insertion (ESD): 'method' can be\n");
  286. fprintf(stdout,
  287. " [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
  288. fprintf(stdout,
  289. " 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
  290. fprintf(stdout,
  291. " if 'tilepart' is absent, it is for main header only\n");
  292. fprintf(stdout,
  293. " if 'tilepart' is present, it applies from that tile\n");
  294. fprintf(stdout,
  295. " onwards, up to the next s<> spec, or to the last tilepart\n");
  296. fprintf(stdout, " in the codestream (max. %d specs)\n",
  297. JPWL_MAX_NO_TILESPECS);
  298. fprintf(stdout, "\n");
  299. fprintf(stdout,
  300. " g determines the addressing mode: <range> can be\n");
  301. fprintf(stdout, " [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
  302. fprintf(stdout, "\n");
  303. fprintf(stdout,
  304. " a determines the size of data addressing: <addr> can be\n");
  305. fprintf(stdout,
  306. " 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
  307. fprintf(stdout, "\n");
  308. fprintf(stdout,
  309. " z determines the size of sensitivity values: <size> can be\n");
  310. fprintf(stdout,
  311. " 1/2 bytes, for the transformed pseudo-floating point value\n");
  312. fprintf(stdout, "\n");
  313. fprintf(stdout, " ex.:\n");
  314. fprintf(stdout,
  315. " h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
  316. fprintf(stdout, " s0=6,s3=-1,a=0,g=1,z=1\n");
  317. fprintf(stdout, " means\n");
  318. fprintf(stdout,
  319. " predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
  320. fprintf(stdout,
  321. " CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
  322. fprintf(stdout,
  323. " UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
  324. fprintf(stdout,
  325. " UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
  326. fprintf(stdout,
  327. " UEP rs default for packets of tilepart 1,\n");
  328. fprintf(stdout,
  329. " no UEP for packets 0 to 19 of tilepart 3,\n");
  330. fprintf(stdout,
  331. " UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
  332. fprintf(stdout, " relative sensitivity ESD for MH,\n");
  333. fprintf(stdout,
  334. " TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
  335. fprintf(stdout,
  336. " size of addresses and 1 byte for each sensitivity value\n");
  337. fprintf(stdout, "\n");
  338. fprintf(stdout, " ex.:\n");
  339. fprintf(stdout, " h,s,p\n");
  340. fprintf(stdout, " means\n");
  341. fprintf(stdout,
  342. " default protection to headers (MH and TPHs) as well as\n");
  343. fprintf(stdout, " data packets, one ESD in MH\n");
  344. fprintf(stdout, "\n");
  345. fprintf(stdout,
  346. " N.B.: use the following recommendations when specifying\n");
  347. fprintf(stdout, " the JPWL parameters list\n");
  348. fprintf(stdout,
  349. " - when you use UEP, always pair the 'p' option with 'h'\n");
  350. fprintf(stdout, " \n");
  351. #endif /* USE_JPWL */
  352. /* <<UniPG */
  353. fprintf(stdout, "IMPORTANT:\n");
  354. fprintf(stdout, "-----------\n");
  355. fprintf(stdout, "\n");
  356. fprintf(stdout, "The index file has the structure below:\n");
  357. fprintf(stdout, "---------------------------------------\n");
  358. fprintf(stdout, "\n");
  359. fprintf(stdout, "Image_height Image_width\n");
  360. fprintf(stdout, "progression order\n");
  361. fprintf(stdout, "Tiles_size_X Tiles_size_Y\n");
  362. fprintf(stdout, "Tiles_nb_X Tiles_nb_Y\n");
  363. fprintf(stdout, "Components_nb\n");
  364. fprintf(stdout, "Layers_nb\n");
  365. fprintf(stdout, "decomposition_levels\n");
  366. fprintf(stdout, "[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
  367. fprintf(stdout, " [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
  368. fprintf(stdout, "Main_header_start_position\n");
  369. fprintf(stdout, "Main_header_end_position\n");
  370. fprintf(stdout, "Codestream_size\n");
  371. fprintf(stdout, "\n");
  372. fprintf(stdout, "INFO ON TILES\n");
  373. fprintf(stdout,
  374. "tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
  375. fprintf(stdout,
  376. "Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
  377. fprintf(stdout,
  378. "Tile_1 '' '' '' '' '' '' ''\n");
  379. fprintf(stdout, "...\n");
  380. fprintf(stdout,
  381. "Tile_Nt '' '' '' '' '' '' ''\n");
  382. fprintf(stdout, "...\n");
  383. fprintf(stdout, "TILE 0 DETAILS\n");
  384. fprintf(stdout, "part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
  385. fprintf(stdout, "...\n");
  386. fprintf(stdout, "Progression_string\n");
  387. fprintf(stdout,
  388. "pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
  389. fprintf(stdout,
  390. "Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
  391. fprintf(stdout, "...\n");
  392. fprintf(stdout,
  393. "Tpacket_Np '' '' '' '' '' '' '' ''\n");
  394. fprintf(stdout, "MaxDisto\n");
  395. fprintf(stdout, "TotalDisto\n\n");
  396. }
  397. static OPJ_PROG_ORDER give_progression(const char progression[4])
  398. {
  399. if (strncmp(progression, "LRCP", 4) == 0) {
  400. return LRCP;
  401. }
  402. if (strncmp(progression, "RLCP", 4) == 0) {
  403. return RLCP;
  404. }
  405. if (strncmp(progression, "RPCL", 4) == 0) {
  406. return RPCL;
  407. }
  408. if (strncmp(progression, "PCRL", 4) == 0) {
  409. return PCRL;
  410. }
  411. if (strncmp(progression, "CPRL", 4) == 0) {
  412. return CPRL;
  413. }
  414. return PROG_UNKNOWN;
  415. }
  416. static int initialise_4K_poc(opj_poc_t *POC, int numres)
  417. {
  418. POC[0].tile = 1;
  419. POC[0].resno0 = 0;
  420. POC[0].compno0 = 0;
  421. POC[0].layno1 = 1;
  422. POC[0].resno1 = numres - 1;
  423. POC[0].compno1 = 3;
  424. POC[0].prg1 = CPRL;
  425. POC[1].tile = 1;
  426. POC[1].resno0 = numres - 1;
  427. POC[1].compno0 = 0;
  428. POC[1].layno1 = 1;
  429. POC[1].resno1 = numres;
  430. POC[1].compno1 = 3;
  431. POC[1].prg1 = CPRL;
  432. return 2;
  433. }
  434. static void cinema_parameters(opj_cparameters_t *parameters)
  435. {
  436. parameters->tile_size_on = OPJ_FALSE;
  437. parameters->cp_tdx = 1;
  438. parameters->cp_tdy = 1;
  439. /*Tile part*/
  440. parameters->tp_flag = 'C';
  441. parameters->tp_on = 1;
  442. /*Tile and Image shall be at (0,0)*/
  443. parameters->cp_tx0 = 0;
  444. parameters->cp_ty0 = 0;
  445. parameters->image_offset_x0 = 0;
  446. parameters->image_offset_y0 = 0;
  447. /*Codeblock size= 32*32*/
  448. parameters->cblockw_init = 32;
  449. parameters->cblockh_init = 32;
  450. parameters->csty |= 0x01;
  451. /*The progression order shall be CPRL*/
  452. parameters->prog_order = CPRL;
  453. /* No ROI */
  454. parameters->roi_compno = -1;
  455. parameters->subsampling_dx = 1;
  456. parameters->subsampling_dy = 1;
  457. /* 9-7 transform */
  458. parameters->irreversible = 1;
  459. }
  460. static void cinema_setup_encoder(opj_cparameters_t *parameters,
  461. opj_image_t *image, img_fol_t *img_fol)
  462. {
  463. int i;
  464. float temp_rate;
  465. opj_poc_t *POC = NULL;
  466. switch (parameters->cp_cinema) {
  467. case CINEMA2K_24:
  468. case CINEMA2K_48:
  469. if (parameters->numresolution > 6) {
  470. parameters->numresolution = 6;
  471. }
  472. if (!((image->comps[0].w == 2048) | (image->comps[0].h == 1080))) {
  473. fprintf(stdout,
  474. "Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3"
  475. "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
  476. image->comps[0].w, image->comps[0].h);
  477. parameters->cp_rsiz = STD_RSIZ;
  478. }
  479. break;
  480. case CINEMA4K_24:
  481. if (parameters->numresolution < 1) {
  482. parameters->numresolution = 1;
  483. } else if (parameters->numresolution > 7) {
  484. parameters->numresolution = 7;
  485. }
  486. if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))) {
  487. fprintf(stdout,
  488. "Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
  489. "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
  490. image->comps[0].w, image->comps[0].h);
  491. parameters->cp_rsiz = STD_RSIZ;
  492. }
  493. parameters->numpocs = initialise_4K_poc(parameters->POC,
  494. parameters->numresolution);
  495. break;
  496. }
  497. switch (parameters->cp_cinema) {
  498. case CINEMA2K_24:
  499. case CINEMA4K_24:
  500. for (i = 0 ; i < parameters->tcp_numlayers ; i++) {
  501. temp_rate = 0 ;
  502. if (img_fol->rates[i] == 0) {
  503. parameters->tcp_rates[0] = ((float)(image->numcomps * image->comps[0].w *
  504. image->comps[0].h * image->comps[0].prec)) /
  505. (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  506. } else {
  507. temp_rate = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h *
  508. image->comps[0].prec)) /
  509. (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
  510. if (temp_rate > CINEMA_24_CS) {
  511. parameters->tcp_rates[i] = ((float)(image->numcomps * image->comps[0].w *
  512. image->comps[0].h * image->comps[0].prec)) /
  513. (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  514. } else {
  515. parameters->tcp_rates[i] = img_fol->rates[i];
  516. }
  517. }
  518. }
  519. parameters->max_comp_size = COMP_24_CS;
  520. break;
  521. case CINEMA2K_48:
  522. for (i = 0 ; i < parameters->tcp_numlayers ; i++) {
  523. temp_rate = 0 ;
  524. if (img_fol->rates[i] == 0) {
  525. parameters->tcp_rates[0] = ((float)(image->numcomps * image->comps[0].w *
  526. image->comps[0].h * image->comps[0].prec)) /
  527. (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  528. } else {
  529. temp_rate = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h *
  530. image->comps[0].prec)) /
  531. (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
  532. if (temp_rate > CINEMA_48_CS) {
  533. parameters->tcp_rates[0] = ((float)(image->numcomps * image->comps[0].w *
  534. image->comps[0].h * image->comps[0].prec)) /
  535. (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  536. } else {
  537. parameters->tcp_rates[i] = img_fol->rates[i];
  538. }
  539. }
  540. }
  541. parameters->max_comp_size = COMP_48_CS;
  542. break;
  543. }
  544. parameters->cp_disto_alloc = 1;
  545. }
  546. /* ------------------------------------------------------------------------------------ */
  547. static int parse_cmdline_encoder(int argc, char **argv,
  548. opj_cparameters_t *parameters,
  549. img_fol_t *img_fol, char *indexfilename)
  550. {
  551. int i, j, totlen;
  552. opj_option_t long_option[] = {
  553. {"cinema2K", REQ_ARG, NULL, 'w'},
  554. {"cinema4K", NO_ARG, NULL, 'y'},
  555. {"ImgDir", REQ_ARG, NULL, 'z'},
  556. {"TP", REQ_ARG, NULL, 'u'},
  557. {"SOP", NO_ARG, NULL, 'S'},
  558. {"EPH", NO_ARG, NULL, 'E'},
  559. {"OutFor", REQ_ARG, NULL, 'O'},
  560. {"POC", REQ_ARG, NULL, 'P'},
  561. {"ROI", REQ_ARG, NULL, 'R'},
  562. {"jpip", NO_ARG, NULL, 'J'}
  563. };
  564. /* parse the command line */
  565. /* UniPG>> */
  566. const char optlist[] = "i:o:hr:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:"
  567. #ifdef USE_JPWL
  568. "W:"
  569. #endif /* USE_JPWL */
  570. ;
  571. /*printf("C: parse_cmdline_encoder:");
  572. for (i=0; i<argc; i++) {
  573. printf("[%s]",argv[i]);
  574. }
  575. printf("\n");*/
  576. totlen = sizeof(long_option);
  577. img_fol->set_out_format = 0;
  578. reset_options_reading();
  579. while (1) {
  580. int c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
  581. if (c == -1) {
  582. break;
  583. }
  584. switch (c) {
  585. /* ----------------------------------------------------- */
  586. case 'o': { /* output file */
  587. char *outfile = opj_optarg;
  588. parameters->cod_format = get_file_format(outfile);
  589. switch (parameters->cod_format) {
  590. case J2K_CFMT:
  591. case JP2_CFMT:
  592. break;
  593. default:
  594. fprintf(stderr,
  595. "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
  596. return 1;
  597. }
  598. strncpy(parameters->outfile, outfile, sizeof(parameters->outfile) - 1);
  599. }
  600. break;
  601. /* ----------------------------------------------------- */
  602. case 'O': { /* output format */
  603. char outformat[50];
  604. char *of = opj_optarg;
  605. sprintf(outformat, ".%s", of);
  606. img_fol->set_out_format = 1;
  607. parameters->cod_format = get_file_format(outformat);
  608. switch (parameters->cod_format) {
  609. case J2K_CFMT:
  610. case JP2_CFMT:
  611. img_fol->out_format = opj_optarg;
  612. break;
  613. default:
  614. fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
  615. return 1;
  616. }
  617. }
  618. break;
  619. /* ----------------------------------------------------- */
  620. case 'r': { /* rates rates/distorsion */
  621. char *s = opj_optarg;
  622. while (sscanf(s, "%f", &parameters->tcp_rates[parameters->tcp_numlayers]) ==
  623. 1) {
  624. parameters->tcp_numlayers++;
  625. while (*s && *s != ',') {
  626. s++;
  627. }
  628. if (!*s) {
  629. break;
  630. }
  631. s++;
  632. }
  633. parameters->cp_disto_alloc = 1;
  634. }
  635. break;
  636. /* ----------------------------------------------------- */
  637. case 'q': { /* add fixed_quality */
  638. char *s = opj_optarg;
  639. while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers])
  640. == 1) {
  641. parameters->tcp_numlayers++;
  642. while (*s && *s != ',') {
  643. s++;
  644. }
  645. if (!*s) {
  646. break;
  647. }
  648. s++;
  649. }
  650. parameters->cp_fixed_quality = 1;
  651. }
  652. break;
  653. /* dda */
  654. /* ----------------------------------------------------- */
  655. case 'f': { /* mod fixed_quality (before : -q) */
  656. int *row = NULL, *col = NULL;
  657. int numlayers = 0, numresolution = 0, matrix_width = 0;
  658. char *s = opj_optarg;
  659. sscanf(s, "%d", &numlayers);
  660. s++;
  661. if (numlayers > 9) {
  662. s++;
  663. }
  664. parameters->tcp_numlayers = numlayers;
  665. numresolution = parameters->numresolution;
  666. matrix_width = numresolution * 3;
  667. parameters->cp_matrice = (int *) opj_malloc(numlayers * matrix_width * sizeof(
  668. int));
  669. s = s + 2;
  670. for (i = 0; i < numlayers; i++) {
  671. row = &parameters->cp_matrice[i * matrix_width];
  672. col = row;
  673. parameters->tcp_rates[i] = 1;
  674. sscanf(s, "%d,", &col[0]);
  675. s += 2;
  676. if (col[0] > 9) {
  677. s++;
  678. }
  679. col[1] = 0;
  680. col[2] = 0;
  681. for (j = 1; j < numresolution; j++) {
  682. col += 3;
  683. sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
  684. s += 6;
  685. if (col[0] > 9) {
  686. s++;
  687. }
  688. if (col[1] > 9) {
  689. s++;
  690. }
  691. if (col[2] > 9) {
  692. s++;
  693. }
  694. }
  695. if (i < numlayers - 1) {
  696. s++;
  697. }
  698. }
  699. parameters->cp_fixed_alloc = 1;
  700. }
  701. break;
  702. /* ----------------------------------------------------- */
  703. case 't': { /* tiles */
  704. sscanf(opj_optarg, "%d,%d", &parameters->cp_tdx, &parameters->cp_tdy);
  705. parameters->tile_size_on = OPJ_TRUE;
  706. }
  707. break;
  708. /* ----------------------------------------------------- */
  709. case 'n': { /* resolution */
  710. sscanf(opj_optarg, "%d", &parameters->numresolution);
  711. }
  712. break;
  713. /* ----------------------------------------------------- */
  714. case 'c': { /* precinct dimension */
  715. char sep;
  716. int res_spec = 0;
  717. char *s = opj_optarg;
  718. do {
  719. sep = 0;
  720. sscanf(s, "[%d,%d]%c", &parameters->prcw_init[res_spec],
  721. &parameters->prch_init[res_spec], &sep);
  722. parameters->csty |= 0x01;
  723. res_spec++;
  724. s = strpbrk(s, "]") + 2;
  725. } while (sep == ',');
  726. parameters->res_spec = res_spec;
  727. }
  728. break;
  729. /* ----------------------------------------------------- */
  730. case 'b': { /* code-block dimension */
  731. int cblockw_init = 0, cblockh_init = 0;
  732. sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
  733. if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
  734. || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
  735. fprintf(stderr,
  736. "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
  737. " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
  738. return 1;
  739. }
  740. parameters->cblockw_init = cblockw_init;
  741. parameters->cblockh_init = cblockh_init;
  742. }
  743. break;
  744. /* ----------------------------------------------------- */
  745. case 'x': { /* creation of index file */
  746. char *index = opj_optarg;
  747. strncpy(indexfilename, index, OPJ_PATH_LEN);
  748. }
  749. break;
  750. /* ----------------------------------------------------- */
  751. case 'p': { /* progression order */
  752. char progression[4];
  753. strncpy(progression, opj_optarg, 4);
  754. parameters->prog_order = give_progression(progression);
  755. if (parameters->prog_order == -1) {
  756. fprintf(stderr, "Unrecognized progression order "
  757. "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
  758. return 1;
  759. }
  760. }
  761. break;
  762. /* ----------------------------------------------------- */
  763. case 's': { /* subsampling factor */
  764. if (sscanf(opj_optarg, "%d,%d", &parameters->subsampling_dx,
  765. &parameters->subsampling_dy) != 2) {
  766. fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
  767. return 1;
  768. }
  769. }
  770. break;
  771. /* ----------------------------------------------------- */
  772. case 'd': { /* coordonnate of the reference grid */
  773. if (sscanf(opj_optarg, "%d,%d", &parameters->image_offset_x0,
  774. &parameters->image_offset_y0) != 2) {
  775. fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
  776. "error !! [-d x0,y0]\n");
  777. return 1;
  778. }
  779. }
  780. break;
  781. /* ----------------------------------------------------- */
  782. case 'h': /* display an help description */
  783. encode_help_display();
  784. return 1;
  785. /* ----------------------------------------------------- */
  786. case 'P': { /* POC */
  787. int numpocs = 0; /* number of progression order change (POC) default 0 */
  788. opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
  789. char *s = opj_optarg;
  790. POC = parameters->POC;
  791. while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
  792. &POC[numpocs].resno0, &POC[numpocs].compno0,
  793. &POC[numpocs].layno1, &POC[numpocs].resno1,
  794. &POC[numpocs].compno1, &POC[numpocs].progorder) == 7) {
  795. POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
  796. numpocs++;
  797. while (*s && *s != '/') {
  798. s++;
  799. }
  800. if (!*s) {
  801. break;
  802. }
  803. s++;
  804. }
  805. parameters->numpocs = numpocs;
  806. }
  807. break;
  808. /* ------------------------------------------------------ */
  809. case 'S': { /* SOP marker */
  810. parameters->csty |= 0x02;
  811. }
  812. break;
  813. /* ------------------------------------------------------ */
  814. case 'E': { /* EPH marker */
  815. parameters->csty |= 0x04;
  816. }
  817. break;
  818. /* ------------------------------------------------------ */
  819. case 'M': { /* Mode switch pas tous au point !! */
  820. int value = 0;
  821. if (sscanf(opj_optarg, "%d", &value) == 1) {
  822. for (i = 0; i <= 5; i++) {
  823. int cache = value & (1 << i);
  824. if (cache) {
  825. parameters->mode |= (1 << i);
  826. }
  827. }
  828. }
  829. }
  830. break;
  831. /* ------------------------------------------------------ */
  832. case 'R': { /* ROI */
  833. if (sscanf(opj_optarg, "c=%d,U=%d", &parameters->roi_compno,
  834. &parameters->roi_shift) != 2) {
  835. fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n");
  836. return 1;
  837. }
  838. }
  839. break;
  840. /* ------------------------------------------------------ */
  841. case 'T': { /* Tile offset */
  842. if (sscanf(opj_optarg, "%d,%d", &parameters->cp_tx0,
  843. &parameters->cp_ty0) != 2) {
  844. fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
  845. return 1;
  846. }
  847. }
  848. break;
  849. /* ------------------------------------------------------ */
  850. case 'C': { /* add a comment */
  851. parameters->cp_comment = (char*)opj_malloc(strlen(opj_optarg) + 1);
  852. if (parameters->cp_comment) {
  853. strcpy(parameters->cp_comment, opj_optarg);
  854. }
  855. }
  856. break;
  857. /* ------------------------------------------------------ */
  858. case 'I': { /* reversible or not */
  859. parameters->irreversible = 1;
  860. }
  861. break;
  862. /* ------------------------------------------------------ */
  863. case 'u': { /* Tile part generation*/
  864. parameters->tp_flag = opj_optarg[0];
  865. parameters->tp_on = 1;
  866. }
  867. break;
  868. /* ------------------------------------------------------ */
  869. case 'z': { /* Image Directory path */
  870. img_fol->imgdirpath = (char*)opj_malloc(strlen(opj_optarg) + 1);
  871. strcpy(img_fol->imgdirpath, opj_optarg);
  872. img_fol->set_imgdir = 1;
  873. }
  874. break;
  875. /* ------------------------------------------------------ */
  876. case 'w': { /* Digital Cinema 2K profile compliance*/
  877. int fps = 0;
  878. sscanf(opj_optarg, "%d", &fps);
  879. if (fps == 24) {
  880. parameters->cp_cinema = CINEMA2K_24;
  881. } else if (fps == 48) {
  882. parameters->cp_cinema = CINEMA2K_48;
  883. } else {
  884. fprintf(stderr, "Incorrect value!! must be 24 or 48\n");
  885. return 1;
  886. }
  887. fprintf(stdout, "CINEMA 2K compliant codestream\n");
  888. parameters->cp_rsiz = CINEMA2K;
  889. }
  890. break;
  891. /* ------------------------------------------------------ */
  892. case 'y': { /* Digital Cinema 4K profile compliance*/
  893. parameters->cp_cinema = CINEMA4K_24;
  894. fprintf(stdout, "CINEMA 4K compliant codestream\n");
  895. parameters->cp_rsiz = CINEMA4K;
  896. }
  897. break;
  898. /* ------------------------------------------------------ */
  899. /* UniPG>> */
  900. #ifdef USE_JPWL
  901. /* ------------------------------------------------------ */
  902. case 'W': { /* JPWL capabilities switched on */
  903. char *token = NULL;
  904. int hprot, pprot, sens, addr, size, range;
  905. /* we need to enable indexing */
  906. if (!indexfilename) {
  907. strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN);
  908. }
  909. /* search for different protection methods */
  910. /* break the option in comma points and parse the result */
  911. token = strtok(opj_optarg, ",");
  912. while (token != NULL) {
  913. /* search header error protection method */
  914. if (*token == 'h') {
  915. static int tile = 0, tilespec = 0, lasttileno = 0;
  916. hprot = 1; /* predefined method */
  917. if (sscanf(token, "h=%d", &hprot) == 1) {
  918. /* Main header, specified */
  919. if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
  920. ((hprot >= 37) && (hprot <= 128)))) {
  921. fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n",
  922. hprot);
  923. return 1;
  924. }
  925. parameters->jpwl_hprot_MH = hprot;
  926. } else if (sscanf(token, "h%d=%d", &tile, &hprot) == 2) {
  927. /* Tile part header, specified */
  928. if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) ||
  929. ((hprot >= 37) && (hprot <= 128)))) {
  930. fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n",
  931. hprot);
  932. return 1;
  933. }
  934. if (tile < 0) {
  935. fprintf(stderr,
  936. "ERROR -> invalid tile part number on protection method t = %d\n", tile);
  937. return 1;
  938. }
  939. if (tilespec < JPWL_MAX_NO_TILESPECS) {
  940. parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
  941. parameters->jpwl_hprot_TPH[tilespec++] = hprot;
  942. }
  943. } else if (sscanf(token, "h%d", &tile) == 1) {
  944. /* Tile part header, unspecified */
  945. if (tile < 0) {
  946. fprintf(stderr,
  947. "ERROR -> invalid tile part number on protection method t = %d\n", tile);
  948. return 1;
  949. }
  950. if (tilespec < JPWL_MAX_NO_TILESPECS) {
  951. parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile;
  952. parameters->jpwl_hprot_TPH[tilespec++] = hprot;
  953. }
  954. } else if (!strcmp(token, "h")) {
  955. /* Main header, unspecified */
  956. parameters->jpwl_hprot_MH = hprot;
  957. } else {
  958. fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
  959. return 1;
  960. };
  961. }
  962. /* search packet error protection method */
  963. if (*token == 'p') {
  964. static int pack = 0, tile = 0, packspec = 0, lastpackno = 0;
  965. pprot = 1; /* predefined method */
  966. if (sscanf(token, "p=%d", &pprot) == 1) {
  967. /* Method for all tiles and all packets */
  968. if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  969. ((pprot >= 37) && (pprot <= 128)))) {
  970. fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n",
  971. pprot);
  972. return 1;
  973. }
  974. parameters->jpwl_pprot_tileno[0] = 0;
  975. parameters->jpwl_pprot_packno[0] = 0;
  976. parameters->jpwl_pprot[0] = pprot;
  977. } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) {
  978. /* method specified from that tile on */
  979. if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  980. ((pprot >= 37) && (pprot <= 128)))) {
  981. fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
  982. return 1;
  983. }
  984. if (tile < 0) {
  985. fprintf(stderr,
  986. "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  987. return 1;
  988. }
  989. if (packspec < JPWL_MAX_NO_PACKSPECS) {
  990. parameters->jpwl_pprot_tileno[packspec] = tile;
  991. parameters->jpwl_pprot_packno[packspec] = 0;
  992. parameters->jpwl_pprot[packspec++] = pprot;
  993. }
  994. } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) {
  995. /* method fully specified from that tile and that packet on */
  996. if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  997. ((pprot >= 37) && (pprot <= 128)))) {
  998. fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
  999. return 1;
  1000. }
  1001. if (tile < 0) {
  1002. fprintf(stderr,
  1003. "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1004. return 1;
  1005. }
  1006. if (pack < 0) {
  1007. fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
  1008. pack);
  1009. return 1;
  1010. }
  1011. if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1012. parameters->jpwl_pprot_tileno[packspec] = tile;
  1013. parameters->jpwl_pprot_packno[packspec] = pack;
  1014. parameters->jpwl_pprot[packspec++] = pprot;
  1015. }
  1016. } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) {
  1017. /* default method from that tile and that packet on */
  1018. if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) ||
  1019. ((pprot >= 37) && (pprot <= 128)))) {
  1020. fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot);
  1021. return 1;
  1022. }
  1023. if (tile < 0) {
  1024. fprintf(stderr,
  1025. "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1026. return 1;
  1027. }
  1028. if (pack < 0) {
  1029. fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n",
  1030. pack);
  1031. return 1;
  1032. }
  1033. if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1034. parameters->jpwl_pprot_tileno[packspec] = tile;
  1035. parameters->jpwl_pprot_packno[packspec] = pack;
  1036. parameters->jpwl_pprot[packspec++] = pprot;
  1037. }
  1038. } else if (sscanf(token, "p%d", &tile) == 1) {
  1039. /* default from a tile on */
  1040. if (tile < 0) {
  1041. fprintf(stderr,
  1042. "ERROR -> invalid tile part number on protection method p = %d\n", tile);
  1043. return 1;
  1044. }
  1045. if (packspec < JPWL_MAX_NO_PACKSPECS) {
  1046. parameters->jpwl_pprot_tileno[packspec] = tile;
  1047. parameters->jpwl_pprot_packno[packspec] = 0;
  1048. parameters->jpwl_pprot[packspec++] = pprot;
  1049. }
  1050. } else if (!strcmp(token, "p")) {
  1051. /* all default */
  1052. parameters->jpwl_pprot_tileno[0] = 0;
  1053. parameters->jpwl_pprot_packno[0] = 0;
  1054. parameters->jpwl_pprot[0] = pprot;
  1055. } else {
  1056. fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token);
  1057. return 1;
  1058. };
  1059. }
  1060. /* search sensitivity method */
  1061. if (*token == 's') {
  1062. static int tile = 0, tilespec = 0, lasttileno = 0;
  1063. sens = 0; /* predefined: relative error */
  1064. if (sscanf(token, "s=%d", &sens) == 1) {
  1065. /* Main header, specified */
  1066. if ((sens < -1) || (sens > 7)) {
  1067. fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n",
  1068. sens);
  1069. return 1;
  1070. }
  1071. parameters->jpwl_sens_MH = sens;
  1072. } else if (sscanf(token, "s%d=%d", &tile, &sens) == 2) {
  1073. /* Tile part header, specified */
  1074. if ((sens < -1) || (sens > 7)) {
  1075. fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n",
  1076. sens);
  1077. return 1;
  1078. }
  1079. if (tile < 0) {
  1080. fprintf(stderr,
  1081. "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
  1082. return 1;
  1083. }
  1084. if (tilespec < JPWL_MAX_NO_TILESPECS) {
  1085. parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
  1086. parameters->jpwl_sens_TPH[tilespec++] = sens;
  1087. }
  1088. } else if (sscanf(token, "s%d", &tile) == 1) {
  1089. /* Tile part header, unspecified */
  1090. if (tile < 0) {
  1091. fprintf(stderr,
  1092. "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile);
  1093. return 1;
  1094. }
  1095. if (tilespec < JPWL_MAX_NO_TILESPECS) {
  1096. parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile;
  1097. parameters->jpwl_sens_TPH[tilespec++] = hprot;
  1098. }
  1099. } else if (!strcmp(token, "s")) {
  1100. /* Main header, unspecified */
  1101. parameters->jpwl_sens_MH = sens;
  1102. } else {
  1103. fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token);
  1104. return 1;
  1105. };
  1106. parameters->jpwl_sens_size = 2; /* 2 bytes for default size */
  1107. }
  1108. /* search addressing size */
  1109. if (*token == 'a') {
  1110. static int tile = 0, tilespec = 0, lasttileno = 0;
  1111. addr = 0; /* predefined: auto */
  1112. if (sscanf(token, "a=%d", &addr) == 1) {
  1113. /* Specified */
  1114. if ((addr != 0) && (addr != 2) && (addr != 4)) {
  1115. fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr);
  1116. return 1;
  1117. }
  1118. parameters->jpwl_sens_addr = addr;
  1119. } else if (!strcmp(token, "a")) {
  1120. /* default */
  1121. parameters->jpwl_sens_addr = addr; /* auto for default size */
  1122. } else {
  1123. fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token);
  1124. return 1;
  1125. };
  1126. }
  1127. /* search sensitivity size */
  1128. if (*token == 'z') {
  1129. static int tile = 0, tilespec = 0, lasttileno = 0;
  1130. size = 1; /* predefined: 1 byte */
  1131. if (sscanf(token, "z=%d", &size) == 1) {
  1132. /* Specified */
  1133. if ((size != 0) && (size != 1) && (size != 2)) {
  1134. fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size);
  1135. return 1;
  1136. }
  1137. parameters->jpwl_sens_size = size;
  1138. } else if (!strcmp(token, "a")) {
  1139. /* default */
  1140. parameters->jpwl_sens_size = size; /* 1 for default size */
  1141. } else {
  1142. fprintf(stderr, "ERROR -> invalid size selection = %s\n", token);
  1143. return 1;
  1144. };
  1145. }
  1146. /* search range method */
  1147. if (*token == 'g') {
  1148. static int tile = 0, tilespec = 0, lasttileno = 0;
  1149. range = 0; /* predefined: 0 (packet) */
  1150. if (sscanf(token, "g=%d", &range) == 1) {
  1151. /* Specified */
  1152. if ((range < 0) || (range > 3)) {
  1153. fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range);
  1154. return 1;
  1155. }
  1156. parameters->jpwl_sens_range = range;
  1157. } else if (!strcmp(token, "g")) {
  1158. /* default */
  1159. parameters->jpwl_sens_range = range;
  1160. } else {
  1161. fprintf(stderr, "ERROR -> invalid range selection = %s\n", token);
  1162. return 1;
  1163. };
  1164. }
  1165. /* next token or bust */
  1166. token = strtok(NULL, ",");
  1167. };
  1168. /* some info */
  1169. fprintf(stdout, "Info: JPWL capabilities enabled\n");
  1170. parameters->jpwl_epc_on = true;
  1171. }
  1172. break;
  1173. #endif /* USE_JPWL */
  1174. /* <<UniPG */
  1175. /* ------------------------------------------------------ */
  1176. break;
  1177. /* ------------------------------------------------------ */
  1178. default:
  1179. fprintf(stderr, "ERROR -> Command line not valid\n");
  1180. return 1;
  1181. }
  1182. }
  1183. /* check for possible errors */
  1184. if (parameters->cp_cinema) {
  1185. if (parameters->tcp_numlayers > 1) {
  1186. parameters->cp_rsiz = STD_RSIZ;
  1187. fprintf(stdout,
  1188. "Warning: DC profiles do not allow more than one quality layer. The codestream created will not be compliant with the DC profile\n");
  1189. }
  1190. }
  1191. if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc ||
  1192. parameters->cp_fixed_quality)
  1193. && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^
  1194. parameters->cp_fixed_quality))) {
  1195. fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
  1196. return 1;
  1197. } /* mod fixed_quality */
  1198. /* if no rate entered, lossless by default */
  1199. if (parameters->tcp_numlayers == 0) {
  1200. parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
  1201. parameters->tcp_numlayers++;
  1202. parameters->cp_disto_alloc = 1;
  1203. }
  1204. if ((parameters->cp_tx0 > parameters->image_offset_x0) ||
  1205. (parameters->cp_ty0 > parameters->image_offset_y0)) {
  1206. fprintf(stderr,
  1207. "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
  1208. parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0,
  1209. parameters->image_offset_y0);
  1210. return 1;
  1211. }
  1212. for (i = 0; i < parameters->numpocs; i++) {
  1213. if (parameters->POC[i].prg == -1) {
  1214. fprintf(stderr,
  1215. "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
  1216. i + 1);
  1217. }
  1218. }
  1219. return 0;
  1220. }
  1221. /** Create the same index as j2k_create_index does, but in an int[] instead of in a file ==> easy to pass it back to Java, to transfer it etc.
  1222. @param buffer_size, increased by the length of the compressed index, in number of bytes
  1223. @return a pointer to a char[]
  1224. Syntax of the index:
  1225. one char for the version number (1): one byte because no problem with little endian, big endian etc.
  1226. one int for each of the following information:
  1227. Image Width
  1228. Image Height
  1229. progression order
  1230. Tile width
  1231. Tile height
  1232. Nb tiles in X
  1233. Nb tiles in Y
  1234. Nb of components
  1235. Nb of layers
  1236. Nb of resolutions
  1237. for each resolution:
  1238. Precinct width
  1239. Precinct height
  1240. End main header position
  1241. codestream size
  1242. For each tile:
  1243. tile number
  1244. tile start pos in codestream
  1245. tile header end position
  1246. tile end position in codestream
  1247. For each LRCP, RLCP etc.:
  1248. packet number
  1249. tile number
  1250. layer number
  1251. resolution number
  1252. component number
  1253. precinct number
  1254. start position in the codestream
  1255. end position of this packet
  1256. */
  1257. static char* create_index_into_byte_array(opj_codestream_info_t *cstr_info,
  1258. int* buffer_size)
  1259. {
  1260. int tileno, compno, layno, resno, precno, pack_nb, x, y;
  1261. char* buffer = NULL;
  1262. int buffer_pos = 0;
  1263. int prec_max = 0;
  1264. prec_max = 0;
  1265. for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  1266. for (resno = 0; resno < cstr_info->numdecompos[0] + 1; resno++) {
  1267. prec_max = int_max(prec_max,
  1268. cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]);
  1269. }
  1270. }
  1271. /* Compute the size of the index buffer, in number of bytes*/
  1272. *buffer_size =
  1273. 1 /* version */
  1274. + (10 /* image_w until decomposition */
  1275. + (cstr_info->numdecompos[0] + 1) * 2 /* pdx size for each tile */
  1276. + 2 /* main_head_end + codestream_size */
  1277. + cstr_info->tw * cstr_info->th * 4 /* tile info, without distorsion info */
  1278. + cstr_info->tw * cstr_info->th * cstr_info->numlayers *
  1279. (cstr_info->numdecompos[0] + 1) * cstr_info->numcomps * prec_max * 8
  1280. ) * sizeof(int);
  1281. /*printf("C: index buffer size = %d bytes\n", *buffer_size);*/
  1282. buffer = (char*) opj_malloc(*buffer_size);
  1283. if (!buffer) {
  1284. /*opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to allocate index buffer for writing %d int\n", *buffer_size);*/
  1285. fprintf(stderr, "failed to allocate index buffer for writing %d int\n",
  1286. *buffer_size);
  1287. return 0;
  1288. }
  1289. buffer[0] = 1; /* Version stored on a byte*/
  1290. buffer++;
  1291. /* Remaining information are stored on a int.*/
  1292. ((int*)buffer)[buffer_pos++] = cstr_info->image_w;
  1293. ((int*)buffer)[buffer_pos++] = cstr_info->image_h;
  1294. ((int*)buffer)[buffer_pos++] = cstr_info->prog;
  1295. ((int*)buffer)[buffer_pos++] = cstr_info->tile_x;
  1296. ((int*)buffer)[buffer_pos++] = cstr_info->tile_y;
  1297. ((int*)buffer)[buffer_pos++] = cstr_info->tw;
  1298. ((int*)buffer)[buffer_pos++] = cstr_info->th;
  1299. ((int*)buffer)[buffer_pos++] = cstr_info->numcomps;
  1300. ((int*)buffer)[buffer_pos++] = cstr_info->numlayers;
  1301. ((int*)buffer)[buffer_pos++] = cstr_info->numdecompos[0];
  1302. for (resno = cstr_info->numdecompos[0]; resno >= 0; resno--) {
  1303. /* based on tile 0 */
  1304. ((int*)buffer)[buffer_pos++] = (1 << cstr_info->tile[0].pdx[resno]);
  1305. ((int*)buffer)[buffer_pos++] = (1 << cstr_info->tile[0].pdx[resno]);
  1306. }
  1307. ((int*)buffer)[buffer_pos++] = cstr_info->main_head_end;
  1308. ((int*)buffer)[buffer_pos++] = cstr_info->codestream_size;
  1309. for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  1310. ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].tileno;
  1311. ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].start_pos;
  1312. ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].end_header;
  1313. ((int*)buffer)[buffer_pos++] = cstr_info->tile[tileno].end_pos;
  1314. }
  1315. for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  1316. int start_pos, end_pos;
  1317. int max_numdecompos = 0;
  1318. pack_nb = 0;
  1319. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1320. if (max_numdecompos < cstr_info->numdecompos[compno]) {
  1321. max_numdecompos = cstr_info->numdecompos[compno];
  1322. }
  1323. }
  1324. if (cstr_info->prog == LRCP) { /* LRCP */
  1325. for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1326. for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1327. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1328. int prec_max;
  1329. if (resno > cstr_info->numdecompos[compno]) {
  1330. break;
  1331. }
  1332. prec_max = cstr_info->tile[tileno].pw[resno] *
  1333. cstr_info->tile[tileno].ph[resno];
  1334. for (precno = 0; precno < prec_max; precno++) {
  1335. start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1336. end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1337. ((int*)buffer)[buffer_pos++] = pack_nb;
  1338. ((int*)buffer)[buffer_pos++] = tileno;
  1339. ((int*)buffer)[buffer_pos++] = layno;
  1340. ((int*)buffer)[buffer_pos++] = resno;
  1341. ((int*)buffer)[buffer_pos++] = compno;
  1342. ((int*)buffer)[buffer_pos++] = precno;
  1343. ((int*)buffer)[buffer_pos++] = start_pos;
  1344. ((int*)buffer)[buffer_pos++] = end_pos;
  1345. pack_nb++;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. } /* LRCP */
  1351. else if (cstr_info->prog == RLCP) { /* RLCP */
  1352. for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1353. for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1354. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1355. int prec_max;
  1356. if (resno > cstr_info->numdecompos[compno]) {
  1357. break;
  1358. }
  1359. prec_max = cstr_info->tile[tileno].pw[resno] *
  1360. cstr_info->tile[tileno].ph[resno];
  1361. for (precno = 0; precno < prec_max; precno++) {
  1362. start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1363. end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1364. ((int*)buffer)[buffer_pos++] = pack_nb;
  1365. ((int*)buffer)[buffer_pos++] = tileno;
  1366. ((int*)buffer)[buffer_pos++] = resno;
  1367. ((int*)buffer)[buffer_pos++] = layno;
  1368. ((int*)buffer)[buffer_pos++] = compno;
  1369. ((int*)buffer)[buffer_pos++] = precno;
  1370. ((int*)buffer)[buffer_pos++] = start_pos;
  1371. ((int*)buffer)[buffer_pos++] = end_pos;
  1372. pack_nb++;
  1373. }
  1374. }
  1375. }
  1376. }
  1377. } /* RLCP */
  1378. else if (cstr_info->prog == RPCL) { /* RPCL */
  1379. for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1380. /* I suppose components have same XRsiz, YRsiz */
  1381. int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
  1382. (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
  1383. int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
  1384. (float)cstr_info->tw) * cstr_info->tile_y;
  1385. int x1 = x0 + cstr_info->tile_x;
  1386. int y1 = y0 + cstr_info->tile_y;
  1387. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1388. int prec_max;
  1389. if (resno > cstr_info->numdecompos[compno]) {
  1390. break;
  1391. }
  1392. prec_max = cstr_info->tile[tileno].pw[resno] *
  1393. cstr_info->tile[tileno].ph[resno];
  1394. for (precno = 0; precno < prec_max; precno++) {
  1395. int pcnx = cstr_info->tile[tileno].pw[resno];
  1396. int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
  1397. cstr_info->numdecompos[compno] - resno);
  1398. int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
  1399. cstr_info->numdecompos[compno] - resno);
  1400. int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
  1401. int precno_y = (int) floor((float)precno / (float)pcnx);
  1402. for (y = y0; y < y1; y++) {
  1403. if (precno_y * pcy == y) {
  1404. for (x = x0; x < x1; x++) {
  1405. if (precno_x * pcx == x) {
  1406. for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1407. start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1408. end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1409. ((int*)buffer)[buffer_pos++] = pack_nb;
  1410. ((int*)buffer)[buffer_pos++] = tileno;
  1411. ((int*)buffer)[buffer_pos++] = resno;
  1412. ((int*)buffer)[buffer_pos++] = precno;
  1413. ((int*)buffer)[buffer_pos++] = compno;
  1414. ((int*)buffer)[buffer_pos++] = layno;
  1415. ((int*)buffer)[buffer_pos++] = start_pos;
  1416. ((int*)buffer)[buffer_pos++] = end_pos;
  1417. pack_nb++;
  1418. }
  1419. }
  1420. }/* x = x0..x1 */
  1421. }
  1422. } /* y = y0..y1 */
  1423. } /* precno */
  1424. } /* compno */
  1425. } /* resno */
  1426. } /* RPCL */
  1427. else if (cstr_info->prog == PCRL) { /* PCRL */
  1428. /* I suppose components have same XRsiz, YRsiz */
  1429. int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
  1430. (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
  1431. int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
  1432. (float)cstr_info->tw) * cstr_info->tile_y;
  1433. int x1 = x0 + cstr_info->tile_x;
  1434. int y1 = y0 + cstr_info->tile_y;
  1435. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1436. for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1437. int prec_max;
  1438. if (resno > cstr_info->numdecompos[compno]) {
  1439. break;
  1440. }
  1441. prec_max = cstr_info->tile[tileno].pw[resno] *
  1442. cstr_info->tile[tileno].ph[resno];
  1443. for (precno = 0; precno < prec_max; precno++) {
  1444. int pcnx = cstr_info->tile[tileno].pw[resno];
  1445. int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
  1446. cstr_info->numdecompos[compno] - resno);
  1447. int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
  1448. cstr_info->numdecompos[compno] - resno);
  1449. int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
  1450. int precno_y = (int) floor((float)precno / (float)pcnx);
  1451. for (y = y0; y < y1; y++) {
  1452. if (precno_y * pcy == y) {
  1453. for (x = x0; x < x1; x++) {
  1454. if (precno_x * pcx == x) {
  1455. for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1456. start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1457. end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1458. ((int*)buffer)[buffer_pos++] = pack_nb;
  1459. ((int*)buffer)[buffer_pos++] = tileno;
  1460. ((int*)buffer)[buffer_pos++] = precno;
  1461. ((int*)buffer)[buffer_pos++] = compno;
  1462. ((int*)buffer)[buffer_pos++] = resno;
  1463. ((int*)buffer)[buffer_pos++] = layno;
  1464. ((int*)buffer)[buffer_pos++] = start_pos;
  1465. ((int*)buffer)[buffer_pos++] = end_pos;
  1466. pack_nb++;
  1467. }
  1468. }
  1469. }/* x = x0..x1 */
  1470. }
  1471. } /* y = y0..y1 */
  1472. } /* precno */
  1473. } /* resno */
  1474. } /* compno */
  1475. } /* PCRL */
  1476. else { /* CPRL */
  1477. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1478. /* I suppose components have same XRsiz, YRsiz */
  1479. int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno /
  1480. (float)cstr_info->tw) * cstr_info->tw * cstr_info->tile_x;
  1481. int y0 = cstr_info->tile_Ox + (int)floor((float)tileno /
  1482. (float)cstr_info->tw) * cstr_info->tile_y;
  1483. int x1 = x0 + cstr_info->tile_x;
  1484. int y1 = y0 + cstr_info->tile_y;
  1485. for (resno = 0; resno < max_numdecompos + 1; resno++) {
  1486. int prec_max;
  1487. if (resno > cstr_info->numdecompos[compno]) {
  1488. break;
  1489. }
  1490. prec_max = cstr_info->tile[tileno].pw[resno] *
  1491. cstr_info->tile[tileno].ph[resno];
  1492. for (precno = 0; precno < prec_max; precno++) {
  1493. int pcnx = cstr_info->tile[tileno].pw[resno];
  1494. int pcx = (int) pow(2, cstr_info->tile[tileno].pdx[resno] +
  1495. cstr_info->numdecompos[compno] - resno);
  1496. int pcy = (int) pow(2, cstr_info->tile[tileno].pdy[resno] +
  1497. cstr_info->numdecompos[compno] - resno);
  1498. int precno_x = precno - (int) floor((float)precno / (float)pcnx) * pcnx;
  1499. int precno_y = (int) floor((float)precno / (float)pcnx);
  1500. for (y = y0; y < y1; y++) {
  1501. if (precno_y * pcy == y) {
  1502. for (x = x0; x < x1; x++) {
  1503. if (precno_x * pcx == x) {
  1504. for (layno = 0; layno < cstr_info->numlayers; layno++) {
  1505. start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
  1506. end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
  1507. ((int*)buffer)[buffer_pos++] = pack_nb;
  1508. ((int*)buffer)[buffer_pos++] = tileno;
  1509. ((int*)buffer)[buffer_pos++] = compno;
  1510. ((int*)buffer)[buffer_pos++] = precno;
  1511. ((int*)buffer)[buffer_pos++] = resno;
  1512. ((int*)buffer)[buffer_pos++] = layno;
  1513. ((int*)buffer)[buffer_pos++] = start_pos;
  1514. ((int*)buffer)[buffer_pos++] = end_pos;
  1515. pack_nb++;
  1516. }
  1517. }
  1518. }/* x = x0..x1 */
  1519. }
  1520. } /* y = y0..y1 */
  1521. } /* precno */
  1522. } /* resno */
  1523. } /* compno */
  1524. } /* CPRL */
  1525. } /* tileno */
  1526. if (buffer_pos > *buffer_size) {
  1527. /*opj_event_msg(j2k->cinfo, EVT_ERROR, "index creation: buffer_pos (%d) > buffer_size (%d)!\n", buffer_pos, *buffer_size);*/
  1528. fprintf(stderr, "index creation: buffer_pos (%d) > buffer_size (%d)!\n",
  1529. buffer_pos, *buffer_size);
  1530. return 0;
  1531. }
  1532. return --buffer;
  1533. }
  1534. /* --------------------------------------------------------------------------
  1535. ------------ Get the image byte[] from the Java object -------------------*/
  1536. static opj_image_t* loadImage(opj_cparameters_t *parameters, JNIEnv *env,
  1537. jobject obj, jclass cls)
  1538. {
  1539. int i, max, shift, w, h, depth;
  1540. opj_image_t * img = NULL;
  1541. int compno, numcomps;
  1542. opj_image_t * image = NULL;
  1543. opj_image_comp_t *comp;
  1544. opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */
  1545. OPJ_COLOR_SPACE color_space;
  1546. jfieldID fid;
  1547. jint ji;
  1548. jbyteArray jba;
  1549. jshortArray jsa;
  1550. jintArray jia;
  1551. int len;
  1552. jbyte *jbBody;
  1553. jshort *jsBody;
  1554. jint *jiBody;
  1555. jboolean isCopy;
  1556. /* Image width, height and depth*/
  1557. fid = (*env)->GetFieldID(env, cls, "width", "I");
  1558. ji = (*env)->GetIntField(env, obj, fid);
  1559. w = ji;
  1560. fid = (*env)->GetFieldID(env, cls, "height", "I");
  1561. ji = (*env)->GetIntField(env, obj, fid);
  1562. h = ji;
  1563. fid = (*env)->GetFieldID(env, cls, "depth", "I");
  1564. ji = (*env)->GetIntField(env, obj, fid);
  1565. depth = ji;
  1566. /* Read the image*/
  1567. if (depth <= 16) {
  1568. numcomps = 1;
  1569. color_space = CLRSPC_GRAY;
  1570. } else {
  1571. numcomps = 3;
  1572. color_space = CLRSPC_SRGB;
  1573. }
  1574. memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
  1575. if (numcomps == 1) {
  1576. cmptparm[0].x0 = parameters->image_offset_x0;
  1577. cmptparm[0].y0 = parameters->image_offset_y0;
  1578. cmptparm[0].w = !cmptparm[0].x0 ? (w - 1) * parameters->subsampling_dx + 1 :
  1579. cmptparm[0].x0 + (w - 1) * parameters->subsampling_dx + 1;
  1580. cmptparm[0].h = !cmptparm[0].y0 ? (h - 1) * parameters->subsampling_dy + 1 :
  1581. cmptparm[0].y0 + (h - 1) * parameters->subsampling_dy + 1;
  1582. /* Java types are always signed but we use them as unsigned types (shift of the negative part of
  1583. the pixels of the images in Telemis before entering the encoder).*/
  1584. cmptparm[0].sgnd = 0;
  1585. if (depth <= 16) {
  1586. cmptparm[0].prec = depth;
  1587. } else {
  1588. cmptparm[0].prec = 8;
  1589. }
  1590. cmptparm[0].bpp = cmptparm[0].prec;
  1591. cmptparm[0].dx = parameters->subsampling_dx;
  1592. cmptparm[0].dy = parameters->subsampling_dy;
  1593. /*printf("C: component 0 initialised: x0=%d, y0=%d, w=%d, h=%d, sgnd=%d, bpp=%d, dx=%d, dy=%d, color_space=%d\n", cmptparm[0].x0, cmptparm[0].y0, cmptparm[0].w,
  1594. cmptparm[0].h, cmptparm[0].sgnd, cmptparm[0].bpp, cmptparm[0].dx, cmptparm[0].dy, color_space);*/
  1595. } else {
  1596. for (i = 0; i < numcomps; i++) {
  1597. cmptparm[i].prec = 8;
  1598. cmptparm[i].bpp = 8;
  1599. cmptparm[i].sgnd = 0;
  1600. cmptparm[i].dx = parameters->subsampling_dx;
  1601. cmptparm[i].dy = parameters->subsampling_dy;
  1602. cmptparm[i].w = w;
  1603. cmptparm[i].h = h;
  1604. }
  1605. }
  1606. /* create the image */
  1607. image = opj_image_create(numcomps, &cmptparm[0], color_space);
  1608. if (!image) {
  1609. return NULL;
  1610. }
  1611. if (depth <= 16) {
  1612. image->numcomps = 1;
  1613. } else {
  1614. image->numcomps = 3;
  1615. }
  1616. /* set image offset and reference grid */
  1617. image->x0 = cmptparm[0].x0;
  1618. image->y0 = cmptparm[0].x0;
  1619. image->x1 = cmptparm[0].w;
  1620. image->y1 = cmptparm[0].h;
  1621. /* set image data */
  1622. for (compno = 0; compno < numcomps; compno++) {
  1623. comp = &image->comps[compno];
  1624. max = -100000;
  1625. if (depth == 8) {
  1626. fid = (*env)->GetFieldID(env, cls, "image8", "[B"); /* byteArray []*/
  1627. jba = (*env)->GetObjectField(env, obj, fid);
  1628. len = (*env)->GetArrayLength(env, jba);
  1629. jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, &isCopy);
  1630. /*printf("C: before transferring 8 bpp image\n");*/
  1631. if (comp->sgnd) {
  1632. for (i = 0; i < len; i++) {
  1633. comp->data[i] = (char) jbBody[i];
  1634. if (comp->data[i] > max) {
  1635. max = comp->data[i];
  1636. }
  1637. }
  1638. } else {
  1639. for (i = 0; i < len; i++) {
  1640. comp->data[i] = (unsigned char) jbBody[i];
  1641. if (comp->data[i] > max) {
  1642. max = comp->data[i];
  1643. }
  1644. }
  1645. }
  1646. (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
  1647. } else if (depth == 16) {
  1648. fid = (*env)->GetFieldID(env, cls, "image16", "[S"); /* shortArray []*/
  1649. jsa = (*env)->GetObjectField(env, obj, fid);
  1650. len = (*env)->GetArrayLength(env, jsa);
  1651. jsBody = (*env)->GetPrimitiveArrayCritical(env, jsa, &isCopy);
  1652. /*printf("C: before transferring 16 bpp image\n");*/
  1653. if (comp->sgnd) { /* Special behaviour to deal with signed elements ??*/
  1654. comp->data[i] = (short) jsBody[i];
  1655. for (i = 0; i < len; i++) {
  1656. if (comp->data[i] > max) {
  1657. max = comp->data[i];
  1658. }
  1659. }
  1660. } else {
  1661. for (i = 0; i < len; i++) {
  1662. comp->data[i] = (unsigned short) jsBody[i];
  1663. if (comp->data[i] > max) {
  1664. max = comp->data[i];
  1665. }
  1666. }
  1667. }
  1668. (*env)->ReleasePrimitiveArrayCritical(env, jsa, jsBody, 0);
  1669. } else if (depth == 24) {
  1670. fid = (*env)->GetFieldID(env, cls, "image24", "[I"); /* intArray []*/
  1671. jia = (*env)->GetObjectField(env, obj, fid);
  1672. len = (*env)->GetArrayLength(env, jia);
  1673. shift = compno * 8;
  1674. jiBody = (*env)->GetPrimitiveArrayCritical(env, jia, &isCopy);
  1675. /*printf("C: before transferring 24 bpp image (component %d, signed = %d)\n", compno, comp->sgnd);*/
  1676. if (comp->sgnd) { /* Special behaviour to deal with signed elements ?? XXXXX*/
  1677. for (i = 0; i < len; i++) {
  1678. comp->data[i] = (((int) jiBody[i]) & (0xFF << shift)) >> shift;
  1679. if (comp->data[i] > max) {
  1680. max = comp->data[i];
  1681. }
  1682. }
  1683. } else {
  1684. for (i = 0; i < len; i++) {
  1685. comp->data[i] = (((unsigned int) jiBody[i]) & (0xFF << shift)) >> shift;
  1686. if (comp->data[i] > max) {
  1687. max = comp->data[i];
  1688. }
  1689. }
  1690. }
  1691. (*env)->ReleasePrimitiveArrayCritical(env, jia, jiBody, 0);
  1692. }
  1693. comp->bpp = int_floorlog2(max) + 1;
  1694. comp->prec = comp->bpp;
  1695. /*printf("C: component %d: max %d, real bpp = %d\n", compno, max, comp->bpp);*/
  1696. }
  1697. return image;
  1698. }
  1699. /* --------------------------------------------------------------------------
  1700. -------------------- MAIN METHOD, CALLED BY JAVA -----------------------*/
  1701. JNIEXPORT jlong JNICALL
  1702. Java_org_openJpeg_OpenJPEGJavaEncoder_internalEncodeImageToJ2K(JNIEnv *env,
  1703. jobject obj, jobjectArray javaParameters)
  1704. {
  1705. int argc; /* To simulate the command line parameters (taken from the javaParameters variable) and be able to re-use the */
  1706. char **argv; /* 'parse_cmdline_decoder' method taken from the j2k_to_image project */
  1707. opj_bool bSuccess;
  1708. opj_cparameters_t parameters; /* compression parameters */
  1709. img_fol_t img_fol;
  1710. opj_event_mgr_t event_mgr; /* event manager */
  1711. opj_image_t *image = NULL;
  1712. int i, j, num_images;
  1713. int imageno;
  1714. opj_codestream_info_t cstr_info; /* Codestream information structure */
  1715. char indexfilename[OPJ_PATH_LEN]; /* index file name */
  1716. char* compressed_index = NULL;
  1717. int compressed_index_size = -1;
  1718. /* ==> Access variables to the Java member variables*/
  1719. jsize arraySize;
  1720. jclass cls;
  1721. jobject object;
  1722. jboolean isCopy;
  1723. jfieldID fid;
  1724. jbyteArray jba;
  1725. jbyte *jbBody;
  1726. callback_variables_t msgErrorCallback_vars;
  1727. /* <== access variable to the Java member variables.*/
  1728. /* For the encoding and storage into the file*/
  1729. opj_cinfo_t* cinfo;
  1730. int codestream_length;
  1731. opj_cio_t *cio = NULL;
  1732. FILE *f = NULL;
  1733. /* JNI reference to the calling class*/
  1734. cls = (*env)->GetObjectClass(env, obj);
  1735. /* Pointers to be able to call a Java method for all the info and error messages*/
  1736. msgErrorCallback_vars.env = env;
  1737. msgErrorCallback_vars.jobj = &obj;
  1738. msgErrorCallback_vars.message_mid = (*env)->GetMethodID(env, cls, "logMessage",
  1739. "(Ljava/lang/String;)V");
  1740. msgErrorCallback_vars.error_mid = (*env)->GetMethodID(env, cls, "logError",
  1741. "(Ljava/lang/String;)V");
  1742. arraySize = (*env)->GetArrayLength(env, javaParameters);
  1743. argc = (int) arraySize + 1;
  1744. argv = opj_malloc(argc * sizeof(char*));
  1745. argv[0] = "ProgramName.exe"; /* The program name: useless*/
  1746. j = 0;
  1747. for (i = 1; i < argc; i++) {
  1748. object = (*env)->GetObjectArrayElement(env, javaParameters, i - 1);
  1749. argv[i] = (char*)(*env)->GetStringUTFChars(env, object, &isCopy);
  1750. }
  1751. /*printf("C: ");
  1752. for (i=0; i<argc; i++) {
  1753. printf("[%s]",argv[i]);
  1754. }
  1755. printf("\n");*/
  1756. /*
  1757. configure the event callbacks
  1758. */
  1759. memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
  1760. event_mgr.error_handler = error_callback;
  1761. event_mgr.warning_handler = warning_callback;
  1762. event_mgr.info_handler = info_callback;
  1763. /* set encoding parameters to default values */
  1764. opj_set_default_encoder_parameters(&parameters);
  1765. parameters.cod_format = J2K_CFMT;
  1766. /*parameters.index_on = 1;*/
  1767. /* Initialize indexfilename and img_fol */
  1768. *indexfilename = 0;
  1769. memset(&img_fol, 0, sizeof(img_fol_t));
  1770. /* parse input and get user encoding parameters */
  1771. if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol,
  1772. indexfilename) == 1) {
  1773. /* Release the Java arguments array*/
  1774. for (i = 1; i < argc; i++) {
  1775. (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env,
  1776. javaParameters, i - 1), argv[i]);
  1777. }
  1778. return -1;
  1779. }
  1780. /* Release the Java arguments array*/
  1781. for (i = 1; i < argc; i++) {
  1782. (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env,
  1783. javaParameters, i - 1), argv[i]);
  1784. }
  1785. if (parameters.cp_cinema) {
  1786. cinema_parameters(&parameters);
  1787. }
  1788. /* Create comment for codestream */
  1789. if (parameters.cp_comment == NULL) {
  1790. const char comment[] = "Created by JavaOpenJPEG version ";
  1791. const size_t clen = strlen(comment);
  1792. const char *version = opj_version();
  1793. /* UniPG>> */
  1794. #ifdef USE_JPWL
  1795. parameters.cp_comment = (char*)opj_malloc(clen + strlen(version) + 11);
  1796. sprintf(parameters.cp_comment, "%s%s with JPWL", comment, version);
  1797. #else
  1798. parameters.cp_comment = (char*)opj_malloc(clen + strlen(version) + 1);
  1799. sprintf(parameters.cp_comment, "%s%s", comment, version);
  1800. #endif
  1801. /* <<UniPG */
  1802. }
  1803. /* Read directory if necessary */
  1804. num_images = 1;
  1805. /*Encoding image one by one*/
  1806. for (imageno = 0; imageno < num_images; imageno++) {
  1807. image = NULL;
  1808. fprintf(stderr, "\n");
  1809. image = loadImage(&parameters, env, obj, cls);
  1810. /*printf("C: after load image: image = %d\n", image);*/
  1811. if (!image) {
  1812. fprintf(stderr, "Unable to load image\n");
  1813. return -1;
  1814. }
  1815. /* Decide if MCT should be used */
  1816. parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
  1817. if (parameters.cp_cinema) {
  1818. cinema_setup_encoder(&parameters, image, &img_fol);
  1819. }
  1820. /* encode the destination image */
  1821. /* ---------------------------- */
  1822. /* get a J2K compressor handle */
  1823. if (parameters.cod_format == J2K_CFMT) { /* J2K format output */
  1824. cinfo = opj_create_compress(CODEC_J2K);
  1825. } else { /* JP2 format output */
  1826. cinfo = opj_create_compress(CODEC_JP2);
  1827. }
  1828. /* catch events using our callbacks and give a local context */
  1829. opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, &msgErrorCallback_vars);
  1830. /* setup the encoder parameters using the current image and user parameters */
  1831. opj_setup_encoder(cinfo, &parameters, image);
  1832. /* open a byte stream for writing */
  1833. /* allocate memory for all tiles */
  1834. cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
  1835. /* encode the image */
  1836. bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
  1837. printf("C: after opj_encode_with_info\n");
  1838. if (!bSuccess) {
  1839. opj_cio_close(cio);
  1840. fprintf(stderr, "failed to encode image\n");
  1841. return -1;
  1842. }
  1843. codestream_length = cio_tell(cio);
  1844. /* write the index on disk, if needed (-x 'filename') */
  1845. if (*indexfilename) {
  1846. bSuccess = write_index_file(&cstr_info, indexfilename);
  1847. if (bSuccess) {
  1848. fprintf(stderr, "Failed to output index file into [%s]\n", indexfilename);
  1849. }
  1850. }
  1851. compressed_index = create_index_into_byte_array(&cstr_info,
  1852. &compressed_index_size);
  1853. /* Allocates the Java compressedIndex byte[] and sends this index into the Java object */
  1854. fid = (*env)->GetFieldID(env, cls, "compressedIndex", "[B");
  1855. jba = (*env)->NewByteArray(env, compressed_index_size + 1);
  1856. jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, 0);
  1857. memcpy(jbBody, compressed_index, compressed_index_size);
  1858. (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
  1859. (*env)->SetObjectField(env, obj, fid, jba);
  1860. opj_free(compressed_index);
  1861. /* write the generated codestream to disk ? */
  1862. if (parameters.outfile[0] != '\0') {
  1863. f = fopen(parameters.outfile, "wb");
  1864. if (!f) {
  1865. fprintf(stderr, "failed to open [%s] for writing\n", parameters.outfile);
  1866. return -1;
  1867. }
  1868. fwrite(cio->buffer, 1, codestream_length, f);
  1869. fclose(f);
  1870. fprintf(stdout, "Generated outfile [%s]\n", parameters.outfile);
  1871. }
  1872. /* Write the generated codestream to the Java pre-allocated compressedStream byte[] */
  1873. fid = (*env)->GetFieldID(env, cls, "compressedStream", "[B");
  1874. jba = (*env)->GetObjectField(env, obj, fid);
  1875. jbBody = (*env)->GetPrimitiveArrayCritical(env, jba, 0);
  1876. memcpy(jbBody, cio->buffer, codestream_length);
  1877. (*env)->ReleasePrimitiveArrayCritical(env, jba, jbBody, 0);
  1878. /* close and free the byte stream */
  1879. opj_cio_close(cio);
  1880. /* free remaining compression structures */
  1881. opj_destroy_compress(cinfo);
  1882. opj_destroy_cstr_info(&cstr_info);
  1883. /* free image data */
  1884. opj_image_destroy(image);
  1885. }
  1886. /* free user parameters structure */
  1887. if (parameters.cp_comment) {
  1888. opj_free(parameters.cp_comment);
  1889. }
  1890. if (parameters.cp_matrice) {
  1891. opj_free(parameters.cp_matrice);
  1892. }
  1893. return codestream_length;
  1894. }