]> git.sesse.net Git - casparcg/blob - modules/psd/layer.h
95baf8aaf4bee3ef056661a66681ee62e7571a5d
[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                 enum class flags {
52                         inverted = 1,
53                         unlinked = 2,
54                         disabled = 4,
55                         unsupported = 128
56                 };
57
58                 std::uint8_t    flags_;
59                 psd::rect<int>          rect_;
60
61                 friend class layer::mask_info;
62                 bool populate(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height);
63
64         public:
65                 vector_mask_info() : flags_(0)
66                 {}
67
68                 bool enabled() const { return (flags_ & static_cast<std::uint8_t>(flags::disabled)) == 0; }
69                 bool linked() const { return (flags_ & static_cast<std::uint8_t>(flags::unlinked)) == 0; }
70                 bool inverted() const { return (flags_ & static_cast<std::uint8_t>(flags::inverted)) == static_cast<std::uint8_t>(flags::inverted); }
71                 bool unsupported() const { return (flags_ & static_cast<std::uint8_t>(flags::unsupported)) == static_cast<std::uint8_t>(flags::unsupported); }
72
73                 bool empty() { return rect_.empty(); }
74
75                 const psd::rect<int>& rect() const { return rect_; }
76         };
77
78         class mask_info
79         {
80                 friend struct layer::impl;
81                 friend class layer::vector_mask_info;
82
83                 void read_mask_data(bigendian_file_input_stream&);
84                 void read_vector_mask_data(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height)
85                 {
86                         vector_mask_.reset(new vector_mask_info);
87                         vector_mask_->populate(length, stream, doc_width, doc_height);
88                 }
89
90                 image8bit_ptr   bitmap_;
91                 std::uint8_t    default_value_;
92                 std::uint8_t    flags_;
93                 psd::rect<int>          rect_;
94
95                 std::unique_ptr<vector_mask_info> vector_mask_;
96                 std::unique_ptr<mask_info> total_mask_;
97
98                 void create_bitmap() {
99                         bitmap_ = std::make_shared<image8bit>(rect_.size.width, rect_.size.height, 1);
100                 }
101
102         public:
103                 mask_info() : default_value_(0), flags_(0)
104                 {}
105
106                 bool enabled() const { return (flags_ & 2) == 0; }
107                 bool linked() const { return (flags_ & 1) == 0;  }
108                 bool inverted() const { return (flags_ & 4) == 4; }
109
110                 bool empty() const { return rect_.empty(); }
111                 
112                 bool has_vector() const { return (vector_mask_ && !vector_mask_->empty() && vector_mask_->enabled()); }
113                 bool has_bitmap() const { return (!vector_mask_ && !empty()) || (vector_mask_ && total_mask_); }
114                 const std::unique_ptr<vector_mask_info>& vector() { return vector_mask_; }
115
116                 const psd::rect<int>& rect() const { return rect_; }
117                 const image8bit_ptr& bitmap() const { return bitmap_; }
118         };
119
120         layer();
121
122         void populate(bigendian_file_input_stream&, const psd_document&);
123         void read_channel_data(bigendian_file_input_stream&);
124
125         const std::wstring& name() const;
126         int opacity() const;
127         int sheet_color() const;
128         bool is_visible();
129         bool is_position_protected();
130
131         double text_scale() const;
132         bool is_text() const;
133         const boost::property_tree::wptree& text_data() const;
134
135         bool is_solid() const;
136         color<std::uint8_t> solid_color() const;
137
138         bool has_timeline() const;
139         const boost::property_tree::wptree& timeline_data() const;
140
141         const point<int>& location() const;
142         const psd::size<int>& size() const;
143         const image8bit_ptr& bitmap() const;
144
145         layer_type group_mode() const;
146
147         int link_group_id() const;
148         void set_link_group_id(int id);
149
150         bool is_explicit_dynamic() const;
151         bool is_static() const;
152         bool is_movable() const;
153         bool is_resizable() const;
154         bool is_placeholder() const;
155         layer_tag tags() const;
156 };
157
158 }       //namespace psd
159 }       //namespace caspar