Fix uninitialised variable.
[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 "encoded_data.h"
24 #include <dcp/raw_convert.h>
25 #include <dcp/mono_picture_frame.h>
26 #include <dcp/stereo_picture_frame.h>
27 #include <dcp/colour_conversion.h>
28 #include <dcp/rgb_xyz.h>
29 #include <libcxml/cxml.h>
30
31 #include "i18n.h"
32
33 using std::string;
34 using boost::shared_ptr;
35 using boost::optional;
36
37 /** Construct a J2KImageProxy from a JPEG2000 file */
38 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size)
39         : _mono (new dcp::MonoPictureFrame (path))
40         , _size (size)
41         , _eye (EYE_BOTH)
42 {
43
44 }
45
46 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size)
47         : _mono (frame)
48         , _size (size)
49         , _eye (EYE_BOTH)
50 {
51         
52 }
53
54 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye)
55         : _stereo (frame)
56         , _size (size)
57         , _eye (eye)
58 {
59
60 }
61
62 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
63 {
64         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
65         if (xml->optional_number_child<int> ("Eye")) {
66                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
67                 int const left_size = xml->number_child<int> ("LeftSize");
68                 int const right_size = xml->number_child<int> ("RightSize");
69                 shared_ptr<dcp::StereoPictureFrame> f (new dcp::StereoPictureFrame ());
70                 socket->read (f->left_j2k_data(), left_size);
71                 socket->read (f->right_j2k_data(), right_size);
72                 _stereo = f;
73         } else {
74                 int const size = xml->number_child<int> ("Size");
75                 shared_ptr<dcp::MonoPictureFrame> f (new dcp::MonoPictureFrame ());
76                 socket->read (f->j2k_data (), size);
77                 _mono = f;
78         }
79 }
80
81 shared_ptr<Image>
82 J2KImageProxy::image (optional<dcp::NoteHandler> note) const
83 {
84         shared_ptr<Image> image (new Image (PIX_FMT_RGB48LE, _size, true));
85
86         if (_mono) {
87                 dcp::xyz_to_rgb (_mono->xyz_image (), dcp::ColourConversion::xyz_to_srgb(), image->data()[0], image->stride()[0], note);
88         } else {
89                 dcp::xyz_to_rgb (_stereo->xyz_image (_eye), dcp::ColourConversion::xyz_to_srgb(), image->data()[0], image->stride()[0], note);
90         }
91
92         return image;
93 }
94
95 void
96 J2KImageProxy::add_metadata (xmlpp::Node* node) const
97 {
98         node->add_child("Type")->add_child_text (N_("J2K"));
99         node->add_child("Width")->add_child_text (dcp::raw_convert<string> (_size.width));
100         node->add_child("Height")->add_child_text (dcp::raw_convert<string> (_size.height));
101         if (_stereo) {
102                 node->add_child("Eye")->add_child_text (dcp::raw_convert<string> (_eye));
103                 node->add_child("LeftSize")->add_child_text (dcp::raw_convert<string> (_stereo->left_j2k_size ()));
104                 node->add_child("RightSize")->add_child_text (dcp::raw_convert<string> (_stereo->right_j2k_size ()));
105         } else {
106                 node->add_child("Size")->add_child_text (dcp::raw_convert<string> (_mono->j2k_size ()));
107         }
108 }
109
110 void
111 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
112 {
113         if (_mono) {
114                 socket->write (_mono->j2k_data(), _mono->j2k_size ());
115         } else {
116                 socket->write (_stereo->left_j2k_data(), _stereo->left_j2k_size ());
117                 socket->write (_stereo->right_j2k_data(), _stereo->right_j2k_size ());
118         }
119 }
120
121 shared_ptr<EncodedData>
122 J2KImageProxy::j2k () const
123 {
124         if (_mono) {
125                 return shared_ptr<EncodedData> (new EncodedData (_mono->j2k_data(), _mono->j2k_size()));
126         } else {
127                 if (_eye == dcp::EYE_LEFT) {
128                         return shared_ptr<EncodedData> (new EncodedData (_stereo->left_j2k_data(), _stereo->left_j2k_size()));
129                 } else {
130                         return shared_ptr<EncodedData> (new EncodedData (_stereo->right_j2k_data(), _stereo->right_j2k_size()));
131                 }
132         }
133 }