Use enum class for the things in types.h
[libdcp.git] / src / j2k.cc
index b3192025e9377cab5de02038ddbeb1a8a73ff46e..654146ca848845e3cc735fa517ad247189688385 100644 (file)
     files in the program, then also delete it here.
 */
 
+
+#include "array_data.h"
 #include "j2k.h"
 #include "exceptions.h"
 #include "openjpeg_image.h"
-#include "data.h"
 #include "dcp_assert.h"
 #include "compose.hpp"
 #include <openjpeg.h>
 
 using std::min;
 using std::pow;
-using boost::shared_ptr;
+using std::string;
+using std::shared_ptr;
 using boost::shared_array;
 using namespace dcp;
 
 shared_ptr<dcp::OpenJPEGImage>
-dcp::decompress_j2k (Data data, int reduce)
+dcp::decompress_j2k (ArrayData data, int reduce)
 {
-       return dcp::decompress_j2k (data.data().get(), data.size(), reduce);
+       return dcp::decompress_j2k (data.data(), data.size(), reduce);
 }
 
 #ifdef LIBDCP_OPENJPEG2
@@ -90,6 +92,20 @@ read_free_function (void* data)
        delete reinterpret_cast<ReadBuffer*>(data);
 }
 
+
+static void
+decompress_error_callback (char const * msg, void *)
+{
+       throw J2KDecompressionError (msg);
+}
+
+
+static void
+compress_error_callback (char const * msg, void *)
+{
+       throw MiscError (msg);
+}
+
 /** Decompress a JPEG2000 image to a bitmap.
  *  @param data JPEG2000 data.
  *  @param size Size of data in bytes.
@@ -102,6 +118,8 @@ read_free_function (void* data)
 shared_ptr<dcp::OpenJPEGImage>
 dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
 {
+       DCP_ASSERT (reduce >= 0);
+
        uint8_t const jp2_magic[] = {
                0x00,
                0x00,
@@ -120,7 +138,7 @@ dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
 
        opj_codec_t* decoder = opj_create_decompress (format);
        if (!decoder) {
-               boost::throw_exception (DCPReadError ("could not create JPEG2000 decompresser"));
+               boost::throw_exception (ReadError ("could not create JPEG2000 decompresser"));
        }
        opj_dparameters_t parameters;
        opj_set_default_decoder_parameters (&parameters);
@@ -132,6 +150,8 @@ dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
                throw MiscError ("could not create JPEG2000 stream");
        }
 
+       opj_set_error_handler(decoder, decompress_error_callback, 00);
+
        opj_stream_set_read_function (stream, read_function);
        ReadBuffer* buffer = new ReadBuffer (data, size);
        opj_stream_set_user_data (stream, buffer, read_free_function);
@@ -143,9 +163,9 @@ dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
                opj_destroy_codec (decoder);
                opj_stream_destroy (stream);
                if (format == OPJ_CODEC_J2K) {
-                       boost::throw_exception (DCPReadError (String::compose ("could not decode JPEG2000 codestream of %1 bytes.", size)));
+                       boost::throw_exception (ReadError (String::compose ("could not decode JPEG2000 codestream of %1 bytes.", size)));
                } else {
-                       boost::throw_exception (DCPReadError (String::compose ("could not decode JP2 file of %1 bytes.", size)));
+                       boost::throw_exception (ReadError (String::compose ("could not decode JP2 file of %1 bytes.", size)));
                }
        }
 
@@ -181,7 +201,7 @@ dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
        if (!image) {
                opj_destroy_decompress (decoder);
                opj_cio_close (cio);
-               boost::throw_exception (DCPReadError (String::compose ("could not decode JPEG2000 codestream of %1 bytes.", size)));
+               boost::throw_exception (ReadError (String::compose ("could not decode JPEG2000 codestream of %1 bytes.", size)));
        }
 
        opj_destroy_decompress (decoder);
@@ -209,7 +229,7 @@ public:
        OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes)
        {
                DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE);
-               memcpy (_data.data().get() + _offset, buffer, nb_bytes);
+               memcpy (_data.data() + _offset, buffer, nb_bytes);
                _offset += nb_bytes;
                if (_offset > OPJ_SIZE_T (_data.size())) {
                        _data.set_size (_offset);
@@ -223,13 +243,13 @@ public:
                return OPJ_TRUE;
        }
 
-       Data data () const
+       ArrayData data () const
        {
                return _data;
        }
 
 private:
-       Data _data;
+       ArrayData _data;
        OPJ_SIZE_T _offset;
 };
 
@@ -251,17 +271,11 @@ seek_function (OPJ_OFF_T nb_bytes, void* data)
        return reinterpret_cast<WriteBuffer*>(data)->seek (nb_bytes);
 }
 
-static void
-error_callback (char const * msg, void *)
-{
-       throw MiscError (msg);
-}
-
 /** @xyz Picture to compress.  Parts of xyz's data WILL BE OVERWRITTEN by libopenjpeg so xyz cannot be re-used
  *  after this call; see opj_j2k_encode where if l_reuse_data is false it will set l_tilec->data = l_img_comp->data.
  */
