X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fj2k_image_proxy.cc;h=35073201c121a6e6a693e43bb64b183bb5f8d8b2;hb=d2728b07a42b0da60effd259b508761e670c0789;hp=28b7299c8911d50a61c4ee830a197138fd88ade7;hpb=ca34d35b476868f98d5b7dde00f07831a4302e6b;p=dcpomatic.git diff --git a/src/lib/j2k_image_proxy.cc b/src/lib/j2k_image_proxy.cc index 28b7299c8..35073201c 100644 --- a/src/lib/j2k_image_proxy.cc +++ b/src/lib/j2k_image_proxy.cc @@ -22,12 +22,15 @@ #include "image.h" #include "data.h" #include "raw_convert.h" -#include +#include #include #include #include #include #include +#include +#include +#include #include "i18n.h" @@ -35,6 +38,7 @@ using std::string; using std::cout; using boost::shared_ptr; using boost::optional; +using boost::dynamic_pointer_cast; /** Construct a J2KImageProxy from a JPEG2000 file */ J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size) @@ -77,27 +81,48 @@ J2KImageProxy::J2KImageProxy (shared_ptr xml, shared_ptr soc socket->read (_data.data().get (), _data.size ()); } -shared_ptr -J2KImageProxy::image (optional note) const +void +J2KImageProxy::ensure_j2k () const { - shared_ptr image (new Image (PIX_FMT_RGB48LE, _size, true)); + if (!_j2k) { + _j2k = dcp::decompress_j2k (const_cast (_data.data().get()), _data.size (), 0); + } +} - shared_ptr xyz = dcp::decompress_j2k (const_cast (_data.data().get()), _data.size (), 0); +shared_ptr +J2KImageProxy::image (optional) const +{ + ensure_j2k (); - if (xyz->opj_image()->comps[0].prec < 12) { - int const shift = 12 - xyz->opj_image()->comps[0].prec; + if (_j2k->opj_image()->comps[0].prec < 12) { + int const shift = 12 - _j2k->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) { + int* p = _j2k->data (c); + for (int y = 0; y < _j2k->size().height; ++y) { + for (int x = 0; x < _j2k->size().width; ++x) { *p++ <<= shift; } } } } - dcp::xyz_to_rgb (xyz, dcp::ColourConversion::srgb_to_xyz(), image->data()[0], image->stride()[0], note); - + shared_ptr image (new Image (pixel_format(), _size, true)); + + /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming + the data is 12-bit either way. + */ + + int p = 0; + for (int y = 0; y < _j2k->size().height; ++y) { + uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]); + for (int x = 0; x < _j2k->size().width; ++x) { + for (int c = 0; c < 3; ++c) { + *q++ = _j2k->data(c)[p] << 4; + } + ++p; + } + } + return image; } @@ -118,3 +143,37 @@ J2KImageProxy::send_binary (shared_ptr socket) const { socket->write (_data.data().get(), _data.size()); } + +bool +J2KImageProxy::same (shared_ptr other) const +{ + shared_ptr jp = dynamic_pointer_cast (other); + if (!jp) { + return false; + } + + if (_data.size() != jp->_data.size()) { + return false; + } + + return memcmp (_data.data().get(), jp->_data.data().get(), _data.size()) == 0; +} + +AVPixelFormat +J2KImageProxy::pixel_format () const +{ + ensure_j2k (); + + if (_j2k->opj_image()->color_space == CLRSPC_SRGB) { + return AV_PIX_FMT_RGB48LE; + } + + return AV_PIX_FMT_XYZ12LE; +} + +J2KImageProxy::J2KImageProxy (Data data, dcp::Size size) + : _data (data) + , _size (size) +{ + +}