openjpeg.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2005, Hervé Drolon, FreeImage Team
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef WIN32
  27. #include <windows.h>
  28. #endif /* WIN32 */
  29. #include "opj_includes.h"
  30. /* ---------------------------------------------------------------------- */
  31. #ifdef WIN32
  32. #ifndef OPJ_STATIC
  33. BOOL APIENTRY
  34. DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
  35. switch (ul_reason_for_call) {
  36. case DLL_PROCESS_ATTACH :
  37. break;
  38. case DLL_PROCESS_DETACH :
  39. break;
  40. case DLL_THREAD_ATTACH :
  41. case DLL_THREAD_DETACH :
  42. break;
  43. }
  44. return TRUE;
  45. }
  46. #endif /* OPJ_STATIC */
  47. #endif /* WIN32 */
  48. /* ---------------------------------------------------------------------- */
  49. const char* OPJ_CALLCONV opj_version(void) {
  50. return OPENJPEG_VERSION;
  51. }
  52. opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
  53. opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
  54. if(!dinfo) return NULL;
  55. dinfo->is_decompressor = true;
  56. switch(format) {
  57. case CODEC_J2K:
  58. case CODEC_JPT:
  59. /* get a J2K decoder handle */
  60. dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
  61. if(!dinfo->j2k_handle) {
  62. opj_free(dinfo);
  63. return NULL;
  64. }
  65. break;
  66. case CODEC_JP2:
  67. /* get a JP2 decoder handle */
  68. dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
  69. if(!dinfo->jp2_handle) {
  70. opj_free(dinfo);
  71. return NULL;
  72. }
  73. break;
  74. case CODEC_UNKNOWN:
  75. default:
  76. opj_free(dinfo);
  77. return NULL;
  78. }
  79. dinfo->codec_format = format;
  80. return dinfo;
  81. }
  82. void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
  83. if(dinfo) {
  84. /* destroy the codec */
  85. switch(dinfo->codec_format) {
  86. case CODEC_J2K:
  87. case CODEC_JPT:
  88. j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
  89. break;
  90. case CODEC_JP2:
  91. jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
  92. break;
  93. case CODEC_UNKNOWN:
  94. default:
  95. break;
  96. }
  97. /* destroy the decompressor */
  98. opj_free(dinfo);
  99. }
  100. }
  101. void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
  102. if(parameters) {
  103. memset(parameters, 0, sizeof(opj_dparameters_t));
  104. /* default decoding parameters */
  105. parameters->cp_layer = 0;
  106. parameters->cp_reduce = 0;
  107. parameters->cp_limit_decoding = NO_LIMITATION;
  108. parameters->decod_format = -1;
  109. parameters->cod_format = -1;
  110. /* UniPG>> */
  111. #ifdef USE_JPWL
  112. parameters->jpwl_correct = false;
  113. parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
  114. parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
  115. #endif /* USE_JPWL */
  116. /* <<UniPG */
  117. }
  118. }
  119. void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
  120. if(dinfo && parameters) {
  121. switch(dinfo->codec_format) {
  122. case CODEC_J2K:
  123. case CODEC_JPT:
  124. j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
  125. break;
  126. case CODEC_JP2:
  127. jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
  128. break;
  129. case CODEC_UNKNOWN:
  130. default:
  131. break;
  132. }
  133. }
  134. }
  135. opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
  136. return opj_decode_with_info(dinfo, cio, NULL);
  137. }
  138. opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
  139. if(dinfo && cio) {
  140. switch(dinfo->codec_format) {
  141. case CODEC_J2K:
  142. return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
  143. case CODEC_JPT:
  144. return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
  145. case CODEC_JP2:
  146. return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
  147. case CODEC_UNKNOWN:
  148. default:
  149. break;
  150. }
  151. }
  152. return NULL;
  153. }
  154. opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
  155. opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
  156. if(!cinfo) return NULL;
  157. cinfo->is_decompressor = false;
  158. switch(format) {
  159. case CODEC_J2K:
  160. /* get a J2K coder handle */
  161. cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
  162. if(!cinfo->j2k_handle) {
  163. opj_free(cinfo);
  164. return NULL;
  165. }
  166. break;
  167. case CODEC_JP2:
  168. /* get a JP2 coder handle */
  169. cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
  170. if(!cinfo->jp2_handle) {
  171. opj_free(cinfo);
  172. return NULL;
  173. }
  174. break;
  175. case CODEC_JPT:
  176. case CODEC_UNKNOWN:
  177. default:
  178. opj_free(cinfo);
  179. return NULL;
  180. }
  181. cinfo->codec_format = format;
  182. return cinfo;
  183. }
  184. void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
  185. if(cinfo) {
  186. /* destroy the codec */
  187. switch(cinfo->codec_format) {
  188. case CODEC_J2K:
  189. j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
  190. break;
  191. case CODEC_JP2:
  192. jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
  193. break;
  194. case CODEC_JPT:
  195. case CODEC_UNKNOWN:
  196. default:
  197. break;
  198. }
  199. /* destroy the decompressor */
  200. opj_free(cinfo);
  201. }
  202. }
  203. void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
  204. if(parameters) {
  205. memset(parameters, 0, sizeof(opj_cparameters_t));
  206. /* default coding parameters */
  207. parameters->cp_cinema = OFF;
  208. parameters->max_comp_size = 0;
  209. parameters->numresolution = 6;
  210. parameters->cp_rsiz = STD_RSIZ;
  211. parameters->cblockw_init = 64;
  212. parameters->cblockh_init = 64;
  213. parameters->prog_order = LRCP;
  214. parameters->roi_compno = -1; /* no ROI */
  215. parameters->subsampling_dx = 1;
  216. parameters->subsampling_dy = 1;
  217. parameters->tp_on = 0;
  218. parameters->decod_format = -1;
  219. parameters->cod_format = -1;
  220. parameters->tcp_rates[0] = 0;
  221. parameters->tcp_numlayers = 0;
  222. parameters->cp_disto_alloc = 0;
  223. parameters->cp_fixed_alloc = 0;
  224. parameters->cp_fixed_quality = 0;
  225. /* UniPG>> */
  226. #ifdef USE_JPWL
  227. parameters->jpwl_epc_on = false;
  228. parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
  229. {
  230. int i;
  231. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  232. parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
  233. parameters->jpwl_hprot_TPH[i] = 0; /* absent */
  234. }
  235. };
  236. {
  237. int i;
  238. for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
  239. parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
  240. parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
  241. parameters->jpwl_pprot[i] = 0; /* absent */
  242. }
  243. };
  244. parameters->jpwl_sens_size = 0; /* 0 means no ESD */
  245. parameters->jpwl_sens_addr = 0; /* 0 means auto */
  246. parameters->jpwl_sens_range = 0; /* 0 means packet */
  247. parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
  248. {
  249. int i;
  250. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  251. parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
  252. parameters->jpwl_sens_TPH[i] = -1; /* absent */
  253. }
  254. };
  255. #endif /* USE_JPWL */
  256. /* <<UniPG */
  257. }
  258. }
  259. void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
  260. if(cinfo && parameters && image) {
  261. switch(cinfo->codec_format) {
  262. case CODEC_J2K:
  263. j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
  264. break;
  265. case CODEC_JP2:
  266. jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
  267. break;
  268. case CODEC_JPT:
  269. case CODEC_UNKNOWN:
  270. default:
  271. break;
  272. }
  273. }
  274. }
  275. bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
  276. if (index != NULL)
  277. opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
  278. "To extract the index, use the opj_encode_with_info() function.\n"
  279. "No index will be generated during this encoding\n");
  280. return opj_encode_with_info(cinfo, cio, image, NULL);
  281. }
  282. bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  283. if(cinfo && cio && image) {
  284. switch(cinfo->codec_format) {
  285. case CODEC_J2K:
  286. return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
  287. case CODEC_JP2:
  288. return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
  289. case CODEC_JPT:
  290. case CODEC_UNKNOWN:
  291. default:
  292. break;
  293. }
  294. }
  295. return false;
  296. }
  297. void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
  298. if (cstr_info) {
  299. int tileno;
  300. for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  301. opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
  302. opj_free(tile_info->thresh);
  303. opj_free(tile_info->packet);
  304. opj_free(tile_info->tp);
  305. }
  306. opj_free(cstr_info->tile);
  307. opj_free(cstr_info->marker);
  308. opj_free(cstr_info->numdecompos);
  309. }
  310. }