0001-debcherry-fixup-patch.patch 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. From 74ca29440e46136504ed6fc811ccd60da8af8abc Mon Sep 17 00:00:00 2001
  2. From: David Bremner <david@tethera.net>
  3. Date: Wed, 26 Oct 2016 22:18:48 -0300
  4. Subject: [PATCH 1/3] debcherry fixup patch
  5. 88800f7 J2K: use g_strlcpy() instead of g_snprintf()
  6. - no changes against upstream or conflicts
  7. c6e0b7c FindOpenJPEG.cmake: use HINTS, not PATHS. NO_DEFAULT_PATH is not needed.
  8. - no changes against upstream or conflicts
  9. 5cf698f Port to OpenJPEG2
  10. - extra changes or conflicts
  11. 6f48d69 FindOpenJPEG.cmake: pick right include path if multiple headers.
  12. - extra changes or conflicts
  13. acf974a Update cmake stuff to look for OpenJPEG2 (openjpeg >= 2.0.0 aka openjp2)
  14. - extra changes or conflicts
  15. ecef362 restore upstream TODO
  16. - no changes against upstream or conflicts
  17. 898199a debian: cleanup useless / missing docs
  18. - extra changes or conflicts
  19. 31da456 remove dcraw.c again
  20. - no changes against upstream or conflicts
  21. 4c3f916 remove dcraw.c
  22. - no changes against upstream or conflicts
  23. 3b395b6 "undelete" some files now shipped in upstream tarball
  24. - extra changes or conflicts
  25. 7d30472 remove version.h from git; generate it at build time
  26. - no changes against upstream or conflicts
  27. 7441226 remove version.sh, unneeded for build
  28. - no changes against upstream or conflicts
  29. b0be4a0 commit version.h, version.sh for new version
  30. - extra changes or conflicts
  31. b5784e1 commit version.sh for current upstream snapshot
  32. - extra changes or conflicts
  33. cea1f68 remove generated files that are no longer carried in upstream branch (since we are not going to import them from tarballs.
  34. - extra changes or conflicts
  35. f67ef8e Revert "As a workaround, add files to POTFILES.skip"
  36. - extra changes or conflicts
  37. f6a4d09 As a workaround, add files to POTFILES.skip
  38. - extra changes or conflicts
  39. 1eb15b3 Imported Upstream version 0.5
  40. - extra changes or conflicts
  41. ---
  42. cmake/modules/FindOpenJPEG.cmake | 112 ++++---------------
  43. src/CMakeLists.txt | 17 +--
  44. src/common/imageio_j2k.c | 228 ++++++++++++++++++++++----------------
  45. src/imageio/format/CMakeLists.txt | 4 +-
  46. src/imageio/format/j2k.c | 130 +++++++++++-----------
  47. 5 files changed, 224 insertions(+), 267 deletions(-)
  48. diff --git a/cmake/modules/FindOpenJPEG.cmake b/cmake/modules/FindOpenJPEG.cmake
  49. index b80f922..da6d275 100644
  50. --- a/cmake/modules/FindOpenJPEG.cmake
  51. +++ b/cmake/modules/FindOpenJPEG.cmake
  52. @@ -1,101 +1,29 @@
  53. -# - Find OpenJPEG library
  54. -# Find the native OpenJPEG includes and library
  55. -# This module defines
  56. -# OPENJPEG_INCLUDE_DIRS, where to find openjpeg.h, Set when
  57. -# OPENJPEG_INCLUDE_DIR is found.
  58. -# OPENJPEG_LIBRARIES, libraries to link against to use OpenJPEG.
  59. -# OPENJPEG_ROOT_DIR, The base directory to search for OpenJPEG.
  60. -# This can also be an environment variable.
  61. -# OPENJPEG_FOUND, If false, do not try to use OpenJPEG.
  62. +# - Try to find OpenJPEG
  63. +# Once done, this will define
  64. #
  65. -# also defined, but not for general use are
  66. -# OPENJPEG_LIBRARY, where to find the OpenJPEG library.
  67. -
  68. -#=============================================================================
  69. -# Copyright 2011 Blender Foundation.
  70. -#
  71. -# Distributed under the OSI-approved BSD License (the "License");
  72. -# see accompanying file Copyright.txt for details.
  73. -#
  74. -# This software is distributed WITHOUT ANY WARRANTY; without even the
  75. -# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  76. -# See the License for more information.
  77. -#=============================================================================
  78. -
  79. -# If OPENJPEG_ROOT_DIR was defined in the environment, use it.
  80. -IF(NOT OPENJPEG_ROOT_DIR AND NOT $ENV{OPENJPEG_ROOT_DIR} STREQUAL "")
  81. - SET(OPENJPEG_ROOT_DIR $ENV{OPENJPEG_ROOT_DIR})
  82. -ENDIF()
  83. +# OpenJPEG_FOUND - system has OpenJPEG
  84. +# OpenJPEG_INCLUDE_DIRS - the OpenJPEG include directories
  85. +# OpenJPEG_LIBRARIES - link these to use OpenJPEG
  86. include(LibFindMacros)
  87. -libfind_pkg_check_modules(PC_OPENJPEG libopenjpeg1)
  88. +# Use pkg-config to get hints about paths
  89. +libfind_pkg_check_modules(OpenJPEG_PKGCONF libopenjp2)
  90. -SET(_openjpeg_SEARCH_DIRS
  91. - ${OPENJPEG_ROOT_DIR}
  92. - /usr/local
  93. - /sw # Fink
  94. - /opt/local # DarwinPorts
  95. - /opt/csw # Blastwave
  96. +# Include dir
  97. +find_path(OpenJPEG_INCLUDE_DIR
  98. + NAMES openjpeg.h
  99. + HINTS ${OpenJPEG_PKGCONF_INCLUDE_DIRS}
  100. )
  101. -FIND_PATH(OPENJPEG_INCLUDE_DIR
  102. - NAMES
  103. - openjpeg.h
  104. - HINTS
  105. - ${PC_OPENJPEG_INCLUDEDIR}
  106. - ${PC_OPENJPEG_INCLUDE_DIRS}
  107. - ${_openjpeg_SEARCH_DIRS}
  108. - PATH_SUFFIXES
  109. - include
  110. +# Finally the library itself
  111. +find_library(OpenJPEG_LIBRARY
  112. + NAMES openjp2
  113. + HINTS ${OpenJPEG_PKGCONF_LIBRARY_DIRS}
  114. )
  115. -FIND_LIBRARY(OPENJPEG_LIBRARY
  116. - NAMES
  117. - openjpeg
  118. - HINTS
  119. - ${PC_OPENJPEG_LIBDIR}
  120. - ${PC_OPENJPEG_LIBRARY_DIRS}
  121. - ${_openjpeg_SEARCH_DIRS}
  122. - PATH_SUFFIXES
  123. - lib64 lib
  124. - )
  125. -
  126. -# handle the QUIETLY and REQUIRED arguments and set OPENJPEG_FOUND to TRUE if
  127. -# all listed variables are TRUE
  128. -INCLUDE(FindPackageHandleStandardArgs)
  129. -FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenJPEG DEFAULT_MSG
  130. - OPENJPEG_LIBRARY OPENJPEG_INCLUDE_DIR)
  131. -
  132. -IF(OPENJPEG_FOUND)
  133. - SET(OPENJPEG_LIBRARIES ${OPENJPEG_LIBRARY})
  134. - SET(OPENJPEG_INCLUDE_DIRS ${OPENJPEG_INCLUDE_DIR})
  135. -
  136. - # 1.3 didn't have pkg-config support, so we can't use that to get the version.
  137. - # 1.5 no longer has the #define OPENJPEG_VERSION ... in openjpeg.h so we can't use that either.
  138. - # Thus we might have to compile a smalltest program to get the version string. Could someone please kill me?
  139. -
  140. - # see if pkg-config found the library. this should succeed for cross compiles as they are harder to run test programs
  141. - if(NOT "${PC_OPENJPEG_VERSION}" STREQUAL "")
  142. - # use the version as found by pkg-config
  143. - set(OPENJPEG_VERSION ${PC_OPENJPEG_VERSION})
  144. - else(NOT "${PC_OPENJPEG_VERSION}" STREQUAL "")
  145. - # too bad, we have to run our test code
  146. - try_run(run_var result_var
  147. - ${CMAKE_BINARY_DIR}/CMakeTmp
  148. - ${CMAKE_SOURCE_DIR}/cmake/modules/openjpeg_version.c
  149. - CMAKE_FLAGS "-DLINK_LIBRARIES:STRING=${OPENJPEG_LIBRARIES}"
  150. - "-DINCLUDE_DIRECTORIES:STRING=${OPENJPEG_INCLUDE_DIRS}"
  151. - RUN_OUTPUT_VARIABLE rout_var
  152. - )
  153. - if(result_var)
  154. - set(OPENJPEG_VERSION ${rout_var})
  155. - endif(result_var)
  156. - endif(NOT "${PC_OPENJPEG_VERSION}" STREQUAL "")
  157. -
  158. -ENDIF(OPENJPEG_FOUND)
  159. -
  160. -MARK_AS_ADVANCED(
  161. - OPENJPEG_INCLUDE_DIR
  162. - OPENJPEG_LIBRARY
  163. -)
  164. +# Set the include dir variables and the libraries and let libfind_process do the rest.
  165. +# NOTE: Singular variables for this library, plural for libraries this this lib depends on.
  166. +set(OpenJPEG_PROCESS_INCLUDES OpenJPEG_INCLUDE_DIR)
  167. +set(OpenJPEG_PROCESS_LIBS OpenJPEG_LIBRARY)
  168. +libfind_process(OpenJPEG)
  169. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
  170. index 9278d91..7d29528 100644
  171. --- a/src/CMakeLists.txt
  172. +++ b/src/CMakeLists.txt
  173. @@ -225,18 +225,13 @@ endif(USE_FLICKR)
  174. if(USE_OPENJPEG)
  175. find_package(OpenJPEG)
  176. - if(OPENJPEG_FOUND)
  177. - list(APPEND SOURCES "common/imageio_j2k.c")
  178. - include_directories(SYSTEM ${OPENJPEG_INCLUDE_DIRS})
  179. - list(APPEND LIBS ${OPENJPEG_LIBRARIES})
  180. - add_definitions(${OPENJPEG_DEFINITIONS})
  181. + if(OpenJPEG_FOUND)
  182. add_definitions("-DHAVE_OPENJPEG")
  183. - if(OPENJPEG_VERSION VERSION_LESS "1.5.0")
  184. - message(STATUS "OpenJPEG version ${OPENJPEG_VERSION} found. Only 1.5 and newer support reading of icc profiles.")
  185. - else()
  186. - add_definitions("-DHAVE_OPENJPEG_ICC")
  187. - endif()
  188. - endif(OPENJPEG_FOUND)
  189. + list(APPEND SOURCES "common/imageio_j2k.c")
  190. +
  191. + include_directories(SYSTEM ${OpenJPEG_INCLUDE_DIRS})
  192. + list(APPEND LIBS ${OpenJPEG_LIBRARIES})
  193. + endif(OpenJPEG_FOUND)
  194. endif(USE_OPENJPEG)
  195. #
  196. diff --git a/src/common/imageio_j2k.c b/src/common/imageio_j2k.c
  197. index 84bcd28..7f60e7f 100644
  198. --- a/src/common/imageio_j2k.c
  199. +++ b/src/common/imageio_j2k.c
  200. @@ -36,6 +36,7 @@
  201. #define JPT_CFMT 2
  202. static char JP2_HEAD[] = { 0x0, 0x0, 0x0, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };
  203. +static char JP2_MAGIC[] = { 0x0d, 0x0a, 0x87, 0x0a };
  204. static char J2K_HEAD[] = { 0xFF, 0x4F, 0xFF, 0x51, 0x00 };
  205. // there seems to be no JPIP/JPT magic string, so we can't load it ...
  206. @@ -74,7 +75,7 @@ static int get_file_format(const char *filename)
  207. char *ext = strrchr(filename, '.');
  208. if(ext == NULL) return -1;
  209. ext++;
  210. - if(ext)
  211. + if(*ext)
  212. {
  213. for(i = 0; i < sizeof(format) / sizeof(*format); i++)
  214. {
  215. @@ -91,100 +92,116 @@ static int get_file_format(const char *filename)
  216. dt_imageio_retval_t dt_imageio_open_j2k(dt_image_t *img, const char *filename, dt_mipmap_buffer_t *mbuf)
  217. {
  218. opj_dparameters_t parameters; /* decompression parameters */
  219. - opj_event_mgr_t event_mgr; /* event manager */
  220. opj_image_t *image = NULL;
  221. FILE *fsrc = NULL;
  222. - unsigned char *src = NULL;
  223. - size_t file_length;
  224. - opj_dinfo_t *dinfo = NULL; /* handle to a decompressor */
  225. - opj_cio_t *cio = NULL;
  226. + unsigned char src_header[12] = { 0 };
  227. + opj_codec_t *d_codec = NULL;
  228. OPJ_CODEC_FORMAT codec;
  229. + opj_stream_t *d_stream = NULL; /* Stream */
  230. int ret = DT_IMAGEIO_FILE_CORRUPTED;
  231. - int file_format = get_file_format(filename);
  232. - if(file_format == -1) return DT_IMAGEIO_FILE_CORRUPTED;
  233. + /* set decoding parameters to default values */
  234. + opj_set_default_decoder_parameters(&parameters);
  235. +
  236. + g_strlcpy(parameters.infile, filename, sizeof(parameters.infile));
  237. +
  238. + parameters.decod_format = get_file_format(filename);
  239. + if(parameters.decod_format == -1) return DT_IMAGEIO_FILE_CORRUPTED;
  240. if(!img->exif_inited) (void)dt_exif_read(img, filename);
  241. - /* read the input file and put it in memory */
  242. - /* ---------------------------------------- */
  243. fsrc = fopen(filename, "rb");
  244. if(!fsrc)
  245. {
  246. fprintf(stderr, "[j2k_open] Error: failed to open `%s' for reading\n", filename);
  247. return DT_IMAGEIO_FILE_NOT_FOUND;
  248. }
  249. - fseek(fsrc, 0, SEEK_END);
  250. - file_length = ftell(fsrc);
  251. - fseek(fsrc, 0, SEEK_SET);
  252. - src = (unsigned char *)malloc(file_length);
  253. - if(fread(src, 1, file_length, fsrc) != file_length)
  254. + if(fread(src_header, 1, 12, fsrc) != 12)
  255. {
  256. - free(src);
  257. fclose(fsrc);
  258. fprintf(stderr, "[j2k_open] Error: fread returned a number of elements different from the expected.\n");
  259. return DT_IMAGEIO_FILE_NOT_FOUND;
  260. }
  261. fclose(fsrc);
  262. - if(memcmp(JP2_HEAD, src, sizeof(JP2_HEAD)) == 0)
  263. + if(memcmp(JP2_HEAD, src_header, sizeof(JP2_HEAD)) == 0 || memcmp(JP2_MAGIC, src_header, sizeof(JP2_MAGIC)) == 0)
  264. {
  265. - file_format = JP2_CFMT; // just in case someone used the wrong extension
  266. + parameters.decod_format = JP2_CFMT; // just in case someone used the wrong extension
  267. }
  268. - else if(memcmp(J2K_HEAD, src, sizeof(J2K_HEAD)) == 0)
  269. + else if(memcmp(J2K_HEAD, src_header, sizeof(J2K_HEAD)) == 0)
  270. {
  271. - file_format = J2K_CFMT; // just in case someone used the wrong extension
  272. + parameters.decod_format = J2K_CFMT; // just in case someone used the wrong extension
  273. }
  274. else // this will also reject jpt files.
  275. {
  276. - free(src);
  277. fprintf(stderr, "[j2k_open] Error: `%s' has unsupported file format.\n", filename);
  278. return DT_IMAGEIO_FILE_CORRUPTED;
  279. }
  280. - /* configure the event callbacks (not required) */
  281. - memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
  282. - event_mgr.error_handler = error_callback;
  283. - // event_mgr.warning_handler = warning_callback;
  284. - // event_mgr.info_handler = info_callback;
  285. -
  286. - /* set decoding parameters to default values */
  287. - opj_set_default_decoder_parameters(&parameters);
  288. /* decode the code-stream */
  289. /* ---------------------- */
  290. - if(file_format == J2K_CFMT) /* JPEG-2000 codestream */
  291. - codec = CODEC_J2K;
  292. - else if(file_format == JP2_CFMT) /* JPEG 2000 compressed image data */
  293. - codec = CODEC_JP2;
  294. - else if(file_format == JPT_CFMT) /* JPEG 2000, JPIP */
  295. - codec = CODEC_JPT;
  296. + if(parameters.decod_format == J2K_CFMT) /* JPEG-2000 codestream */
  297. + codec = OPJ_CODEC_J2K;
  298. + else if(parameters.decod_format == JP2_CFMT) /* JPEG 2000 compressed image data */
  299. + codec = OPJ_CODEC_JP2;
  300. + else if(parameters.decod_format == JPT_CFMT) /* JPEG 2000, JPIP */
  301. + codec = OPJ_CODEC_JPT;
  302. else
  303. {
  304. - free(src);
  305. return DT_IMAGEIO_FILE_CORRUPTED; // can't happen
  306. }
  307. - /* get a decoder handle */
  308. - dinfo = opj_create_decompress(codec);
  309. + d_codec = opj_create_decompress(codec);
  310. + if(!d_codec)
  311. + {
  312. + fprintf(stderr, "[j2k_open] Error: failed to create the decoder\n");
  313. + return DT_IMAGEIO_FILE_CORRUPTED;
  314. + }
  315. /* catch events using our callbacks and give a local context */
  316. - opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
  317. + opj_set_error_handler(d_codec, error_callback, stderr);
  318. + // opj_set_warning_handler(d_codec, error_callback, stderr);
  319. + // opj_set_info_handler(d_codec, error_callback, stderr);
  320. /* setup the decoder decoding parameters using user parameters */
  321. - opj_setup_decoder(dinfo, &parameters);
  322. + if(!opj_setup_decoder(d_codec, &parameters))
  323. + {
  324. + fprintf(stderr, "[j2k_open] Error: failed to setup the decoder %s\n", parameters.infile);
  325. + opj_destroy_codec(d_codec);
  326. + return DT_IMAGEIO_FILE_CORRUPTED;
  327. + }
  328. - /* open a byte stream */
  329. - cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
  330. + d_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
  331. + if(!d_stream)
  332. + {
  333. + fprintf(stderr, "[j2k_open] Error: failed to create the stream from the file %s\n", parameters.infile);
  334. + opj_destroy_codec(d_codec);
  335. + return DT_IMAGEIO_FILE_CORRUPTED;
  336. + }
  337. - /* decode the stream and fill the image structure */
  338. - image = opj_decode(dinfo, cio);
  339. + /* Read the main header of the codestream and if necessary the JP2 boxes*/
  340. + if(!opj_read_header(d_stream, d_codec, &image))
  341. + {
  342. + fprintf(stderr, "[j2k_open] Error: failed to read the header\n");
  343. + opj_stream_destroy(d_stream);
  344. + opj_destroy_codec(d_codec);
  345. + opj_image_destroy(image);
  346. + return EXIT_FAILURE;
  347. + }
  348. - /* close the byte stream */
  349. - opj_cio_close(cio);
  350. + /* Get the decoded image */
  351. + if(!(opj_decode(d_codec, d_stream, image) && opj_end_decompress(d_codec, d_stream)))
  352. + {
  353. + fprintf(stderr, "[j2k_open] Error: failed to decode image!\n");
  354. + opj_destroy_codec(d_codec);
  355. + opj_stream_destroy(d_stream);
  356. + opj_image_destroy(image);
  357. + return DT_IMAGEIO_FILE_CORRUPTED;
  358. + }
  359. - /* free the memory containing the code-stream */
  360. - free(src);
  361. + /* Close the byte stream */
  362. + opj_stream_destroy(d_stream);
  363. if(!image)
  364. {
  365. @@ -193,14 +210,11 @@ dt_imageio_retval_t dt_imageio_open_j2k(dt_image_t *img, const char *filename, d
  366. goto end_of_the_world;
  367. }
  368. - if(image->color_space == CLRSPC_SYCC)
  369. + if(image->color_space == OPJ_CLRSPC_SYCC)
  370. {
  371. color_sycc_to_rgb(image);
  372. }
  373. -// FIXME: openjpeg didn't have support for icc profiles before version 1.5
  374. -// this needs some #ifdef magic and proper implementation
  375. -#ifdef HAVE_OPENJPEG_ICC
  376. if(image->icc_profile_buf)
  377. {
  378. #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
  379. @@ -211,7 +225,6 @@ dt_imageio_retval_t dt_imageio_open_j2k(dt_image_t *img, const char *filename, d
  380. image->icc_profile_buf = NULL;
  381. image->icc_profile_len = 0;
  382. }
  383. -#endif
  384. /* create output image */
  385. /* ------------------- */
  386. @@ -289,7 +302,7 @@ dt_imageio_retval_t dt_imageio_open_j2k(dt_image_t *img, const char *filename, d
  387. end_of_the_world:
  388. /* free remaining structures */
  389. - if(dinfo) opj_destroy_decompress(dinfo);
  390. + opj_destroy_codec(d_codec);
  391. /* free image data structure */
  392. opj_image_destroy(image);
  393. @@ -299,83 +312,113 @@ end_of_the_world:
  394. int dt_imageio_j2k_read_profile(const char *filename, uint8_t **out)
  395. {
  396. -#ifdef HAVE_OPENJPEG_ICC
  397. opj_dparameters_t parameters; /* decompression parameters */
  398. opj_image_t *image = NULL;
  399. FILE *fsrc = NULL;
  400. - unsigned char *src = NULL;
  401. - size_t file_length;
  402. - opj_dinfo_t *dinfo = NULL; /* handle to a decompressor */
  403. - opj_cio_t *cio = NULL;
  404. + unsigned char src_header[12] = { 0 };
  405. + opj_codec_t *d_codec = NULL;
  406. OPJ_CODEC_FORMAT codec;
  407. + opj_stream_t *d_stream = NULL; /* Stream */
  408. gboolean res = FALSE;
  409. unsigned int length = 0;
  410. *out = NULL;
  411. + /* set decoding parameters to default values */
  412. + opj_set_default_decoder_parameters(&parameters);
  413. +
  414. + g_strlcpy(parameters.infile, filename, sizeof(parameters.infile));
  415. +
  416. + parameters.decod_format = get_file_format(filename);
  417. + if(parameters.decod_format == -1) return DT_IMAGEIO_FILE_CORRUPTED;
  418. +
  419. /* read the input file and put it in memory */
  420. /* ---------------------------------------- */
  421. fsrc = fopen(filename, "rb");
  422. if(!fsrc)
  423. {
  424. - fprintf(stderr, "[j2k_open] Error: failed to open `%s' for reading\n", filename);
  425. + fprintf(stderr, "[j2k_read_profile] Error: failed to open `%s' for reading\n", filename);
  426. goto another_end_of_the_world;
  427. }
  428. - fseek(fsrc, 0, SEEK_END);
  429. - file_length = ftell(fsrc);
  430. - fseek(fsrc, 0, SEEK_SET);
  431. - src = (unsigned char *)malloc(file_length);
  432. - if(fread(src, 1, file_length, fsrc) != file_length)
  433. + if(fread(src_header, 1, 12, fsrc) != 12)
  434. {
  435. - free(src);
  436. fclose(fsrc);
  437. - fprintf(stderr, "[j2k_open] Error: fread returned a number of elements different from the expected.\n");
  438. + fprintf(stderr,
  439. + "[j2k_read_profile] Error: fread returned a number of elements different from the expected.\n");
  440. goto another_end_of_the_world;
  441. }
  442. fclose(fsrc);
  443. - if(memcmp(JP2_HEAD, src, sizeof(JP2_HEAD)) == 0)
  444. + if(memcmp(JP2_HEAD, src_header, sizeof(JP2_HEAD)) == 0 || memcmp(JP2_MAGIC, src_header, sizeof(JP2_MAGIC)) == 0)
  445. {
  446. - codec = CODEC_JP2;
  447. + codec = OPJ_CODEC_JP2;
  448. }
  449. - else if(memcmp(J2K_HEAD, src, sizeof(J2K_HEAD)) == 0)
  450. + else if(memcmp(J2K_HEAD, src_header, sizeof(J2K_HEAD)) == 0)
  451. {
  452. - codec = CODEC_J2K;
  453. + codec = OPJ_CODEC_J2K;
  454. }
  455. else // this will also reject jpt files.
  456. {
  457. - free(src);
  458. - fprintf(stderr, "[j2k_open] Error: `%s' has unsupported file format.\n", filename);
  459. + fprintf(stderr, "[j2k_read_profile] Error: `%s' has unsupported file format.\n", filename);
  460. goto another_end_of_the_world;
  461. }
  462. - /* set decoding parameters to default values */
  463. - opj_set_default_decoder_parameters(&parameters);
  464. - parameters.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  465. -
  466. /* decode the code-stream */
  467. /* ---------------------- */
  468. /* get a decoder handle */
  469. - dinfo = opj_create_decompress(codec);
  470. + d_codec = opj_create_decompress(codec);
  471. + if(!d_codec)
  472. + {
  473. + fprintf(stderr, "[j2k_read_profile] Error: failed to create the decoder\n");
  474. + return DT_IMAGEIO_FILE_CORRUPTED;
  475. + }
  476. /* setup the decoder decoding parameters using user parameters */
  477. - opj_setup_decoder(dinfo, &parameters);
  478. + opj_setup_decoder(d_codec, &parameters);
  479. + if(!d_codec)
  480. + {
  481. + fprintf(stderr, "[j2k_read_profile] Error: failed to setup the decoder %s\n", parameters.infile);
  482. + return DT_IMAGEIO_FILE_CORRUPTED;
  483. + }
  484. - /* open a byte stream */
  485. - cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
  486. + d_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
  487. + if(!d_stream)
  488. + {
  489. + fprintf(stderr, "[j2k_read_profile] Error: failed to create the stream from the file %s\n", parameters.infile);
  490. + return DT_IMAGEIO_FILE_CORRUPTED;
  491. + }
  492. - /* decode the stream and fill the image structure */
  493. - image = opj_decode(dinfo, cio);
  494. + /* Read the main header of the codestream and if necessary the JP2 boxes*/
  495. + if(!opj_read_header(d_stream, d_codec, &image))
  496. + {
  497. + fprintf(stderr, "[j2k_read_profile] Error: failed to read the header\n");
  498. + opj_stream_destroy(d_stream);
  499. + opj_destroy_codec(d_codec);
  500. + opj_image_destroy(image);
  501. + return EXIT_FAILURE;
  502. + }
  503. - /* close the byte stream */
  504. - opj_cio_close(cio);
  505. + /* Get the decoded image */
  506. + if(!(opj_decode(d_codec, d_stream, image) && opj_end_decompress(d_codec, d_stream)))
  507. + {
  508. + fprintf(stderr, "[j2k_read_profile] Error: failed to decode image!\n");
  509. + opj_destroy_codec(d_codec);
  510. + opj_stream_destroy(d_stream);
  511. + opj_image_destroy(image);
  512. + return DT_IMAGEIO_FILE_CORRUPTED;
  513. + }
  514. - /* free the memory containing the code-stream */
  515. - free(src);
  516. + // FIXME: how to do it without fully-decoding the whole image?
  517. + // opj_jp2_decode() copies the icc_profile_{buf,len}
  518. + // from opj_codec_t *d_codec d_codec->color into opj_image_t *image, but
  519. + // opj_codec_t is private type.
  520. +
  521. + /* Close the byte stream */
  522. + opj_stream_destroy(d_stream);
  523. if(!image)
  524. {
  525. - fprintf(stderr, "[j2k_open] Error: failed to decode image `%s'\n", filename);
  526. + fprintf(stderr, "[j2k_read_profile] Error: failed to decode image `%s'\n", filename);
  527. goto another_end_of_the_world;
  528. }
  529. @@ -391,15 +434,12 @@ int dt_imageio_j2k_read_profile(const char *filename, uint8_t **out)
  530. another_end_of_the_world:
  531. /* free remaining structures */
  532. - if(dinfo) opj_destroy_decompress(dinfo);
  533. + opj_destroy_codec(d_codec);
  534. /* free image data structure */
  535. opj_image_destroy(image);
  536. return res ? length : 0;
  537. -#else
  538. - return 0;
  539. -#endif
  540. }
  541. @@ -632,7 +672,7 @@ static void color_sycc_to_rgb(opj_image_t *img)
  542. {
  543. if(img->numcomps < 3)
  544. {
  545. - img->color_space = CLRSPC_GRAY;
  546. + img->color_space = OPJ_CLRSPC_GRAY;
  547. return;
  548. }
  549. @@ -658,7 +698,7 @@ static void color_sycc_to_rgb(opj_image_t *img)
  550. fprintf(stderr, "%s:%d:color_sycc_to_rgb\n\tCAN NOT CONVERT\n", __FILE__, __LINE__);
  551. return;
  552. }
  553. - img->color_space = CLRSPC_SRGB;
  554. + img->color_space = OPJ_CLRSPC_SRGB;
  555. } /* color_sycc_to_rgb() */
  556. // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
  557. diff --git a/src/imageio/format/CMakeLists.txt b/src/imageio/format/CMakeLists.txt
  558. index e328cb8..a79f3ee 100644
  559. --- a/src/imageio/format/CMakeLists.txt
  560. +++ b/src/imageio/format/CMakeLists.txt
  561. @@ -22,10 +22,10 @@ if(OPENEXR_FOUND)
  562. add_library(exr MODULE "exr.cc")
  563. endif(OPENEXR_FOUND)
  564. -if(OPENJPEG_FOUND)
  565. +if(OpenJPEG_FOUND)
  566. list(APPEND MODULES "j2k")
  567. add_library(j2k MODULE "j2k.c")
  568. -endif(OPENJPEG_FOUND)
  569. +endif(OpenJPEG_FOUND)
  570. foreach(module ${MODULES})
  571. target_link_libraries(${module} ${LIBS})
  572. diff --git a/src/imageio/format/j2k.c b/src/imageio/format/j2k.c
  573. index 0344b75..44a995c 100644
  574. --- a/src/imageio/format/j2k.c
  575. +++ b/src/imageio/format/j2k.c
  576. @@ -63,11 +63,6 @@
  577. #include <openjpeg.h>
  578. -#define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/
  579. -#define CINEMA_48_CS 651041 /*Codestream length for 48fps*/
  580. -#define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/
  581. -#define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/
  582. -
  583. typedef enum
  584. {
  585. J2K_CFMT = 0,
  586. @@ -168,14 +163,14 @@ static int initialise_4K_poc(opj_poc_t *POC, int numres)
  587. POC[0].layno1 = 1;
  588. POC[0].resno1 = numres - 1;
  589. POC[0].compno1 = 3;
  590. - POC[0].prg1 = CPRL;
  591. + POC[0].prg1 = OPJ_CPRL;
  592. POC[1].tile = 1;
  593. POC[1].resno0 = numres - 1;
  594. POC[1].compno0 = 0;
  595. POC[1].layno1 = 1;
  596. POC[1].resno1 = numres;
  597. POC[1].compno1 = 3;
  598. - POC[1].prg1 = CPRL;
  599. + POC[1].prg1 = OPJ_CPRL;
  600. return 2;
  601. }
  602. @@ -201,7 +196,7 @@ static void cinema_parameters(opj_cparameters_t *parameters)
  603. parameters->csty |= 0x01;
  604. /*The progression order shall be CPRL*/
  605. - parameters->prog_order = CPRL;
  606. + parameters->prog_order = OPJ_CPRL;
  607. /* No ROI */
  608. parameters->roi_compno = -1;
  609. @@ -220,9 +215,9 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *ima
  610. switch(parameters->cp_cinema)
  611. {
  612. - case CINEMA2K_24:
  613. - case CINEMA2K_48:
  614. - parameters->cp_rsiz = CINEMA2K;
  615. + case OPJ_CINEMA2K_24:
  616. + case OPJ_CINEMA2K_48:
  617. + parameters->cp_rsiz = OPJ_CINEMA2K;
  618. if(parameters->numresolution > 6)
  619. {
  620. parameters->numresolution = 6;
  621. @@ -233,12 +228,12 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *ima
  622. "Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
  623. "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
  624. image->comps[0].w, image->comps[0].h);
  625. - parameters->cp_rsiz = STD_RSIZ;
  626. + parameters->cp_rsiz = OPJ_STD_RSIZ;
  627. }
  628. break;
  629. - case CINEMA4K_24:
  630. - parameters->cp_rsiz = CINEMA4K;
  631. + case OPJ_CINEMA4K_24:
  632. + parameters->cp_rsiz = OPJ_CINEMA4K;
  633. if(parameters->numresolution < 1)
  634. {
  635. parameters->numresolution = 1;
  636. @@ -253,7 +248,7 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *ima
  637. "Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
  638. "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
  639. image->comps[0].w, image->comps[0].h);
  640. - parameters->cp_rsiz = STD_RSIZ;
  641. + parameters->cp_rsiz = OPJ_STD_RSIZ;
  642. }
  643. parameters->numpocs = initialise_4K_poc(parameters->POC, parameters->numresolution);
  644. break;
  645. @@ -263,26 +258,26 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *ima
  646. switch(parameters->cp_cinema)
  647. {
  648. - case CINEMA2K_24:
  649. - case CINEMA4K_24:
  650. + case OPJ_CINEMA2K_24:
  651. + case OPJ_CINEMA4K_24:
  652. for(i = 0; i < parameters->tcp_numlayers; i++)
  653. {
  654. if(rates[i] == 0)
  655. {
  656. parameters->tcp_rates[0]
  657. = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))
  658. - / (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  659. + / (OPJ_CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  660. }
  661. else
  662. {
  663. temp_rate
  664. = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))
  665. / (rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
  666. - if(temp_rate > CINEMA_24_CS)
  667. + if(temp_rate > OPJ_CINEMA_24_CS)
  668. {
  669. parameters->tcp_rates[i]
  670. = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))
  671. - / (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  672. + / (OPJ_CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  673. }
  674. else
  675. {
  676. @@ -290,28 +285,28 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *ima
  677. }
  678. }
  679. }
  680. - parameters->max_comp_size = COMP_24_CS;
  681. + parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  682. break;
  683. - case CINEMA2K_48:
  684. + case OPJ_CINEMA2K_48:
  685. for(i = 0; i < parameters->tcp_numlayers; i++)
  686. {
  687. if(rates[i] == 0)
  688. {
  689. parameters->tcp_rates[0]
  690. = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))
  691. - / (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  692. + / (OPJ_CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  693. }
  694. else
  695. {
  696. temp_rate
  697. = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))
  698. / (rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
  699. - if(temp_rate > CINEMA_48_CS)
  700. + if(temp_rate > OPJ_CINEMA_48_CS)
  701. {
  702. parameters->tcp_rates[0]
  703. = ((float)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))
  704. - / (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  705. + / (OPJ_CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
  706. }
  707. else
  708. {
  709. @@ -319,7 +314,7 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters, opj_image_t *ima
  710. }
  711. }
  712. }
  713. - parameters->max_comp_size = COMP_48_CS;
  714. + parameters->max_comp_size = OPJ_CINEMA_48_COMP;
  715. break;
  716. default:
  717. break;
  718. @@ -334,19 +329,9 @@ int write_image(dt_imageio_module_data_t *j2k_tmp, const char *filename, const v
  719. dt_imageio_j2k_t *j2k = (dt_imageio_j2k_t *)j2k_tmp;
  720. opj_cparameters_t parameters; /* compression parameters */
  721. float *rates = NULL;
  722. - opj_event_mgr_t event_mgr; /* event manager */
  723. opj_image_t *image = NULL;
  724. const int quality = CLAMP(j2k->quality, 1, 100);
  725. - /*
  726. - configure the event callbacks (not required)
  727. - setting of each callback is optional
  728. - */
  729. - memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
  730. - event_mgr.error_handler = error_callback;
  731. - event_mgr.warning_handler = warning_callback;
  732. - event_mgr.info_handler = info_callback;
  733. -
  734. /* set encoding parameters to default values */
  735. opj_set_default_encoder_parameters(&parameters);
  736. @@ -357,7 +342,7 @@ int write_image(dt_imageio_module_data_t *j2k_tmp, const char *filename, const v
  737. parameters.tcp_numlayers = 1; /* only one resolution */
  738. parameters.cp_disto_alloc = 1;
  739. - parameters.cp_rsiz = STD_RSIZ;
  740. + parameters.cp_rsiz = OPJ_STD_RSIZ;
  741. parameters.cod_format = j2k->format;
  742. parameters.cp_cinema = (OPJ_CINEMA_MODE)j2k->preset;
  743. @@ -397,7 +382,7 @@ int write_image(dt_imageio_module_data_t *j2k_tmp, const char *filename, const v
  744. cmptparm[i].w = w;
  745. cmptparm[i].h = h;
  746. }
  747. - image = opj_image_create(numcomps, &cmptparm[0], CLRSPC_SRGB);
  748. + image = opj_image_create(numcomps, &cmptparm[0], OPJ_CLRSPC_SRGB);
  749. if(!image)
  750. {
  751. fprintf(stderr, "Error: opj_image_create() failed\n");
  752. @@ -437,6 +422,7 @@ int write_image(dt_imageio_module_data_t *j2k_tmp, const char *filename, const v
  753. fprintf(stderr, "Error: this shouldn't happen, there is no bit depth of %d for jpeg 2000 images.\n",
  754. prec);
  755. free(rates);
  756. + opj_image_destroy(image);
  757. return 1;
  758. }
  759. }
  760. @@ -457,58 +443,66 @@ int write_image(dt_imageio_module_data_t *j2k_tmp, const char *filename, const v
  761. int rc = 1;
  762. OPJ_CODEC_FORMAT codec;
  763. if(parameters.cod_format == J2K_CFMT) /* J2K format output */
  764. - codec = CODEC_J2K;
  765. + codec = OPJ_CODEC_J2K;
  766. else
  767. - codec = CODEC_JP2;
  768. + codec = OPJ_CODEC_JP2;
  769. - int codestream_length;
  770. - size_t res;
  771. - opj_cio_t *cio = NULL;
  772. - FILE *f = NULL;
  773. + opj_stream_t *cstream = NULL;
  774. /* get a J2K/JP2 compressor handle */
  775. - opj_cinfo_t *cinfo = opj_create_compress(codec);
  776. + opj_codec_t *ccodec = opj_create_compress(codec);
  777. +
  778. + opj_set_error_handler(ccodec, error_callback, stderr);
  779. + opj_set_warning_handler(ccodec, warning_callback, stderr);
  780. + opj_set_info_handler(ccodec, info_callback, stderr);
  781. - /* catch events using our callbacks and give a local context */
  782. - opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
  783. + g_strlcpy(parameters.outfile, filename, sizeof(parameters.outfile));
  784. /* setup the encoder parameters using the current image and user parameters */
  785. - opj_setup_encoder(cinfo, &parameters, image);
  786. + opj_setup_encoder(ccodec, &parameters, image);
  787. /* open a byte stream for writing */
  788. /* allocate memory for all tiles */
  789. - cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
  790. -
  791. - /* encode the image */
  792. - if(!opj_encode(cinfo, cio, image, NULL))
  793. + cstream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
  794. + if(!cstream)
  795. {
  796. - opj_cio_close(cio);
  797. - fprintf(stderr, "failed to encode image\n");
  798. + opj_destroy_codec(ccodec);
  799. + opj_image_destroy(image);
  800. + fprintf(stderr, "failed to create output stream\n");
  801. return 1;
  802. }
  803. - codestream_length = cio_tell(cio);
  804. - /* write the buffer to disk */
  805. - f = fopen(filename, "wb");
  806. - if(!f)
  807. + if(!opj_start_compress(ccodec, image, cstream))
  808. {
  809. - fprintf(stderr, "failed to open %s for writing\n", filename);
  810. + opj_stream_destroy(cstream);
  811. + opj_destroy_codec(ccodec);
  812. + opj_image_destroy(image);
  813. + fprintf(stderr, "failed to encode image: opj_start_compress\n");
  814. return 1;
  815. }
  816. - res = fwrite(cio->buffer, 1, codestream_length, f);
  817. - if(res < (size_t)codestream_length) /* FIXME */
  818. +
  819. + /* encode the image */
  820. + if(!opj_encode(ccodec, cstream))
  821. {
  822. - fprintf(stderr, "failed to write %d (%s)\n", codestream_length, filename);
  823. - fclose(f);
  824. + opj_stream_destroy(cstream);
  825. + opj_destroy_codec(ccodec);
  826. + opj_image_destroy(image);
  827. + fprintf(stderr, "failed to encode image: opj_encode\n");
  828. return 1;
  829. }
  830. - fclose(f);
  831. - /* close and free the byte stream */
  832. - opj_cio_close(cio);
  833. + /* encode the image */
  834. + if(!opj_end_compress(ccodec, cstream))
  835. + {
  836. + opj_stream_destroy(cstream);
  837. + opj_destroy_codec(ccodec);
  838. + opj_image_destroy(image);
  839. + fprintf(stderr, "failed to encode image: opj_end_compress\n");
  840. + return 1;
  841. + }
  842. - /* free remaining compression structures */
  843. - opj_destroy_compress(cinfo);
  844. + opj_stream_destroy(cstream);
  845. + opj_destroy_codec(ccodec);
  846. /* add exif data blob. seems to not work for j2k files :( */
  847. if(exif && j2k->format == JP2_CFMT) rc = dt_exif_write_blob(exif, exif_len, filename, 1);