]> git.sesse.net Git - casparcg/blob - modules/psd/layer.h
Merge pull request #462 from pkeuter/patch-1
[casparcg] / modules / psd / layer.h
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 #pragma once
23
24 #include "util/bigendian_file_input_stream.h"
25 #include "image.h"
26 #include "misc.h"
27 #include "channel.h"
28
29 #include <boost/property_tree/ptree_fwd.hpp>
30
31 #include <vector>
32 #include <string>
33 #include <common/memory.h>
34
35 namespace caspar { namespace psd {
36
37 class layer;
38 typedef std::shared_ptr<layer> layer_ptr;
39 class psd_document;
40
41 class layer
42 {
43         struct impl;
44         spl::shared_ptr<impl> impl_;
45
46 public:
47         class mask_info;
48
49         class vector_mask_info
50         {
51                 std::uint8_t                    flags_;
52                 psd::rect<int>                  rect_;
53                 std::vector<point<int>> knots_;\r
54
55                 friend class layer::mask_info;
56                 bool populate(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height);
57
58         public:
59                 enum class flags {
60                         none = 0,
61                         inverted = 1,
62                         unlinked = 2,
63                         disabled = 4,
64                         unsupported = 128
65                 };
66
67                 vector_mask_info() : flags_(0)
68                 {}
69
70                 bool enabled() const { return (flags_ & static_cast<std::uint8_t>(flags::disabled)) == 0; }
71                 bool linked() const { return (flags_ & static_cast<std::uint8_t>(flags::unlinked)) == 0; }
72                 bool inverted() const { return (flags_ & static_cast<std::uint8_t>(flags::inverted)) == static_cast<std::uint8_t>(flags::inverted); }
73                 bool unsupported() const { return (flags_ & static_cast<std::uint8_t>(flags::unsupported)) == static_cast<std::uint8_t>(flags::unsupported); }
74
75                 bool empty() { return rect_.empty() && knots_.empty(); }
76
77                 const psd::rect<int>& rect() const { return rect_; }
78                 const std::vector<point<int>>& knots() const { return knots_; }
79         };
80
81         class mask_info
82         {
83                 friend struct layer::impl;
84                 friend class layer::vector_mask_info;
85
86                 void read_mask_data(bigendian_file_input_stream&);
87                 void read_vector_mask_data(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height)
88                 {
89                         vector_mask_.reset(new vector_mask_info);
90                         vector_mask_->populate(length, stream, doc_width, doc_height);
91                 }
92
93                 image8bit_ptr   bitmap_;
94                 std::uint8_t    default_value_;
95                 std::uint8_t    flags_;
96                 psd::rect<int>  rect_;
97
98                 std::unique_ptr<vector_mask_info> vector_mask_;
99                 std::unique_ptr<mask_info> total_mask_;
100
101                 void create_bitmap() {
102                         bitmap_ = std::make_shared<image8bit>(rect_.size.width, rect_.size.height, 1);
103                 }
104
105         public:
106                 mask_info() : default_value_(0), flags_(0)
107                 {}
108
109                 bool enabled() const { return (flags_ & 2) == 0; }
110                 bool linked() const { return (flags_ & 1) == 0;  }
111                 bool inverted() const { return (flags_ & 4) == 4; }
112
113                 bool empty() const { return rect_.empty(); }
114                 
115                 bool has_vector() const { return (vector_mask_ && !vector_mask_->empty() && vector_mask_->enabled()); }
116                 bool has_bitmap() const { return (!vector_mask_ && !empty()) || (vector_mask_ && total_mask_); }
117                 const std::unique_ptr<vector_mask_info>& vector() const { return vector_mask_; }
118
119                 const psd::rect<int>& rect() const { return rect_; }
120                 const image8bit_ptr& bitmap() const { return bitmap_; }
121         };
122
123         layer();
124
125         void populate(bigendian_file_input_stream&, const psd_document&);
126         void read_channel_data(bigendian_file_input_stream&);
127
128         const std::wstring& name() const;
129         int opacity() const;
130         caspar::core::blend_mode blend_mode() const;
131         int sheet_color() const;
132         bool is_visible();
133         bool is_position_protected();
134
135         const mask_info& mask() const;
136
137         const psd::point<double>& text_pos() const;
138         const psd::point<double>& scale() const;
139         const double angle() const;
140         const double shear() const;
141
142         bool is_text() const;
143         const boost::property_tree::wptree& text_data() const;
144
145         bool is_solid() const;
146         color<std::uint8_t> solid_color() const;
147
148         bool has_timeline() const;
149         const boost::property_tree::wptree& timeline_data() const;
150
151         const point<int>& location() const;
152         const psd::size<int>& size() const;
153         const image8bit_ptr& bitmap() const;
154
155         layer_type group_mode() const;
156
157         int link_group_id() const;
158         void set_link_group_id(int id);
159
160         bool is_explicit_dynamic() const;
161         bool is_static() const;
162         bool is_movable() const;
163         bool is_resizable() const;
164         bool is_placeholder() const;
165         bool is_cornerpin() const;
166         layer_tag tags() const;
167 };
168
169 ENUM_ENABLE_BITWISE(layer::vector_mask_info::flags);
170
171 }       //namespace psd
172 }       //namespace caspar