Add Colour::to_rgb_string.
[libdcp.git] / src / openjpeg_image.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/openjpeg_image.cc
35  *  @brief OpenJPEGImage class.
36  */
37
38 #include "openjpeg_image.h"
39 #include "dcp_assert.h"
40 #include <openjpeg.h>
41 #include <stdexcept>
42
43 using namespace dcp;
44
45 #ifdef LIBDCP_OPENJPEG1
46 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
47 #endif
48
49 /** Construct an OpenJPEGImage, taking ownership of the opj_image_t */
50 OpenJPEGImage::OpenJPEGImage (opj_image_t* image)
51         : _opj_image (image)
52 {
53         DCP_ASSERT (_opj_image->numcomps == 3);
54 }
55
56 /** Construct a new OpenJPEGImage with undefined contents.
57  *  @param size Size for the frame in pixels.
58  */
59 OpenJPEGImage::OpenJPEGImage (Size size)
60 {
61         opj_image_cmptparm_t cmptparm[3];
62
63         for (int i = 0; i < 3; ++i) {
64                 cmptparm[i].dx = 1;
65                 cmptparm[i].dy = 1;
66                 cmptparm[i].w = size.width;
67                 cmptparm[i].h = size.height;
68                 cmptparm[i].x0 = 0;
69                 cmptparm[i].y0 = 0;
70                 cmptparm[i].prec = 12;
71                 cmptparm[i].bpp = 12;
72                 cmptparm[i].sgnd = 0;
73         }
74
75         /* XXX: is this _SRGB right? */
76         _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
77         if (_opj_image == 0) {
78                 throw std::runtime_error ("could not create libopenjpeg image");
79         }
80
81         _opj_image->x0 = 0;
82         _opj_image->y0 = 0;
83         _opj_image->x1 = size.width;
84         _opj_image->y1 = size.height;
85 }
86
87 /** OpenJPEGImage destructor */
88 OpenJPEGImage::~OpenJPEGImage ()
89 {
90         opj_image_destroy (_opj_image);
91 }
92
93 /** @param c Component index (0, 1 or 2)
94  *  @return Pointer to the data for component c.
95  */
96 int *
97 OpenJPEGImage::data (int c) const
98 {
99         DCP_ASSERT (c >= 0 && c < 3);
100         return _opj_image->comps[c].data;
101 }
102
103 /** @return Size of the image in pixels */
104 dcp::Size
105 OpenJPEGImage::size () const
106 {
107         /* XXX: this may not be right; x0 and y0 can presumably be non-zero */
108         return dcp::Size (_opj_image->x1, _opj_image->y1);
109 }
110
111 int
112 OpenJPEGImage::precision (int component) const
113 {
114         return _opj_image->comps[component].prec;
115 }
116
117 bool
118 OpenJPEGImage::srgb () const
119 {
120         return _opj_image->color_space == OPJ_CLRSPC_SRGB;
121 }