summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2014-11-30 21:17:50 +0000
committerCarl Hetherington <cth@carlh.net>2014-11-30 21:17:50 +0000
commit4e60579afd2cec1a12dd4ed245111999861ab212 (patch)
tree7b77d4cf51f60f69eb68995ccb0200091a3ec787
parentbbb3db16cc7e6f2262a89da4bec9fc356d6c3c12 (diff)
Use openjpeg2.
-rw-r--r--src/util.cc58
-rw-r--r--src/xyz_frame.cc2
-rw-r--r--wscript14
3 files changed, 54 insertions, 20 deletions
diff --git a/src/util.cc b/src/util.cc
index 4ac3685c..bb1f9834 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -190,6 +190,41 @@ libdcp::content_kind_from_string (string type)
assert (false);
}
+class ReadBuffer
+{
+public:
+ ReadBuffer (uint8_t* data, int64_t size)
+ : _data (data)
+ , _size (size)
+ , _offset (0)
+ {}
+
+ OPJ_SIZE_T read (void* buffer, OPJ_SIZE_T nb_bytes)
+ {
+ int64_t N = min (nb_bytes, _size - _offset);
+ memcpy (buffer, _data + _offset, N);
+ _offset += N;
+ return N;
+ }
+
+private:
+ uint8_t* _data;
+ OPJ_SIZE_T _size;
+ OPJ_SIZE_T _offset;
+};
+
+static OPJ_SIZE_T
+read_function (void* buffer, OPJ_SIZE_T nb_bytes, void* data)
+{
+ return reinterpret_cast<ReadBuffer*>(data)->read (buffer, nb_bytes);
+}
+
+static void
+free_function (void* data)
+{
+ delete reinterpret_cast<ReadBuffer*>(data);
+}
+
/** Decompress a JPEG2000 image to a bitmap.
* @param data JPEG2000 data.
* @param size Size of data in bytes.
@@ -202,21 +237,28 @@ libdcp::content_kind_from_string (string type)
shared_ptr<libdcp::XYZFrame>
libdcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
{
- opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
+ opj_codec_t* decoder = opj_create_decompress (OPJ_CODEC_J2K);
+
opj_dparameters_t parameters;
opj_set_default_decoder_parameters (&parameters);
parameters.cp_reduce = reduce;
opj_setup_decoder (decoder, &parameters);
- opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
- opj_image_t* image = opj_decode (decoder, cio);
- if (!image) {
- opj_destroy_decompress (decoder);
- opj_cio_close (cio);
+
+ opj_stream_t* stream = opj_stream_default_create (OPJ_TRUE);
+ opj_stream_set_read_function (stream, read_function);
+ ReadBuffer* buffer = new ReadBuffer (data, size);
+ opj_stream_set_user_data (stream, buffer, free_function);
+
+ opj_image_t* image = 0;
+ opj_read_header (stream, decoder, &image);
+ if (opj_decode (decoder, stream, image) == OPJ_FALSE) {
+ opj_destroy_codec (decoder);
+ opj_stream_destroy (stream);
boost::throw_exception (DCPReadError ("could not decode JPEG2000 codestream of " + lexical_cast<string> (size) + " bytes."));
}
- opj_destroy_decompress (decoder);
- opj_cio_close (cio);
+ opj_destroy_codec (decoder);
+ opj_stream_destroy (stream);
image->x1 = rint (float(image->x1) / pow (2, reduce));
image->y1 = rint (float(image->y1) / pow (2, reduce));
diff --git a/src/xyz_frame.cc b/src/xyz_frame.cc
index f5b0ee86..8ea3afba 100644
--- a/src/xyz_frame.cc
+++ b/src/xyz_frame.cc
@@ -47,7 +47,7 @@ XYZFrame::XYZFrame (Size size)
}
/* XXX: is this _SRGB right? */
- _opj_image = opj_image_create (3, &cmptparm[0], CLRSPC_SRGB);
+ _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
if (_opj_image == 0) {
throw std::runtime_error ("could not create libopenjpeg image");
}
diff --git a/wscript b/wscript
index 9ce74e06..9db20a65 100644
--- a/wscript
+++ b/wscript
@@ -42,20 +42,12 @@ def configure(conf):
conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
if conf.options.static:
- conf.check_cc(fragment="""
- #include <stdio.h>\n
- #include <openjpeg.h>\n
- int main () {\n
- void* p = (void *) opj_image_create;\n
- return 0;\n
- }
- """,
- msg='Checking for library openjpeg', stlib='openjpeg', uselib_store='OPENJPEG', mandatory=True)
-
+ conf.check_cfg(package='libopenjp2', args='--cflags', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
+ conf.env.STLIB_OPENJPEG = ['openjpeg']
conf.env.HAVE_CXML = 1
conf.env.STLIB_CXML = ['cxml']
else:
- conf.check_cfg(package='libopenjpeg', args='--cflags --libs', uselib_store='OPENJPEG', mandatory=True)
+ conf.check_cfg(package='libopenjp2', args='--cflags --libs', atleast_version='2.1.0', uselib_store='OPENJPEG', mandatory=True)
conf.check_cfg(package='libcxml', atleast_version='0.11.0', args='--cflags --libs', uselib_store='CXML', mandatory=True)
if conf.options.target_windows: