openjpeg.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2005, Herve 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_config.h"
  30. #include "opj_includes.h"
  31. /* ---------------------------------------------------------------------- */
  32. #ifdef _WIN32
  33. #ifndef OPJ_STATIC
  34. BOOL APIENTRY
  35. DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
  36. OPJ_ARG_NOT_USED(lpReserved);
  37. OPJ_ARG_NOT_USED(hModule);
  38. switch (ul_reason_for_call) {
  39. case DLL_PROCESS_ATTACH :
  40. break;
  41. case DLL_PROCESS_DETACH :
  42. break;
  43. case DLL_THREAD_ATTACH :
  44. case DLL_THREAD_DETACH :
  45. break;
  46. }
  47. return TRUE;
  48. }
  49. #endif /* OPJ_STATIC */
  50. #endif /* _WIN32 */
  51. /* ---------------------------------------------------------------------- */
  52. const char* OPJ_CALLCONV opj_version(void) {
  53. return PACKAGE_VERSION;
  54. }
  55. opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
  56. opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
  57. if(!dinfo) return NULL;
  58. dinfo->is_decompressor = OPJ_TRUE;
  59. switch(format) {
  60. case CODEC_J2K:
  61. case CODEC_JPT:
  62. /* get a J2K decoder handle */
  63. dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
  64. if(!dinfo->j2k_handle) {
  65. opj_free(dinfo);
  66. return NULL;
  67. }
  68. break;
  69. case CODEC_JP2:
  70. /* get a JP2 decoder handle */
  71. dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
  72. if(!dinfo->jp2_handle) {
  73. opj_free(dinfo);
  74. return NULL;
  75. }
  76. break;
  77. case CODEC_UNKNOWN:
  78. default:
  79. opj_free(dinfo);
  80. return NULL;
  81. }
  82. dinfo->codec_format = format;
  83. return dinfo;
  84. }
  85. void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
  86. if(dinfo) {
  87. /* destroy the codec */
  88. switch(dinfo->codec_format) {
  89. case CODEC_J2K:
  90. case CODEC_JPT:
  91. j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
  92. break;
  93. case CODEC_JP2:
  94. jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
  95. break;
  96. case CODEC_UNKNOWN:
  97. default:
  98. break;
  99. }
  100. /* destroy the decompressor */
  101. opj_free(dinfo);
  102. }
  103. }
  104. void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
  105. if(parameters) {
  106. memset(parameters, 0, sizeof(opj_dparameters_t));
  107. /* default decoding parameters */
  108. parameters->cp_layer = 0;
  109. parameters->cp_reduce = 0;
  110. parameters->cp_limit_decoding = NO_LIMITATION;
  111. parameters->decod_format = -1;
  112. parameters->cod_format = -1;
  113. parameters->flags = 0;
  114. /* UniPG>> */
  115. #ifdef USE_JPWL
  116. parameters->jpwl_correct = OPJ_FALSE;
  117. parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
  118. parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
  119. #endif /* USE_JPWL */
  120. /* <<UniPG */
  121. }
  122. }
  123. void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
  124. if(dinfo && parameters) {
  125. switch(dinfo->codec_format) {
  126. case CODEC_J2K:
  127. case CODEC_JPT:
  128. j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
  129. break;
  130. case CODEC_JP2:
  131. jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
  132. break;
  133. case CODEC_UNKNOWN:
  134. default:
  135. break;
  136. }
  137. }
  138. }
  139. opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
  140. return opj_decode_with_info(dinfo, cio, NULL);
  141. }
  142. opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
  143. if(dinfo && cio) {
  144. switch(dinfo->codec_format) {
  145. case CODEC_J2K:
  146. return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
  147. case CODEC_JPT:
  148. return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
  149. case CODEC_JP2:
  150. return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
  151. case CODEC_UNKNOWN:
  152. default:
  153. break;
  154. }
  155. }
  156. return NULL;
  157. }
  158. opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
  159. opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
  160. if(!cinfo) return NULL;
  161. cinfo->is_decompressor = OPJ_FALSE;
  162. switch(format) {
  163. case CODEC_J2K:
  164. /* get a J2K coder handle */
  165. cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
  166. if(!cinfo->j2k_handle) {
  167. opj_free(cinfo);
  168. return NULL;
  169. }
  170. break;
  171. case CODEC_JP2:
  172. /* get a JP2 coder handle */
  173. cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
  174. if(!cinfo->jp2_handle) {
  175. opj_free(cinfo);
  176. return NULL;
  177. }
  178. break;
  179. case CODEC_JPT:
  180. case CODEC_UNKNOWN:
  181. default:
  182. opj_free(cinfo);
  183. return NULL;
  184. }
  185. cinfo->codec_format = format;
  186. return cinfo;
  187. }
  188. void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
  189. if(cinfo) {
  190. /* destroy the codec */
  191. switch(cinfo->codec_format) {
  192. case CODEC_J2K:
  193. j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
  194. break;
  195. case CODEC_JP2:
  196. jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
  197. break;
  198. case CODEC_JPT:
  199. case CODEC_UNKNOWN:
  200. default:
  201. break;
  202. }
  203. /* destroy the decompressor */
  204. opj_free(cinfo);
  205. }
  206. }
  207. void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
  208. if(parameters) {
  209. memset(parameters, 0, sizeof(opj_cparameters_t));
  210. /* default coding parameters */
  211. parameters->cp_cinema = OFF;
  212. parameters->max_comp_size = 0;
  213. parameters->numresolution = 6;
  214. parameters->cp_rsiz = STD_RSIZ;
  215. parameters->cblockw_init = 64;
  216. parameters->cblockh_init = 64;
  217. parameters->prog_order = LRCP;
  218. parameters->roi_compno = -1; /* no ROI */
  219. parameters->subsampling_dx = 1;
  220. parameters->subsampling_dy = 1;
  221. parameters->tp_on = 0;
  222. parameters->decod_format = -1;
  223. parameters->cod_format = -1;
  224. parameters->tcp_rates[0] = 0;
  225. parameters->tcp_numlayers = 0;
  226. parameters->cp_disto_alloc = 0;
  227. parameters->cp_fixed_alloc = 0;
  228. parameters->cp_fixed_quality = 0;
  229. parameters->jpip_on = OPJ_FALSE;
  230. /* UniPG>> */
  231. #ifdef USE_JPWL
  232. parameters->jpwl_epc_on = OPJ_FALSE;
  233. parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
  234. {
  235. int i;
  236. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  237. parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
  238. parameters->jpwl_hprot_TPH[i] = 0; /* absent */
  239. }
  240. };
  241. {
  242. int i;
  243. for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
  244. parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
  245. parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
  246. parameters->jpwl_pprot[i] = 0; /* absent */
  247. }
  248. };
  249. parameters->jpwl_sens_size = 0; /* 0 means no ESD */
  250. parameters->jpwl_sens_addr = 0; /* 0 means auto */
  251. parameters->jpwl_sens_range = 0; /* 0 means packet */
  252. parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
  253. {
  254. int i;
  255. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  256. parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
  257. parameters->jpwl_sens_TPH[i] = -1; /* absent */
  258. }
  259. };
  260. #endif /* USE_JPWL */
  261. /* <<UniPG */
  262. }
  263. }
  264. void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
  265. if(cinfo && parameters && image) {
  266. switch(cinfo->codec_format) {
  267. case CODEC_J2K:
  268. j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
  269. break;
  270. case CODEC_JP2:
  271. jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
  272. break;
  273. case CODEC_JPT:
  274. case CODEC_UNKNOWN:
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
  281. if (index != NULL)
  282. opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
  283. "To extract the index, use the opj_encode_with_info() function.\n"
  284. "No index will be generated during this encoding\n");
  285. return opj_encode_with_info(cinfo, cio, image, NULL);
  286. }
  287. opj_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) {
  288. if(cinfo && cio && image) {
  289. switch(cinfo->codec_format) {
  290. case CODEC_J2K:
  291. return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
  292. case CODEC_JP2:
  293. return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
  294. case CODEC_JPT:
  295. case CODEC_UNKNOWN:
  296. default:
  297. break;
  298. }
  299. }
  300. return OPJ_FALSE;
  301. }
  302. void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
  303. if (cstr_info) {
  304. int tileno;
  305. for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
  306. opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
  307. opj_free(tile_info->thresh);
  308. opj_free(tile_info->packet);
  309. opj_free(tile_info->tp);
  310. opj_free(tile_info->marker);
  311. }
  312. opj_free(cstr_info->tile);
  313. opj_free(cstr_info->marker);
  314. opj_free(cstr_info->numdecompos);
  315. }
  316. }