No-op; Fix GPL address and mention libdcp by name.
[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 */
20
21 /** @file  src/openjpeg_image.cc
22  *  @brief OpenJPEGImage class.
23  */
24
25 #include "openjpeg_image.h"
26 #include "dcp_assert.h"
27 #include <openjpeg.h>
28 #include <stdexcept>
29
30 using namespace dcp;
31
32 /** Construct an OpenJPEGImage, taking ownership of the opj_image_t */
33 OpenJPEGImage::OpenJPEGImage (opj_image_t* image)
34         : _opj_image (image)
35 {
36         DCP_ASSERT (_opj_image->numcomps == 3);
37 }
38
39 /** Construct a new OpenJPEGImage with undefined contents.
40  *  @param size Size for the frame in pixels.
41  */
42 OpenJPEGImage::OpenJPEGImage (Size size)
43 {
44         opj_image_cmptparm_t cmptparm[3];
45
46         for (int i = 0; i < 3; ++i) {
47                 cmptparm[i].dx = 1;
48                 cmptparm[i].dy = 1;
49                 cmptparm[i].w = size.width;
50                 cmptparm[i].h = size.height;
51                 cmptparm[i].x0 = 0;
52                 cmptparm[i].y0 = 0;
53                 cmptparm[i].prec = 12;
54                 cmptparm[i].bpp = 12;
55                 cmptparm[i].sgnd = 0;
56         }
57
58         /* XXX: is this _SRGB right? */
59         _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
60         if (_opj_image == 0) {
61                 throw std::runtime_error ("could not create libopenjpeg image");
62         }
63
64         _opj_image->x0 = 0;
65         _opj_image->y0 = 0;
66         _opj_image->x1 = size.width;
67         _opj_image->y1 = size.height;
68 }
69
70 /** OpenJPEGImage destructor */
71 OpenJPEGImage::~OpenJPEGImage ()
72 {
73         opj_image_destroy (_opj_image);
74 }
75
76 /** @param c Component index (0, 1 or 2)
77  *  @return Pointer to the data for component c.
78  */
79 int *
80 OpenJPEGImage::data (int c) const
81 {
82         DCP_ASSERT (c >= 0 && c < 3);
83         return _opj_image->comps[c].data;
84 }
85
86 /** @return Size of the image in pixels */
87 dcp::Size
88 OpenJPEGImage::size () const
89 {
90         /* XXX: this may not be right; x0 and y0 can presumably be non-zero */
91         return dcp::Size (_opj_image->x1, _opj_image->y1);
92 }
93
94 int
95 OpenJPEGImage::precision (int component) const
96 {
97         return _opj_image->comps[component].prec;
98 }
99
100 bool
101 OpenJPEGImage::srgb () const
102 {
103         return _opj_image->color_space == OPJ_CLRSPC_SRGB;
104 }