-Data
-dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frames_per_second, bool threed, bool fourk)
+ArrayData
+dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frames_per_second, bool threed, bool fourk, string comment)
 {
        /* get a J2K compressor handle */
        opj_codec_t* encoder = opj_create_compress (OPJ_CODEC_J2K);
@@ -269,7 +283,12 @@ dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frame
                throw MiscError ("could not create JPEG2000 encoder");
        }
 
-       opj_set_error_handler (encoder, error_callback, 0);
+       if (comment.empty()) {
+               /* asdcplib complains with "Illegal data size" when reading frames encoded with an empty comment */
+               throw MiscError("compress_j2k comment can not be an empty string");
+       }
+
+       opj_set_error_handler (encoder, compress_error_callback, 0);
 
        /* Set encoding parameters to default values */
        opj_cparameters_t parameters;
@@ -278,7 +297,7 @@ dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frame
                parameters.numresolution = 7;
        }
        parameters.rsiz = fourk ? OPJ_PROFILE_CINEMA_4K : OPJ_PROFILE_CINEMA_2K;
-       parameters.cp_comment = strdup ("libdcp");
+       parameters.cp_comment = strdup (comment.c_str());
 
        /* set max image */
        parameters.max_cs_size = (bandwidth / 8) / frames_per_second;
@@ -295,6 +314,8 @@ dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frame
 
        opj_stream_t* stream = opj_stream_default_create (OPJ_FALSE);
        if (!stream) {
+               opj_destroy_codec (encoder);
+               free (parameters.cp_comment);
                throw MiscError ("could not create JPEG2000 stream");
        }
 
@@ -304,38 +325,43 @@ dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frame
        opj_stream_set_user_data (stream, buffer, write_free_function);
 
        if (!opj_start_compress (encoder, xyz->opj_image(), stream)) {
+               opj_stream_destroy (stream);
+               opj_destroy_codec (encoder);
+               free (parameters.cp_comment);
                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));
+                       boost::throw_exception (StartCompressionError (errno & 0xff));
                } else {
-                       throw MiscError ("could not start JPEG2000 encoding");
+                       boost::throw_exception (StartCompressionError ());
                }
        }
 
        if (!opj_encode (encoder, stream)) {
-               opj_destroy_codec (encoder);
                opj_stream_destroy (stream);
+               opj_destroy_codec (encoder);
+               free (parameters.cp_comment);
                throw MiscError ("JPEG2000 encoding failed");
        }
 
        if (!opj_end_compress (encoder, stream)) {
-               opj_destroy_codec (encoder);
                opj_stream_destroy (stream);
+               opj_destroy_codec (encoder);
+               free (parameters.cp_comment);
                throw MiscError ("could not end JPEG2000 encoding");
        }
 
-       Data enc (buffer->data ());
+       ArrayData enc (buffer->data ());
 
-       free (parameters.cp_comment);
-       opj_destroy_codec (encoder);
        opj_stream_destroy (stream);
+       opj_destroy_codec (encoder);
+       free (parameters.cp_comment);
 
        return enc;
 }
 #endif
 
 #ifdef LIBDCP_OPENJPEG1
-Data
+ArrayData
 dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frames_per_second, bool threed, bool fourk)
 {
        /* Set the max image and component sizes based on frame_rate */
@@ -442,7 +468,7 @@ dcp::compress_j2k (shared_ptr<const OpenJPEGImage> xyz, int bandwidth, int frame
                throw MiscError ("JPEG2000 encoding failed");
        }
 
-       Data enc (cio->buffer, cio_tell (cio));
+       ArrayData enc (cio->buffer, cio_tell (cio));
 
        opj_cio_close (cio);
        free (parameters.cp_comment);