- shared_ptr<Image> image (new Image (PIX_FMT_RGB48LE, _size, true));
-
- shared_ptr<dcp::XYZImage> xyz = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0);
-
- if (xyz->opj_image()->comps[0].prec < 12) {
- int const shift = 12 - xyz->opj_image()->comps[0].prec;
- for (int c = 0; c < 3; ++c) {
- int* p = xyz->data (c);
- for (int y = 0; y < xyz->size().height; ++y) {
- for (int x = 0; x < xyz->size().width; ++x) {
- *p++ <<= shift;
- }
+ boost::mutex::scoped_lock lm (_mutex);
+
+ if (_image && target_size == _target_size) {
+ DCPOMATIC_ASSERT (_reduce);
+ return *_reduce;
+ }
+
+ int reduce = 0;
+
+ if (_forced_reduction) {
+ reduce = *_forced_reduction;
+ } else {
+ while (target_size && (_size.width / pow(2, reduce)) > target_size->width && (_size.height / pow(2, reduce)) > target_size->height) {
+ ++reduce;
+ }
+
+ --reduce;
+ reduce = max (0, reduce);
+ }
+
+ try {
+ /* XXX: should check that potentially trashing _data here doesn't matter */
+ auto decompressed = dcp::decompress_j2k (const_cast<uint8_t*>(_data->data()), _data->size(), reduce);
+ _image = make_shared<Image>(_pixel_format, decompressed->size(), alignment);
+
+ int const shift = 16 - decompressed->precision (0);
+
+ /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
+ the data is 12-bit either way.
+ */
+
+ int const width = decompressed->size().width;
+
+ int p = 0;
+ int* decomp_0 = decompressed->data (0);
+ int* decomp_1 = decompressed->data (1);
+ int* decomp_2 = decompressed->data (2);
+ for (int y = 0; y < decompressed->size().height; ++y) {
+ auto q = reinterpret_cast<uint16_t *>(_image->data()[0] + y * _image->stride()[0]);
+ for (int x = 0; x < width; ++x) {
+ *q++ = decomp_0[p] << shift;
+ *q++ = decomp_1[p] << shift;
+ *q++ = decomp_2[p] << shift;
+ ++p;