Throw exceptions in openjpeg's error handler during decode as well as encode.
authorCarl Hetherington <cth@carlh.net>
Wed, 24 Jan 2018 00:41:33 +0000 (00:41 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 24 Jan 2018 00:41:33 +0000 (00:41 +0000)
src/j2k.cc

index b3192025e9377cab5de02038ddbeb1a8a73ff46e..f1e4c3f5ebbddf5710ee14968e54e2032f7e8ede 100644 (file)
@@ -90,6 +90,12 @@ read_free_function (void* data)
        delete reinterpret_cast<ReadBuffer*>(data);
 }
 
+static void
+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 +108,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,
@@ -132,6 +140,8 @@ dcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
                throw MiscError ("could not create JPEG2000 stream");
        }
 
+       opj_set_error_handler(decoder, 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);
@@ -251,12 +261,6 @@ 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.
  */