2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "j2k_image_proxy.h"
21 #include "dcpomatic_socket.h"
24 #include "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>
34 using boost::shared_ptr;
35 using boost::optional;
37 /** Construct a J2KImageProxy from a JPEG2000 file */
38 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size)
45 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size)
46 : _data (frame->j2k_size ())
49 memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
52 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye)
58 _data = Data (frame->left_j2k_size ());
59 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
62 _data = Data (frame->right_j2k_size ());
63 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
68 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
70 _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
71 if (xml->optional_number_child<int> ("Eye")) {
72 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
74 _data = Data (xml->number_child<int> ("Size"));
75 socket->read (_data.data().get (), _data.size ());
79 J2KImageProxy::image (optional<dcp::NoteHandler> note) const
81 shared_ptr<Image> image (new Image (PIX_FMT_RGB48LE, _size, true));
84 dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0),
85 dcp::ColourConversion::srgb_to_xyz(),
95 J2KImageProxy::add_metadata (xmlpp::Node* node) const
97 node->add_child("Type")->add_child_text (N_("J2K"));
98 node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
99 node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
101 node->add_child("Eye")->add_child_text (raw_convert<string> (_eye.get ()));
103 node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
107 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
109 socket->write (_data.data().get(), _data.size());