Use make_shared<>.
[dcpomatic.git] / src / lib / j2k_image_proxy.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "j2k_image_proxy.h"
22 #include "dcpomatic_socket.h"
23 #include "image.h"
24 #include "raw_convert.h"
25 #include <dcp/openjpeg_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 <dcp/j2k.h>
31 #include <libcxml/cxml.h>
32 #include <libxml++/libxml++.h>
33 #include <boost/make_shared.hpp>
34 #include <iostream>
35
36 #include "i18n.h"
37
38 using std::string;
39 using std::cout;
40 using boost::shared_ptr;
41 using boost::optional;
42 using boost::dynamic_pointer_cast;
43 using boost::make_shared;
44 using dcp::Data;
45
46 /** Construct a J2KImageProxy from a JPEG2000 file */
47 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size)
48         : _data (path)
49         , _size (size)
50 {
51
52 }
53
54 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size)
55         : _data (frame->j2k_size ())
56         , _size (size)
57 {
58         memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
59 }
60
61 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye)
62         : _size (size)
63         , _eye (eye)
64 {
65         switch (eye) {
66         case dcp::EYE_LEFT:
67                 _data = Data (frame->left_j2k_size ());
68                 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
69                 break;
70         case dcp::EYE_RIGHT:
71                 _data = Data (frame->right_j2k_size ());
72                 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
73                 break;
74         }
75 }
76
77 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
78 {
79         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
80         if (xml->optional_number_child<int> ("Eye")) {
81                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
82         }
83         _data = Data (xml->number_child<int> ("Size"));
84         socket->read (_data.data().get (), _data.size ());
85 }
86
87 void
88 J2KImageProxy::ensure_j2k () const
89 {
90         if (!_j2k) {
91                 _j2k = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0);
92         }
93 }
94
95 shared_ptr<Image>
96 J2KImageProxy::image (optional<dcp::NoteHandler>) const
97 {
98         ensure_j2k ();
99
100         if (_j2k->precision(0) < 12) {
101                 int const shift = 12 - _j2k->precision (0);
102                 for (int c = 0; c < 3; ++c) {
103                         int* p = _j2k->data (c);
104                         for (int y = 0; y < _j2k->size().height; ++y) {
105                                 for (int x = 0; x < _j2k->size().width; ++x) {
106                                         *p++ <<= shift;
107                                 }
108                         }
109                 }
110         }
111
112         shared_ptr<Image> image = make_shared<Image> (pixel_format(), _size, true);
113
114         /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
115            the data is 12-bit either way.
116         */
117
118         int p = 0;
119         for (int y = 0; y < _j2k->size().height; ++y) {
120                 uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]);
121                 for (int x = 0; x < _j2k->size().width; ++x) {
122                         for (int c = 0; c < 3; ++c) {
123                                 *q++ = _j2k->data(c)[p] << 4;
124                         }
125                         ++p;
126                 }
127         }
128
129         return image;
130 }
131
132 void
133 J2KImageProxy::add_metadata (xmlpp::Node* node) const
134 {
135         node->add_child("Type")->add_child_text (N_("J2K"));
136         node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
137         node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
138         if (_eye) {
139                 node->add_child("Eye")->add_child_text (raw_convert<string> (_eye.get ()));
140         }
141         node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
142 }
143
144 void
145 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
146 {
147         socket->write (_data.data().get(), _data.size());
148 }
149
150 bool
151 J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
152 {
153         shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (other);
154         if (!jp) {
155                 return false;
156         }
157
158         if (_data.size() != jp->_data.size()) {
159                 return false;
160         }
161
162         return memcmp (_data.data().get(), jp->_data.data().get(), _data.size()) == 0;
163 }
164
165 AVPixelFormat
166 J2KImageProxy::pixel_format () const
167 {
168         ensure_j2k ();
169
170         if (_j2k->srgb ()) {
171                 return AV_PIX_FMT_RGB48LE;
172         }
173
174         return AV_PIX_FMT_XYZ12LE;
175 }
176
177 J2KImageProxy::J2KImageProxy (Data data, dcp::Size size)
178         : _data (data)
179         , _size (size)
180 {
181
182 }