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