2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
22 #include "descriptor.h"
25 #include <boost/property_tree/ptree.hpp>
29 namespace caspar { namespace psd {
31 class descriptor::context::scoped_holder
33 descriptor::context::ptr_type ctx_;
35 explicit scoped_holder(const std::wstring& key, descriptor::context::ptr_type ctx) : ctx_(ctx)
37 Ptree *parent = ctx_->stack.back();
38 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
39 ctx_->stack.push_back(child);
43 ctx_->stack.pop_back();
47 descriptor::descriptor() : context_(std::make_shared<context>())
49 context_->stack.push_back(&context_->root);
51 descriptor::descriptor(const std::wstring& key, context::ptr_type context) : context_(context)
53 Ptree *parent = context_->stack.back();
54 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
55 context->stack.push_back(child);
57 descriptor::~descriptor()
59 context_->stack.pop_back();
62 void descriptor::populate(bigendian_file_input_stream& stream)
64 stream.read_unicode_string();
65 stream.read_id_string();
66 unsigned long element_count = stream.read_long();
67 for (unsigned long element_index = 0; element_index < element_count; ++element_index)
69 std::wstring key = stream.read_id_string();
70 read_value(key, stream);
74 void descriptor::read_value(const std::wstring& key, bigendian_file_input_stream& stream)
76 unsigned int type = stream.read_long();
82 descriptor desc(key, context_);
83 desc.populate(stream);
89 context_->stack.back()->put(key, stream.read_double());
95 context_->stack.back()->put(key, stream.read_unicode_string());
101 context_->stack.back()->put(stream.read_id_string(), stream.read_id_string());
107 context_->stack.back()->put(key, static_cast<long>(stream.read_long()));
113 context_->stack.back()->put(key, stream.read_byte());
119 context::scoped_holder list(key, context_);
120 auto count = stream.read_long();
121 for(unsigned long i = 0; i < count; ++i)
123 read_value(L"li", stream);
130 unsigned long rawdata_length = stream.read_long();
131 std::vector<char> rawdata(rawdata_length);
132 stream.read(rawdata.data(), rawdata_length);
134 std::wstring data_str(rawdata.begin(), rawdata.end());
135 context_->stack.back()->put(key, data_str);
141 context::scoped_holder list(key, context_);
142 unsigned long unit = stream.read_long();
143 std::string type(reinterpret_cast<char*>(&unit), 4);
144 std::wstring wtype(type.rbegin(), type.rend());
145 context_->stack.back()->put(wtype, stream.read_double());
155 //descriptor type not supported yet
156 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor type not supported yet"));