0e4d3ca2f21319e494324b32a1c50472230a1cbe
[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 "raw_convert.h"
24 #include <dcp/openjpeg_image.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 <dcp/j2k.h>
30 #include <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
32 #include <iostream>
33
34 #include "i18n.h"
35
36 using std::string;
37 using std::cout;
38 using boost::shared_ptr;
39 using boost::optional;
40 using boost::dynamic_pointer_cast;
41 using dcp::Data;
42
43 /** Construct a J2KImageProxy from a JPEG2000 file */
44 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size)
45         : _data (path)
46         , _size (size)
47 {
48
49 }
50
51 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size)
52         : _data (frame->j2k_size ())
53         , _size (size)
54 {
55         memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
56 }
57
58 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye)
59         : _size (size)
60         , _eye (eye)
61 {
62         switch (eye) {
63         case dcp::EYE_LEFT:
64                 _data = Data (frame->left_j2k_size ());
65                 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
66                 break;
67         case dcp::EYE_RIGHT:
68                 _data = Data (frame->right_j2k_size ());
69                 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
70                 break;
71         }
72 }
73
74 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
75 {
76         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
77         if (xml->optional_number_child<int> ("Eye")) {
78                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
79         }
80         _data = Data (xml->number_child<int> ("Size"));
81         socket->read (_data.data().get (), _data.size ());
82 }
83
84 void
85 J2KImageProxy::ensure_j2k () const
86 {
87         if (!_j2k) {
88                 _j2k = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0);
89         }
90 }
91
92 shared_ptr<Image>
93 J2KImageProxy::image (optional<dcp::NoteHandler>) const
94 {
95         ensure_j2k ();
96
97         if (_j2k->precision(0) < 12) {
98                 int const shift = 12 - _j2k->precision (0);
99                 for (int c = 0; c < 3; ++c) {
100                         int* p = _j2k->data (c);
101                         for (int y = 0; y < _j2k->size().height; ++y) {
102                                 for (int x = 0; x < _j2k->size().width; ++x) {
103                                         *p++ <<= shift;
104                                 }
105                         }
106                 }
107         }
108
109         shared_ptr<Image> image (new Image (pixel_format(), _size, true));
110
111         /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
112            the data is 12-bit either way.
113         */
114
115         int p = 0;
116         for (int y = 0; y < _j2k->size().height; ++y) {
117                 uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]);
118                 for (int x = 0; x < _j2k->size().width; ++x) {
119                         for (int c = 0; c < 3; ++c) {
120                                 *q++ = _j2k->data(c)[p] << 4;
121                         }
122                         ++p;
123                 }
124         }
125
126         return image;
127 }
128
129 void
130 J2KImageProxy::add_metadata (xmlpp::Node* node) const
131 {
132         node->add_child("Type")->add_child_text (N_("J2K"));
133         node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
134         node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
135         if (_eye) {
136                 node->add_child("Eye")->add_child_text (raw_convert<string> (_eye.get ()));
137         }
138         node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
139 }
140
141 void
142 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
143 {
144         socket->write (_data.data().get(), _data.size());
145 }
146
147 bool
148 J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
149 {
150         shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (other);
151         if (!jp) {
152                 return false;
153         }
154
155         if (_data.size() != jp->_data.size()) {
156                 return false;
157         }
158
159         return memcmp (_data.data().get(), jp->_data.data().get(), _data.size()) == 0;
160 }
161
162 AVPixelFormat
163 J2KImageProxy::pixel_format () const
164 {
165         ensure_j2k ();
166
167         if (_j2k->srgb ()) {
168                 return AV_PIX_FMT_RGB48LE;
169         }
170
171         return AV_PIX_FMT_XYZ12LE;
172 }
173
174 J2KImageProxy::J2KImageProxy (Data data, dcp::Size size)
175         : _data (data)
176         , _size (size)
177 {
178
179 }