2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
20 /** @file src/openjpeg_image.cc
21 * @brief OpenJPEGImage class.
24 #include "openjpeg_image.h"
25 #include "dcp_assert.h"
31 /** Construct an OpenJPEGImage, taking ownership of the opj_image_t */
32 OpenJPEGImage::OpenJPEGImage (opj_image_t* image)
35 DCP_ASSERT (_opj_image->numcomps == 3);
38 /** Construct a new OpenJPEGImage with undefined contents.
39 * @param size Size for the frame in pixels.
41 OpenJPEGImage::OpenJPEGImage (Size size)
43 opj_image_cmptparm_t cmptparm[3];
45 for (int i = 0; i < 3; ++i) {
48 cmptparm[i].w = size.width;
49 cmptparm[i].h = size.height;
52 cmptparm[i].prec = 12;
57 /* XXX: is this _SRGB right? */
58 _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
59 if (_opj_image == 0) {
60 throw std::runtime_error ("could not create libopenjpeg image");
65 _opj_image->x1 = size.width;
66 _opj_image->y1 = size.height;
69 /** OpenJPEGImage destructor */
70 OpenJPEGImage::~OpenJPEGImage ()
72 opj_image_destroy (_opj_image);
75 /** @param c Component index (0, 1 or 2)
76 * @return Pointer to the data for component c.
79 OpenJPEGImage::data (int c) const
81 DCP_ASSERT (c >= 0 && c < 3);
82 return _opj_image->comps[c].data;
85 /** @return Size of the image in pixels */
87 OpenJPEGImage::size () const
89 /* XXX: this may not be right; x0 and y0 can presumably be non-zero */
90 return dcp::Size (_opj_image->x1, _opj_image->y1);
94 OpenJPEGImage::precision (int component) const
96 return _opj_image->comps[component].prec;
100 OpenJPEGImage::srgb () const
102 return _opj_image->color_space == OPJ_CLRSPC_SRGB;