Include tidying src/lib/a-j*.h
[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 "data.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 <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::optional;
39 using boost::dynamic_pointer_cast;
40
41 /** Construct a J2KImageProxy from a JPEG2000 file */
42 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size)
43         : _data (path)
44         , _size (size)
45 {
46
47 }
48
49 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size)
50         : _data (frame->j2k_size ())
51         , _size (size)
52 {
53         memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
54 }
55
56 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye)
57         : _size (size)
58         , _eye (eye)
59 {
60         switch (eye) {
61         case dcp::EYE_LEFT:
62                 _data = Data (frame->left_j2k_size ());
63                 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
64                 break;
65         case dcp::EYE_RIGHT:
66                 _data = Data (frame->right_j2k_size ());
67                 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
68                 break;
69         }
70 }
71
72 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
73 {
74         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
75         if (xml->optional_number_child<int> ("Eye")) {
76                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
77         }
78         _data = Data (xml->number_child<int> ("Size"));
79         socket->read (_data.data().get (), _data.size ());
80 }
81
82 shared_ptr<Image>
83 J2KImageProxy::image (optional<dcp::NoteHandler> note) const
84 {
85         shared_ptr<Image> image (new Image (PIX_FMT_RGB48LE, _size, true));
86
87         shared_ptr<dcp::OpenJPEGImage> oj = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), 0);
88
89         if (oj->opj_image()->comps[0].prec < 12) {
90                 int const shift = 12 - oj->opj_image()->comps[0].prec;
91                 for (int c = 0; c < 3; ++c) {
92                         int* p = oj->data (c);
93                         for (int y = 0; y < oj->size().height; ++y) {
94                                 for (int x = 0; x < oj->size().width; ++x) {
95                                         *p++ <<= shift;
96                                 }
97                         }
98                 }
99         }
100
101         if (oj->opj_image()->color_space == CLRSPC_SRGB) {
102                 /* No XYZ -> RGB conversion necessary; just copy and interleave the values */
103                 int p = 0;
104                 for (int y = 0; y < oj->size().height; ++y) {
105                         uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]);
106                         for (int x = 0; x < oj->size().width; ++x) {
107                                 for (int c = 0; c < 3; ++c) {
108                                         *q++ = oj->data(c)[p] << 4;
109                                 }
110                                 ++p;
111                         }
112                 }
113         } else {
114                 dcp::xyz_to_rgb (oj, dcp::ColourConversion::srgb_to_xyz(), image->data()[0], image->stride()[0], note);
115         }
116
117         return image;
118 }
119
120 void
121 J2KImageProxy::add_metadata (xmlpp::Node* node) const
122 {
123         node->add_child("Type")->add_child_text (N_("J2K"));
124         node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
125         node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
126         if (_eye) {
127                 node->add_child("Eye")->add_child_text (raw_convert<string> (_eye.get ()));
128         }
129         node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
130 }
131
132 void
133 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
134 {
135         socket->write (_data.data().get(), _data.size());
136 }
137
138 bool
139 J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
140 {
141         shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (other);
142         if (!jp) {
143                 return false;
144         }
145
146         if (_data.size() != jp->_data.size()) {
147                 return false;
148         }
149
150         return memcmp (_data.data().get(), jp->_data.data().get(), _data.size()) == 0;
151 }