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