2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
\r
23 #include "psd_document.h"
\r
24 #include "descriptor.h"
\r
25 #include "util/pdf_reader.h"
\r
27 #include "../image/util/image_algorithms.h"
\r
28 #include "../image/util/image_view.h"
\r
30 #include <common/log.h>
\r
32 #include <boost/property_tree/ptree.hpp>
\r
33 #include <boost/algorithm/string.hpp>
\r
34 #include <boost/lexical_cast.hpp>
\r
36 #include <algorithm>
\r
38 namespace caspar { namespace psd {
\r
40 void layer::mask_info::read_mask_data(bigendian_file_input_stream& stream)
\r
42 auto length = stream.read_long();
\r
50 rect_.location.y = stream.read_long();
\r
51 rect_.location.x = stream.read_long();
\r
52 rect_.size.height = stream.read_long() - rect_.location.y;
\r
53 rect_.size.width = stream.read_long() - rect_.location.x;
\r
55 default_value_ = stream.read_byte();
\r
56 flags_ = stream.read_byte();
\r
57 stream.discard_bytes(2);
\r
61 //we save the information about the total mask in case the vector-mask has an unsupported shape
\r
62 total_mask_.reset(new layer::mask_info);
\r
63 total_mask_->flags_ = stream.read_byte();
\r
64 total_mask_->default_value_ = stream.read_byte();
\r
65 total_mask_->rect_.location.y = stream.read_long();
\r
66 total_mask_->rect_.location.x = stream.read_long();
\r
67 total_mask_->rect_.size.height = stream.read_long() - rect_.location.y;
\r
68 total_mask_->rect_.size.width = stream.read_long() - rect_.location.x;
\r
73 //TODO: Log that we discard a mask that is not supported
\r
74 stream.discard_bytes(length);
\r
79 bool layer::vector_mask_info::populate(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height)
\r
81 std::vector<point<int>> knots;
\r
82 bool smooth_curve = false;
\r
84 stream.read_long(); // version
\r
85 this->flags_ = static_cast<std::uint8_t>(stream.read_long()); // flags
\r
86 int path_records = (length - 8) / 26;
\r
88 auto position = stream.current_position();
\r
90 const int SELECTOR_SIZE = 2;
\r
91 const int PATH_POINT_SIZE = 4 + 4;
\r
92 const int PATH_POINT_RECORD_SIZE = SELECTOR_SIZE + (3 * PATH_POINT_SIZE);
\r
94 for (int i = 1; i <= path_records; ++i)
\r
96 auto selector = stream.read_short();
\r
97 if (selector == 2) //we only concern ourselves with closed paths
\r
99 auto p_y = stream.read_long();
\r
100 auto p_x = stream.read_long();
\r
101 point<int> prev{ static_cast<int>(p_x), static_cast<int>(p_y) };
\r
103 auto a_y = stream.read_long();
\r
104 auto a_x = stream.read_long();
\r
105 point<int> anchor{ static_cast<int>(a_x), static_cast<int>(a_y) };
\r
107 auto n_y = stream.read_long();
\r
108 auto n_x = stream.read_long();
\r
109 point<int> next{ static_cast<int>(n_x), static_cast<int>(n_y) };
\r
111 if (anchor == prev && anchor == next)
\r
112 knots.push_back(anchor);
\r
115 //note that we've got a smooth curve, but continue to iterate through the data
\r
116 smooth_curve = true;
\r
120 auto offset = PATH_POINT_RECORD_SIZE * i;
\r
121 stream.set_position(position + offset);
\r
124 if (smooth_curve || knots.size() != 4) //we can't handle smooth-curves yet and we only support quad-gons
\r
127 flags_ = static_cast<std::uint8_t>(flags::unsupported | flags::disabled);
\r
131 //the path_points are given in fixed-point 8.24 as a ratio with regards to the width/height of the document. we need to divide by 16777215.0f to get the real ratio.
\r
132 float x_ratio = doc_width / 16777215.0f;
\r
133 float y_ratio = doc_height / 16777215.0f;
\r
137 //is it an orthogonal rectangle
\r
138 if (knots[0].x == knots[3].x && knots[1].x == knots[2].x && knots[0].y == knots[1].y && knots[2].y == knots[3].y)
\r
140 rect_.location.x = static_cast<int>(knots[0].x * x_ratio + 0.5f); //add .5 to get propper rounding when converting to integer
\r
141 rect_.location.y = static_cast<int>(knots[0].y * y_ratio + 0.5f); //add .5 to get propper rounding when converting to integer
\r
142 rect_.size.width = static_cast<int>(knots[1].x * x_ratio + 0.5f) - rect_.location.x; //add .5 to get propper rounding when converting to integer
\r
143 rect_.size.height = static_cast<int>(knots[2].y * y_ratio + 0.5f) - rect_.location.y; //add .5 to get propper rounding when converting to integer
\r
145 else //it's could be any kind of quad-gon
\r
147 for (auto& k : knots)
\r
148 knots_.push_back(psd::point<int>{static_cast<int>(k.x * x_ratio + 0.5f), static_cast<int>(k.y * y_ratio + 0.5f)});
\r
156 friend class layer;
\r
158 impl() : blend_mode_(caspar::core::blend_mode::normal), layer_type_(layer_type::content), link_group_id_(0), opacity_(255), sheet_color_(0), baseClipping_(false), flags_(0), protection_flags_(0), masks_count_(0), scale_{ 1.0, 1.0 }, angle_(0), shear_(0), tags_(layer_tag::none)
\r
162 std::vector<channel> channels_;
\r
163 caspar::core::blend_mode blend_mode_;
\r
164 layer_type layer_type_;
\r
165 int link_group_id_;
\r
168 bool baseClipping_;
\r
169 std::uint8_t flags_;
\r
170 std::uint32_t protection_flags_;
\r
171 std::wstring name_;
\r
173 psd::point<double> text_pos_;
\r
174 psd::point<double> scale_;
\r
178 layer::mask_info mask_;
\r
180 rect<int> bitmap_rect_;
\r
181 image8bit_ptr bitmap_;
\r
183 boost::property_tree::wptree text_layer_info_;
\r
184 boost::property_tree::wptree timeline_info_;
\r
186 color<std::uint8_t> solid_color_;
\r
191 void populate(bigendian_file_input_stream& stream, const psd_document& doc)
\r
193 bitmap_rect_.location.y = stream.read_long();
\r
194 bitmap_rect_.location.x = stream.read_long();
\r
195 bitmap_rect_.size.height = stream.read_long() - bitmap_rect_.location.y;
\r
196 bitmap_rect_.size.width = stream.read_long() - bitmap_rect_.location.x;
\r
198 //Get info about the channels in the layer
\r
199 auto channelCount = stream.read_short();
\r
200 for(int channelIndex = 0; channelIndex < channelCount; ++channelIndex)
\r
202 auto id = static_cast<std::int16_t>(stream.read_short());
\r
203 channel c(id, stream.read_long());
\r
208 channels_.push_back(c);
\r
211 auto blendModeSignature = stream.read_long();
\r
212 if(blendModeSignature != '8BIM')
\r
213 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("blendModeSignature != '8BIM'"));
\r
215 blend_mode_ = int_to_blend_mode(stream.read_long());
\r
216 opacity_ = stream.read_byte();
\r
217 baseClipping_ = stream.read_byte() == 1 ? false : true;
\r
218 flags_ = stream.read_byte();
\r
220 stream.discard_bytes(1); //padding
\r
222 auto extras_size = stream.read_long();
\r
223 auto position = stream.current_position();
\r
224 mask_.read_mask_data(stream);
\r
225 read_blending_ranges(stream);
\r
227 stream.read_pascal_string(4); //throw this away. We'll read the unicode version of the name later
\r
229 //Aditional Layer Information
\r
230 auto end_of_layer_info = position + extras_size;
\r
233 while(stream.current_position() < end_of_layer_info)
\r
235 read_chunk(stream, doc);
\r
238 catch(psd_file_format_exception&)
\r
240 stream.set_position(end_of_layer_info);
\r
244 void read_chunk(bigendian_file_input_stream& stream, const psd_document& doc, bool isMetadata = false)
\r
246 auto signature = stream.read_long();
\r
247 if(signature != '8BIM' && signature != '8B64')
\r
248 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("signature != '8BIM' && signature != '8B64'"));
\r
250 auto key = stream.read_long();
\r
252 if(isMetadata) stream.read_long();
\r
254 auto length = stream.read_long();
\r
255 auto end_of_chunk = stream.current_position() + length;
\r
262 read_solid_color(stream);
\r
265 case 'lsct': //group settings (folders)
\r
266 read_group_settings(stream, length);
\r
268 case 'lspf': //protection settings
\r
269 protection_flags_ = stream.read_long();
\r
272 case 'Txt2': //text engine data
\r
276 set_name_and_tags(stream.read_unicode_string());
\r
279 case 'TySh': //type tool object settings
\r
280 read_text_data(stream);
\r
283 case 'shmd': //metadata
\r
284 read_metadata(stream, doc);
\r
288 sheet_color_ = static_cast<std::int16_t>(stream.read_short());
\r
291 case 'lyvr': //layer version
\r
294 case 'lnkD': //linked layer
\r
295 case 'lnk2': //linked layer
\r
296 case 'lnk3': //linked layer
\r
301 mask_.read_vector_mask_data(length, stream, doc.width(), doc.height());
\r
305 read_timeline_data(stream);
\r
312 catch(psd_file_format_exception& ex)
\r
314 //ignore failed chunks silently
\r
315 CASPAR_LOG(warning) << ex.what();
\r
318 stream.set_position(end_of_chunk);
\r
321 void set_name_and_tags(const std::wstring& name) {
\r
322 auto start_bracket = name.find_first_of(L'[');
\r
323 auto end_bracket = name.find_first_of(L']');
\r
324 if (start_bracket == std::wstring::npos && end_bracket == std::wstring::npos) {
\r
328 else if (start_bracket != std::wstring::npos && end_bracket > start_bracket) {
\r
330 tags_ = string_to_layer_tags(name.substr(start_bracket+1, end_bracket-start_bracket-1));
\r
331 name_ = name.substr(end_bracket+1);
\r
336 CASPAR_LOG(warning) << "Mismatching tag-brackets in layer name";
\r
339 boost::trim(name_);
\r
342 void read_solid_color(bigendian_file_input_stream& stream)
\r
344 if(stream.read_long() != 16) //"descriptor version" should be 16
\r
345 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor version should be 16"));
\r
347 descriptor solid_descriptor(L"solid_color");
\r
348 solid_descriptor.populate(stream);
\r
349 solid_color_.red = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Rd ", 0.0) + 0.5);
\r
350 solid_color_.green = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Grn ", 0.0) + 0.5);
\r
351 solid_color_.blue = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Bl ", 0.0) + 0.5);
\r
352 solid_color_.alpha = 255;
\r
355 void read_group_settings(bigendian_file_input_stream& stream, unsigned int length)
\r
357 auto type = stream.read_long();
\r
358 unsigned int sub_type = 0;
\r
362 auto signature = stream.read_long();
\r
363 if (signature != '8BIM')
\r
364 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("signature != '8BIM'"));
\r
366 blend_mode_ = int_to_blend_mode(stream.read_long());
\r
369 sub_type = stream.read_long();
\r
372 layer_type_ = int_to_layer_type(type, sub_type);
\r
375 void read_metadata(bigendian_file_input_stream& stream, const psd_document& doc)
\r
377 int count = stream.read_long();
\r
378 for(int index = 0; index < count; ++index)
\r
379 read_chunk(stream, doc, true);
\r
382 void read_timeline_data(bigendian_file_input_stream& stream)
\r
384 if(stream.read_long() != 16) //"descriptor version" should be 16
\r
385 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor version should be 16"));
\r
387 descriptor timeline_descriptor(L"timeline");
\r
388 timeline_descriptor.populate(stream);
\r
389 timeline_info_.swap(timeline_descriptor.items());
\r
392 void read_text_data(bigendian_file_input_stream& stream)
\r
394 std::wstring text; //the text in the layer
\r
396 stream.read_short(); //should be 1
\r
398 //transformation info
\r
399 auto xx = stream.read_double();
\r
400 auto xy = stream.read_double();
\r
401 auto yx = stream.read_double();
\r
402 auto yy = stream.read_double();
\r
403 auto tx = stream.read_double(); // tx
\r
404 auto ty = stream.read_double(); // ty
\r
409 if(stream.read_short() != 50) //"text version" should be 50
\r
410 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("invalid text version"));
\r
412 if(stream.read_long() != 16) //"descriptor version" should be 16
\r
413 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Invalid descriptor version while reading text-data"));
\r
415 descriptor text_descriptor(L"text");
\r
416 text_descriptor.populate(stream);
\r
417 auto text_info = text_descriptor.items().get_optional<std::wstring>(L"EngineData");
\r
421 std::string str(text_info->begin(), text_info->end());
\r
422 read_pdf(text_layer_info_, str);
\r
423 log::print_child(boost::log::trivial::trace, L"", L"text_layer_info", text_layer_info_);
\r
426 if(stream.read_short() != 1) //"warp version" should be 1
\r
427 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("invalid warp version"));
\r
429 if(stream.read_long() != 16) //"descriptor version" should be 16
\r
430 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Invalid descriptor version while reading text warp-data"));
\r
432 descriptor warp_descriptor(L"warp");
\r
433 warp_descriptor.populate(stream);
\r
434 stream.read_double(); // w_left
\r
435 stream.read_double(); // w_top
\r
436 stream.read_double(); // w_right
\r
437 stream.read_double(); // w_bottom
\r
440 //extract scale, angle and shear factor from transformation matrix
\r
441 const double PI = 3.141592653589793;
\r
442 auto angle = atan2(xy, xx);
\r
444 auto c = cos(angle);
\r
445 auto s = sin(angle);
\r
446 auto scale_x = (abs(c) > 0.1) ? xx / c : xy / s;
\r
448 if (xx / scale_x < 0) { //fel kvadrant
\r
452 scale_x = (abs(c) > 0.1) ? xx / c : xy / s;
\r
455 auto shear_factor = (yx*c + yy*s) / (yy*c - yx * s);
\r
456 auto scale_y = 1.0;
\r
457 if (abs(shear_factor) < 0.0001 || std::isnan(shear_factor)) {
\r
459 scale_y = (abs(c) > 0.1) ? yy / c : yx / -s;
\r
462 scale_y = yx / (c*shear_factor - s);
\r
465 scale_.x = scale_x;
\r
466 scale_.y = scale_y;
\r
467 angle_ = angle * 180 / PI;
\r
468 shear_ = shear_factor;
\r
476 void read_blending_ranges(bigendian_file_input_stream& stream)
\r
478 auto length = stream.read_long();
\r
479 stream.discard_bytes(length);
\r
482 bool has_channel(channel_type type)
\r
484 return std::find_if(channels_.begin(), channels_.end(), [=](const channel& c) { return c.id == static_cast<int>(type); }) != channels_.end();
\r
487 void read_channel_data(bigendian_file_input_stream& stream)
\r
489 image8bit_ptr bitmap;
\r
491 bool has_transparency = has_channel(channel_type::transparency);
\r
493 if(!bitmap_rect_.empty())
\r
495 bitmap = std::make_shared<image8bit>(bitmap_rect_.size.width, bitmap_rect_.size.height, 4);
\r
496 if(!has_transparency)
\r
497 std::memset(bitmap->data(), 255, bitmap->width()*bitmap->height()*bitmap->channel_count());
\r
500 for(auto it = channels_.begin(); it != channels_.end(); ++it)
\r
502 auto channel = (*it);
\r
503 image8bit_ptr target;
\r
505 bool discard_channel = false;
\r
507 //determine target bitmap and offset
\r
508 if(channel.id >= 3)
\r
509 discard_channel = true; //discard channels that doesn't contribute to the final image
\r
510 else if(channel.id >= -1) //BGRA-data
\r
513 offset = (channel.id >= 0) ? 2 - channel.id : 3;
\r
518 if (channel.id == -2)
\r
520 mask_.create_bitmap();
\r
521 target = mask_.bitmap_;
\r
523 else if (channel.id == -3) //total_mask
\r
525 mask_.total_mask_->create_bitmap();
\r
526 target = mask_.total_mask_->bitmap_;
\r
532 discard_channel = true;
\r
534 auto end_of_data = stream.current_position() + channel.data_length;
\r
535 if(!discard_channel)
\r
537 auto encoding = stream.read_short();
\r
541 read_raw_image_data(stream, channel.data_length-2, target, offset);
\r
542 else if(encoding == 1)
\r
543 read_rle_image_data(stream, target, offset);
\r
545 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Unhandled image data encoding: " + boost::lexical_cast<std::string>(encoding)));
\r
548 stream.set_position(end_of_data);
\r
551 if(bitmap && has_transparency)
\r
553 caspar::image::image_view<caspar::image::bgra_pixel> view(bitmap->data(), bitmap->width(), bitmap->height());
\r
554 caspar::image::premultiply(view);
\r
560 void read_raw_image_data(bigendian_file_input_stream& stream, int data_length, image8bit_ptr target, int offset)
\r
562 auto total_length = target->width() * target->height();
\r
563 if (total_length != data_length)
\r
564 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("total_length != data_length"));
\r
566 auto data = target->data();
\r
567 auto stride = target->channel_count();
\r
570 stream.read(reinterpret_cast<char*>(data + offset), total_length);
\r
573 for(int index = 0; index < total_length; ++index)
\r
574 data[index * stride + offset] = stream.read_byte();
\r
578 void read_rle_image_data(bigendian_file_input_stream& stream, image8bit_ptr target, int offset)
\r
580 auto width = target->width();
\r
581 auto height = target->height();
\r
582 auto stride = target->channel_count();
\r
584 std::vector<int> scanline_lengths;
\r
585 scanline_lengths.reserve(height);
\r
587 for (int scanlineIndex = 0; scanlineIndex < height; ++scanlineIndex)
\r
588 scanline_lengths.push_back(static_cast<std::int16_t>(stream.read_short()));
\r
590 auto target_data = target->data();
\r
592 std::vector<std::uint8_t> line(width);
\r
594 for(int scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
\r
603 char controlByte = static_cast<char>(stream.read_byte());
\r
604 if(controlByte >= 0)
\r
606 //Read uncompressed string
\r
607 length = controlByte+1;
\r
608 for(int index=0; index < length; ++index)
\r
609 line[colIndex+index] = stream.read_byte();
\r
611 else if(controlByte > -128)
\r
614 length = -controlByte+1;
\r
615 auto value = stream.read_byte();
\r
616 for(int index=0; index < length; ++index)
\r
617 line[colIndex+index] = value;
\r
620 colIndex += length;
\r
622 while(colIndex < width);
\r
624 //use line to populate target
\r
625 for(int index = 0; index < width; ++index)
\r
627 target_data[(scanlineIndex*width+index) * stride + offset] = line[index];
\r
633 layer::layer() : impl_(spl::make_shared<impl>()) {}
\r
635 void layer::populate(bigendian_file_input_stream& stream, const psd_document& doc) { impl_->populate(stream, doc); }
\r
636 void layer::read_channel_data(bigendian_file_input_stream& stream) { impl_->read_channel_data(stream); }
\r
638 const std::wstring& layer::name() const { return impl_->name_; }
\r
639 int layer::opacity() const { return impl_->opacity_; }
\r
640 caspar::core::blend_mode layer::blend_mode() const { return impl_->blend_mode_; }
\r
641 int layer::sheet_color() const { return impl_->sheet_color_; }
\r
643 bool layer::is_visible() { return (impl_->flags_ & 2) == 0; } //the (PSD file-format) documentation is is saying the opposite but what the heck
\r
644 bool layer::is_position_protected() { return (impl_->protection_flags_& 4) == 4; }
\r
646 const layer::mask_info& layer::mask() const { return impl_->mask_; }
\r
648 const psd::point<double>& layer::text_pos() const { return impl_->text_pos_; }
\r
649 const psd::point<double>& layer::scale() const { return impl_->scale_; }
\r
650 const double layer::angle() const { return impl_->angle_; }
\r
651 const double layer::shear() const { return impl_->shear_; }
\r
653 bool layer::is_text() const { return !impl_->text_layer_info_.empty(); }
\r
654 const boost::property_tree::wptree& layer::text_data() const { return impl_->text_layer_info_; }
\r
656 bool layer::has_timeline() const { return !impl_->timeline_info_.empty(); }
\r
657 const boost::property_tree::wptree& layer::timeline_data() const { return impl_->timeline_info_; }
\r
659 bool layer::is_solid() const { return impl_->solid_color_.alpha != 0; }
\r
660 color<std::uint8_t> layer::solid_color() const { return impl_->solid_color_; }
\r
662 const point<int>& layer::location() const { return impl_->bitmap_rect_.location; }
\r
663 const size<int>& layer::size() const { return impl_->bitmap_rect_.size; }
\r
664 const image8bit_ptr& layer::bitmap() const { return impl_->bitmap_; }
\r
666 layer_type layer::group_mode() const { return impl_->layer_type_; }
\r
667 int layer::link_group_id() const { return impl_->link_group_id_; }
\r
668 void layer::set_link_group_id(int id) { impl_->link_group_id_ = id; }
\r
670 bool layer::is_explicit_dynamic() const { return (impl_->tags_ & layer_tag::explicit_dynamic) == layer_tag::explicit_dynamic; }
\r
671 bool layer::is_static() const { return (impl_->tags_ & layer_tag::rasterized) == layer_tag::rasterized; }
\r
672 bool layer::is_movable() const { return (impl_->tags_ & layer_tag::moveable) == layer_tag::moveable; }
\r
673 bool layer::is_resizable() const { return (impl_->tags_ & layer_tag::resizable) == layer_tag::resizable; }
\r
674 bool layer::is_placeholder() const { return (impl_->tags_ & layer_tag::placeholder) == layer_tag::placeholder; }
\r
675 bool layer::is_cornerpin() const { return (impl_->tags_ & layer_tag::cornerpin) == layer_tag::cornerpin; }
\r
677 layer_tag layer::tags() const { return impl_->tags_; }
\r
680 } //namespace caspar
\r