Rename XYZImage -> OpenJPEGImage.
[libdcp.git] / examples / read_dcp.cc
1 /*
2     Copyright (C) 2012-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 /* If you are using an installed libdcp, these #includes would need to be changed to
21 #include <dcp/dcp.h>
22 #include <dcp/picture_asset.h>
23 ... etc. ...
24 */
25
26 #include "dcp.h"
27 #include "cpl.h"
28 #include "reel.h"
29 #include "reel_picture_asset.h"
30 #include "mono_picture_frame.h"
31 #include "mono_picture_asset.h"
32 #include "stereo_picture_asset.h"
33 #include "sound_asset.h"
34 #include "subtitle_asset.h"
35 #include "openjpeg_image.h"
36 #include "colour_conversion.h"
37 #include "rgb_xyz.h"
38 #include <Magick++.h>
39 #include <boost/scoped_array.hpp>
40
41 /** @file examples/read_dcp.cc
42  *  @brief Shows how to read a DCP.
43  */
44
45 int
46 main ()
47 {
48         /* Create a DCP, specifying where our existing data is */
49         dcp::DCP dcp ("/home/carl/diagonal.com/APPASSIONATA_TLR_F_UK-DEFR_CH_51_2K_LOK_20121115_DGL_OV");
50         /* Read the DCP to find out about it */
51         dcp.read ();
52
53         if (dcp.encrypted ()) {
54                 std::cout << "DCP is encrypted.\n";
55         } else {
56                 std::cout << "DCP is not encrypted.\n";
57         }
58
59         std::cout << "DCP has " << dcp.cpls().size() << " CPLs.\n";
60         std::list<boost::shared_ptr<dcp::Asset> > assets = dcp.assets ();
61         std::cout << "DCP has " << assets.size() << " assets.\n";
62         for (std::list<boost::shared_ptr<dcp::Asset> >::const_iterator i = assets.begin(); i != assets.end(); ++i) {
63                 if (boost::dynamic_pointer_cast<dcp::MonoPictureAsset> (*i)) {
64                         std::cout << "2D picture\n";
65                 } else if (boost::dynamic_pointer_cast<dcp::StereoPictureAsset> (*i)) {
66                         std::cout << "3D picture\n";
67                 } else if (boost::dynamic_pointer_cast<dcp::SoundAsset> (*i)) {
68                         std::cout << "Sound\n";
69                 } else if (boost::dynamic_pointer_cast<dcp::SubtitleAsset> (*i)) {
70                         std::cout << "Subtitle\n";
71                 } else if (boost::dynamic_pointer_cast<dcp::CPL> (*i)) {
72                         std::cout << "CPL\n";
73                 }
74                 std::cout << "\t" << (*i)->file().leaf().string() << "\n";
75         }
76
77         /* Take the first CPL */
78         boost::shared_ptr<dcp::CPL> cpl = dcp.cpls().front ();
79
80         /* Get the picture asset in the first reel */
81         boost::shared_ptr<dcp::MonoPictureAsset> picture_asset = boost::dynamic_pointer_cast<dcp::MonoPictureAsset> (
82                 cpl->reels().front()->main_picture()->asset()
83                 );
84
85         /* Get the 1000th frame of it */
86         boost::shared_ptr<const dcp::MonoPictureFrame> picture_frame_j2k = picture_asset->get_frame(999);
87
88         /* Get the frame as an XYZ image */
89         boost::shared_ptr<const dcp::OpenJPEGImage> picture_image_xyz = picture_frame_j2k->xyz_image ();
90
91         /* Convert to ARGB */
92         boost::scoped_array<uint8_t> rgba (new uint8_t[picture_image_xyz->size().width * picture_image_xyz->size().height * 4]);
93         dcp::xyz_to_rgba (picture_image_xyz, dcp::ColourConversion::srgb_to_xyz(), rgba.get ());
94
95         Magick::Image image (picture_image_xyz->size().width, picture_image_xyz->size().height, "BGRA", Magick::CharPixel, rgba.get ());
96         image.write ("frame.png");
97
98         return 0;
99 }