]> git.sesse.net Git - casparcg/blob - modules/psd/descriptor.cpp
* Implemented supports for timeline marks in scene_producer. A mark is set on a speci...
[casparcg] / modules / psd / descriptor.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG 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 CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
20 */
21
22 #include "descriptor.h"
23 #include "misc.h"
24
25 #include <common/log.h>
26
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
29
30 #include <memory>
31
32 namespace caspar { namespace psd {
33
34         std::wstring debug_ptree(const boost::property_tree::wptree& tree)
35         {
36                 std::wstringstream str;
37                 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
38                 boost::property_tree::write_xml(str, tree, w);
39                 str.flush();
40                 return str.str();
41         }
42
43         class descriptor::context::scoped_holder
44         {
45                 descriptor::context::ptr_type ctx_;
46         public:
47                 explicit scoped_holder(const std::wstring& key, descriptor::context::ptr_type ctx) : ctx_(ctx) 
48                 {
49                         Ptree *parent = ctx_->stack.back();
50                         Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
51                         ctx_->stack.push_back(child);
52                 }
53                 ~scoped_holder()
54                 {
55                         ctx_->stack.pop_back();
56                 }
57         };
58
59         descriptor::descriptor(const std::wstring& debug_name) : context_(std::make_shared<context>(debug_name))
60         {
61                 context_->stack.push_back(&context_->root);
62         }
63         descriptor::descriptor(const std::wstring& key, context::ptr_type context) : context_(context)
64         {
65                 Ptree *parent = context_->stack.back();
66                 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
67                 context->stack.push_back(child);
68         }
69         descriptor::~descriptor()
70         {
71                 context_->stack.pop_back();
72         }
73
74 void descriptor::populate(bigendian_file_input_stream& stream)
75 {
76         stream.read_unicode_string();
77         stream.read_id_string();
78         unsigned long element_count = stream.read_long();
79         for (unsigned long element_index = 0; element_index < element_count; ++element_index)
80         {
81                 std::wstring key = stream.read_id_string();
82                 read_value(key, stream);
83         }
84
85         if (context_->stack.size() == 1)
86                 CASPAR_LOG(trace) << context_->debug_name << L":\n\n" << debug_ptree(context_->root) << L"\n";
87 }
88
89 void descriptor::read_value(const std::wstring& key, bigendian_file_input_stream& stream)
90 {
91         unsigned int type = stream.read_long();
92
93         switch(type)
94         {
95         case 'Objc': 
96                 {
97                         descriptor desc(key, context_);
98                         desc.populate(stream);
99                 }
100                 break;
101
102         case 'doub': 
103                 {
104                         context_->stack.back()->put(key, stream.read_double());
105                 }
106                 break;
107
108         case 'TEXT':
109                 {
110                         context_->stack.back()->put(key, stream.read_unicode_string());
111                 }
112                 break;
113
114         case 'enum':
115                 {
116                         context_->stack.back()->put(stream.read_id_string(), stream.read_id_string());
117                 }
118                 break;
119
120         case 'long':
121                 {
122                         context_->stack.back()->put(key, static_cast<long>(stream.read_long()));
123                 }
124                 break;
125
126         case 'bool': 
127                 {
128                         context_->stack.back()->put(key, stream.read_byte());
129                 }
130                 break;
131
132         case 'VlLs': 
133                 {
134                         context::scoped_holder list(key, context_);
135                         auto count = stream.read_long();
136                         for(unsigned long i = 0; i < count; ++i)
137                         {
138                                 read_value(L"li", stream);
139                         }
140                 }
141                 break;
142
143         case 'tdta':
144                 {
145                         unsigned long rawdata_length = stream.read_long();
146                         std::vector<char> rawdata(rawdata_length);
147                         stream.read(rawdata.data(), rawdata_length);
148
149                         std::wstring data_str(rawdata.begin(), rawdata.end());
150                         context_->stack.back()->put(key, data_str);
151                 }
152                 break;
153
154         case 'UntF': 
155                 {
156                         context::scoped_holder list(key, context_);
157                         unsigned long unit = stream.read_long();
158                         std::string type(reinterpret_cast<char*>(&unit), 4);
159                         std::wstring wtype(type.rbegin(), type.rend());
160                         context_->stack.back()->put(wtype, stream.read_double());
161                 }
162                 break;
163
164         case 'obj ': 
165         case 'GlbO': 
166         case 'type':
167         case 'GlbC':
168         case 'alis':
169         default:
170                 //descriptor type not supported yet
171                 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor type not supported yet"));
172         }
173 }
174
175 }       //namespace psd
176 }       //namespace caspar