+ /* Setup the encoder parameters using the current image and user parameters */
+ opj_setup_encoder (encoder, ¶meters, xyz->opj_image());
+
+ opj_stream_t* stream = opj_stream_default_create (OPJ_FALSE);
+ if (!stream) {
+ throw MiscError ("could not create JPEG2000 stream");
+ }
+
+ opj_stream_set_write_function (stream, write_function);
+ opj_stream_set_seek_function (stream, seek_function);
+ WriteBuffer* buffer = new WriteBuffer ();
+ opj_stream_set_user_data (stream, buffer, write_free_function);
+
+ if (!opj_start_compress (encoder, xyz->opj_image(), stream)) {
+ if ((errno & 0x61500) == 0x61500) {
+ /* We've had one of the magic error codes from our patched openjpeg */
+ throw MiscError (String::compose ("could not start JPEG2000 encoding (%1)", errno & 0xff));
+ } else {
+ throw MiscError ("could not start JPEG2000 encoding");
+ }
+ }
+
+ if (!opj_encode (encoder, stream)) {
+ opj_destroy_codec (encoder);
+ opj_stream_destroy (stream);
+ throw MiscError ("JPEG2000 encoding failed");
+ }
+
+ if (!opj_end_compress (encoder, stream)) {
+ opj_destroy_codec (encoder);
+ opj_stream_destroy (stream);
+ throw MiscError ("could not end JPEG2000 encoding");
+ }
+
+ Data enc (buffer->data ());
+
+ free (parameters.cp_comment);
+ opj_destroy_codec (encoder);
+ opj_stream_destroy (stream);
+
+ return enc;
+}
+#endif
+
+#ifdef LIBDCP_OPENJPEG1
+Data
+dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frames_per_second, bool threed, bool fourk)
+{