Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / j2k_image_proxy.cc
index a5d13b869c03f844166716614584d599e07d792e..303792b80f406e8e8417e401ed646f3410d10f81 100644 (file)
 #include "image.h"
 #include "data.h"
 #include "raw_convert.h"
+#include <dcp/openjpeg_image.h>
 #include <dcp/mono_picture_frame.h>
 #include <dcp/stereo_picture_frame.h>
 #include <dcp/colour_conversion.h>
 #include <dcp/rgb_xyz.h>
 #include <libcxml/cxml.h>
+#include <libxml++/libxml++.h>
+#include <iostream>
 
 #include "i18n.h"
 
 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)
@@ -80,13 +85,35 @@ J2KImageProxy::image (optional<dcp::NoteHandler> note) const
 {
        shared_ptr<Image> image (new Image (PIX_FMT_RGB48LE, _size, true));
 
-       dcp::xyz_to_rgb (
-               dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0),
-               dcp::ColourConversion::srgb_to_xyz(),
-               image->data()[0],
-               image->stride()[0],
-               note
-               );
+       shared_ptr<dcp::OpenJPEGImage> oj = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0);
+
+       if (oj->opj_image()->comps[0].prec < 12) {
+               int const shift = 12 - oj->opj_image()->comps[0].prec;
+               for (int c = 0; c < 3; ++c) {
+                       int* p = oj->data (c);
+                       for (int y = 0; y < oj->size().height; ++y) {
+                               for (int x = 0; x < oj->size().width; ++x) {
+                                       *p++ <<= shift;
+                               }
+                       }
+               }
+       }
+
+       if (oj->opj_image()->color_space == CLRSPC_SRGB) {
+               /* No XYZ -> RGB conversion necessary; just copy and interleave the values */
+               int p = 0;
+               for (int y = 0; y < oj->size().height; ++y) {
+                       uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]);
+                       for (int x = 0; x < oj->size().width; ++x) {
+                               for (int c = 0; c < 3; ++c) {
+                                       *q++ = oj->data(c)[p] << 4;
+                               }
+                               ++p;
+                       }
+               }
+       } else {
+               dcp::xyz_to_rgb (oj, dcp::ColourConversion::srgb_to_xyz(), image->data()[0], image->stride()[0], note);
+       }
 
        return image;
 }
@@ -108,3 +135,25 @@ J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
 {
        socket->write (_data.data().get(), _data.size());
 }
+
+bool
+J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
+{
+       shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (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;
+}
+
+J2KImageProxy::J2KImageProxy (Data data, dcp::Size size)
+       : _data (data)
+       , _size (size)
+{
+
+}