8ea3afba3aeaf5d54d87a2320c839a90efa356eb
[libdcp.git] / src / xyz_frame.cc
1 /*
2     Copyright (C) 2012-2013 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 <cassert>
21 #include <stdexcept>
22 #include "xyz_frame.h"
23
24 using namespace libdcp;
25
26 /** Construct an XYZFrame, taking ownership of the opj_image_t */
27 XYZFrame::XYZFrame (opj_image_t* image)
28         : _opj_image (image)
29 {
30         assert (_opj_image->numcomps == 3);
31 }
32
33 XYZFrame::XYZFrame (Size size)
34 {
35         opj_image_cmptparm_t cmptparm[3];
36         
37         for (int i = 0; i < 3; ++i) {
38                 cmptparm[i].dx = 1;
39                 cmptparm[i].dy = 1;
40                 cmptparm[i].w = size.width;
41                 cmptparm[i].h = size.height;
42                 cmptparm[i].x0 = 0;
43                 cmptparm[i].y0 = 0;
44                 cmptparm[i].prec = 12;
45                 cmptparm[i].bpp = 12;
46                 cmptparm[i].sgnd = 0;
47         }
48
49         /* XXX: is this _SRGB right? */
50         _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
51         if (_opj_image == 0) {
52                 throw std::runtime_error ("could not create libopenjpeg image");
53         }
54
55         _opj_image->x0 = 0;
56         _opj_image->y0 = 0;
57         _opj_image->x1 = size.width;
58         _opj_image->y1 = size.height;
59 }
60
61 XYZFrame::~XYZFrame ()
62 {
63         opj_image_destroy (_opj_image);
64 }
65
66 int *
67 XYZFrame::data (int c) const
68 {
69         assert (c >= 0 && c < 3);
70         return _opj_image->comps[c].data;
71 }
72
73 libdcp::Size
74 XYZFrame::size () const
75 {
76         /* XXX: this may not be right; x0 and y0 can presumably be non-zero */
77         return libdcp::Size (_opj_image->x1, _opj_image->y1);
78 }