Shift 8-bit XYZ images up so that they are at least visible (part of #497).
[dcpomatic.git] / src / lib / j2k_image_proxy.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "j2k_image_proxy.h"
21 #include "dcpomatic_socket.h"
22 #include "image.h"
23 #include "data.h"
24 #include "raw_convert.h"
25 #include <dcp/xyz_image.h>
26 #include <dcp/mono_picture_frame.h>
27 #include <dcp/stereo_picture_frame.h>
28 #include <dcp/colour_conversion.h>
29 #include <dcp/rgb_xyz.h>
30 #include <libcxml/cxml.h>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::cout;
36 using boost::shared_ptr;
37 using boost::optional;
38
39 /** Construct a J2KImageProxy from a JPEG2000 file */
40 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size)
41         : _data (path)
42         , _size (size)
43 {
44
45 }
46
47 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size)
48         : _data (frame->j2k_size ())
49         , _size (size)
50 {
51         memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
52 }
53
54 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye)
55         : _size (size)
56         , _eye (eye)
57 {
58         switch (eye) {
59         case dcp::EYE_LEFT:
60                 _data = Data (frame->left_j2k_size ());
61                 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
62                 break;
63         case dcp::EYE_RIGHT:
64                 _data = Data (frame->right_j2k_size ());
65                 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
66                 break;
67         }
68 }
69
70 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
71 {
72         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
73         if (xml->optional_number_child<int> ("Eye")) {
74                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
75         }
76         _data = Data (xml->number_child<int> ("Size"));
77         socket->read (_data.data().get (), _data.size ());
78 }
79
80 shared_ptr<Image>
81 J2KImageProxy::image (optional<dcp::NoteHandler> note) const
82 {
83         shared_ptr<Image> image (new Image (PIX_FMT_RGB48LE, _size, true));
84
85         shared_ptr<dcp::XYZImage> xyz = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0);
86
87         if (xyz->opj_image()->comps[0].prec < 12) {
88                 int const shift = 12 - xyz->opj_image()->comps[0].prec;
89                 for (int c = 0; c < 3; ++c) {
90                         int* p = xyz->data (c);
91                         for (int y = 0; y < xyz->size().height; ++y) {
92                                 for (int x = 0; x < xyz->size().width; ++x) {
93                                         *p++ <<= shift;
94                                 }
95                         }
96                 }
97         }
98
99         dcp::xyz_to_rgb (xyz, dcp::ColourConversion::srgb_to_xyz(), image->data()[0], image->stride()[0], note);
100         
101         return image;
102 }
103
104 void
105 J2KImageProxy::add_metadata (xmlpp::Node* node) const
106 {
107         node->add_child("Type")->add_child_text (N_("J2K"));
108         node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
109         node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
110         if (_eye) {
111                 node->add_child("Eye")->add_child_text (raw_convert<string> (_eye.get ()));
112         }
113         node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
114 }
115
116 void
117 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
118 {
119         socket->write (_data.data().get(), _data.size());
120 }