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
23 #include "psd_document.h"
24 #include "descriptor.h"
25 #include "util/pdf_reader.h"
27 #include "../image/util/image_algorithms.h"
28 #include "../image/util/image_view.h"
30 #include <common/log.h>
32 #include <boost/property_tree/ptree.hpp>
33 #include <boost/lexical_cast.hpp>
37 namespace caspar { namespace psd {
39 void layer::layer_mask_info::read_mask_data(bigendian_file_input_stream& stream)
41 auto length = stream.read_long();
48 case 36: //discard total user mask data
49 rect_.location.y = stream.read_long();
50 rect_.location.x = stream.read_long();
51 rect_.size.height = stream.read_long() - rect_.location.y;
52 rect_.size.width = stream.read_long() - rect_.location.x;
54 default_value_ = stream.read_byte();
55 flags_ = stream.read_byte();
56 stream.discard_bytes(2);
60 stream.discard_bytes(18); //discard "total user mask" data
61 //flags_ = stream.read_byte();
62 //default_value_ = stream.read_byte();
63 //rect_.location.y = stream.read_long();
64 //rect_.location.x = stream.read_long();
65 //rect_.size.height = stream.read_long() - rect_.location.y;
66 //rect_.size.width = stream.read_long() - rect_.location.x;
71 //TODO: Log that we discard a mask that is not supported
72 stream.discard_bytes(length);
81 impl() : blend_mode_(blend_mode::InvalidBlendMode), link_group_id_(0), opacity_(255), sheet_color_(0), baseClipping_(false), flags_(0), protection_flags_(0), masks_count_(0), text_scale_(1.0f)
85 std::vector<channel> channels_;
86 blend_mode blend_mode_;
92 std::uint32_t protection_flags_;
97 rect<int> vector_mask_;
98 layer::layer_mask_info mask_;
100 rect<int> bitmap_rect_;
101 image8bit_ptr bitmap_;
103 boost::property_tree::wptree text_layer_info_;
104 boost::property_tree::wptree timeline_info_;
106 color<std::uint8_t> solid_color_;
109 void populate(bigendian_file_input_stream& stream, const psd_document& doc)
111 bitmap_rect_.location.y = stream.read_long();
112 bitmap_rect_.location.x = stream.read_long();
113 bitmap_rect_.size.height = stream.read_long() - bitmap_rect_.location.y;
114 bitmap_rect_.size.width = stream.read_long() - bitmap_rect_.location.x;
116 //Get info about the channels in the layer
117 auto channelCount = stream.read_short();
118 for(int channelIndex = 0; channelIndex < channelCount; ++channelIndex)
120 auto id = static_cast<std::int16_t>(stream.read_short());
121 channel c(id, stream.read_long());
126 channels_.push_back(c);
129 auto blendModeSignature = stream.read_long();
130 if(blendModeSignature != '8BIM')
131 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("blendModeSignature != '8BIM'"));
133 blend_mode_ = int_to_blend_mode(stream.read_long());
134 opacity_ = stream.read_byte();
135 baseClipping_ = stream.read_byte() == 1 ? false : true;
136 flags_ = stream.read_byte();
138 stream.discard_bytes(1); //padding
140 auto extras_size = stream.read_long();
141 auto position = stream.current_position();
142 mask_.read_mask_data(stream);
143 read_blending_ranges(stream);
144 name_ = stream.read_pascal_string(4);
146 //Aditional Layer Information
147 auto end_of_layer_info = position + extras_size;
150 while(stream.current_position() < end_of_layer_info)
152 read_chunk(stream, doc);
155 catch(psd_file_format_exception&)
157 stream.set_position(end_of_layer_info);
161 void read_chunk(bigendian_file_input_stream& stream, const psd_document& doc, bool isMetadata = false)
163 auto signature = stream.read_long();
164 if(signature != '8BIM' && signature != '8B64')
165 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("signature != '8BIM' && signature != '8B64'"));
167 auto key = stream.read_long();
169 if(isMetadata) stream.read_long();
171 auto length = stream.read_long();
172 auto end_of_chunk = stream.current_position() + length;
179 read_solid_color(stream);
182 case 'lspf': //protection settings
183 protection_flags_ = stream.read_long();
186 case 'Txt2': //text engine data
190 name_ = stream.read_unicode_string();
193 case 'TySh': //type tool object settings
194 read_text_data(stream);
197 case 'shmd': //metadata
198 read_metadata(stream, doc);
202 sheet_color_ = static_cast<std::int16_t>(stream.read_short());
205 case 'lyvr': //layer version
208 case 'lnkD': //linked layer
209 case 'lnk2': //linked layer
210 case 'lnk3': //linked layer
214 read_vector_mask(length, stream, doc.width(), doc.height());
218 read_timeline_data(stream);
225 catch(psd_file_format_exception& ex)
227 //ignore failed chunks silently
228 CASPAR_LOG(warning) << ex.what();
231 stream.set_position(end_of_chunk);
234 void read_solid_color(bigendian_file_input_stream& stream)
236 if(stream.read_long() != 16) //"descriptor version" should be 16
237 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor version should be 16"));
239 descriptor solid_descriptor(L"solid_color");
240 solid_descriptor.populate(stream);
241 solid_color_.red = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Rd ", 0.0) + 0.5);
242 solid_color_.green = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Grn ", 0.0) + 0.5);
243 solid_color_.blue = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Bl ", 0.0) + 0.5);
244 solid_color_.alpha = 255;
247 void read_vector_mask(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height)
249 typedef std::pair<int, int> path_point;
251 stream.read_long(); // version
252 stream.read_long(); // flags
253 int path_records = (length-8) / 26;
255 auto position = stream.current_position();
257 std::vector<path_point> knots;
259 const int SELECTOR_SIZE = 2;
260 const int PATH_POINT_SIZE = 4 + 4;
261 const int PATH_POINT_RECORD_SIZE = SELECTOR_SIZE + (3 * PATH_POINT_SIZE);
263 for (int i = 1; i <= path_records; ++i)
265 auto selector = stream.read_short();
266 if(selector == 2) //we only concern ourselves with closed paths
268 auto p_y = stream.read_long();
269 auto p_x = stream.read_long();
270 path_point cp_prev(p_x, p_y);
272 auto a_y = stream.read_long();
273 auto a_x = stream.read_long();
274 path_point anchor(a_x, a_y);
276 auto n_y = stream.read_long();
277 auto n_x = stream.read_long();
278 path_point cp_next(n_x, n_y);
280 if(anchor == cp_prev && anchor == cp_next)
281 knots.push_back(anchor);
283 { //we can't handle smooth curves yet
284 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("we can't handle smooth curves yet"));
288 auto offset = PATH_POINT_RECORD_SIZE * i;
289 stream.set_position(position + offset);
292 if(knots.size() != 4) //we only support rectangular vector masks
293 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("we only support rectangular vector masks"));
295 //we only support rectangular vector masks
296 if(!(knots[0].first == knots[3].first && knots[1].first == knots[2].first && knots[0].second == knots[1].second && knots[2].second == knots[3].second))
297 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("we only support rectangular vector masks"));
299 //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.
300 float x_ratio = doc_width / 16777215.0f;
301 float y_ratio = doc_height / 16777215.0f;
302 vector_mask_.location.x = static_cast<int>(knots[0].first * x_ratio +0.5f); //add .5 to get propper rounding when converting to integer
303 vector_mask_.location.y = static_cast<int>(knots[0].second * y_ratio +0.5f); //add .5 to get propper rounding when converting to integer
304 vector_mask_.size.width = static_cast<int>(knots[1].first * x_ratio +0.5f) - vector_mask_.location.x; //add .5 to get propper rounding when converting to integer
305 vector_mask_.size.height = static_cast<int>(knots[2].second * y_ratio +0.5f) - vector_mask_.location.y; //add .5 to get propper rounding when converting to integer
308 void read_metadata(bigendian_file_input_stream& stream, const psd_document& doc)
310 int count = stream.read_long();
311 for(int index = 0; index < count; ++index)
312 read_chunk(stream, doc, true);
315 void read_timeline_data(bigendian_file_input_stream& stream)
317 if(stream.read_long() != 16) //"descriptor version" should be 16
318 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor version should be 16"));
320 descriptor timeline_descriptor(L"timeline");
321 timeline_descriptor.populate(stream);
322 timeline_info_.swap(timeline_descriptor.items());
325 void read_text_data(bigendian_file_input_stream& stream)
327 std::wstring text; //the text in the layer
329 stream.read_short(); //should be 1
331 //transformation info
332 auto xx = stream.read_double();
333 auto xy = stream.read_double();
334 auto yx = stream.read_double();
335 auto yy = stream.read_double();
336 stream.read_double(); // tx
337 stream.read_double(); // ty
338 if(xx != yy || (xy != 0 && yx != 0))
339 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Rotation and non-uniform scaling of dynamic textfields is not supported yet"));
343 if(stream.read_short() != 50) //"text version" should be 50
344 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("invalid text version"));
346 if(stream.read_long() != 16) //"descriptor version" should be 16
347 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Invalid descriptor version while reading text-data"));
349 descriptor text_descriptor(L"text");
350 text_descriptor.populate(stream);
351 auto text_info = text_descriptor.items().get_optional<std::wstring>(L"EngineData");
355 std::string str(text_info->begin(), text_info->end());
356 read_pdf(text_layer_info_, str);
357 log::print_child(boost::log::trivial::trace, L"", L"text_layer_info", text_layer_info_);
360 if(stream.read_short() != 1) //"warp version" should be 1
361 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("invalid warp version"));
363 if(stream.read_long() != 16) //"descriptor version" should be 16
364 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Invalid descriptor version while reading text warp-data"));
366 descriptor warp_descriptor(L"warp");
367 warp_descriptor.populate(stream);
368 stream.read_double(); // w_top
369 stream.read_double(); // w_left
370 stream.read_double(); // w_right
371 stream.read_double(); // w_bottom
375 void read_blending_ranges(bigendian_file_input_stream& stream)
377 auto length = stream.read_long();
378 stream.discard_bytes(length);
381 bool has_channel(channel_type type)
383 return std::find_if(channels_.begin(), channels_.end(), [=](const channel& c) { return c.id == static_cast<int>(type); }) != channels_.end();
386 void read_channel_data(bigendian_file_input_stream& stream)
388 image8bit_ptr bitmap;
391 bool has_transparency = has_channel(channel_type::transparency);
394 if(!bitmap_rect_.empty())
396 clip_rect = bitmap_rect_;
398 if(!vector_mask_.empty())
399 clip_rect = vector_mask_;
401 bitmap = std::make_shared<image8bit>(clip_rect.size.width, clip_rect.size.height, 4);
403 if(!has_transparency)
404 std::memset(bitmap->data(), 255, bitmap->width()*bitmap->height()*bitmap->channel_count());
407 if(masks_count_ > 0 && !mask_.rect_.empty())
409 mask = std::make_shared<image8bit>(mask_.rect_.size.width, mask_.rect_.size.height, 1);
412 for(auto it = channels_.begin(); it != channels_.end(); ++it)
414 psd::rect<int> src_rect;
415 image8bit_ptr target;
417 bool discard_channel = false;
419 //determine target bitmap and offset
421 discard_channel = true; //discard channels that doesn't contribute to the final image
422 else if((*it).id >= -1) //BGRA-data
425 offset = ((*it).id >= 0) ? 2 - (*it).id : 3;
426 src_rect = bitmap_rect_;
430 if((*it).id == -3) //discard the "total user mask"
431 discard_channel = true;
436 src_rect = mask_.rect_;
440 if(!target || src_rect.empty())
441 discard_channel = true;
443 auto end_of_data = stream.current_position() + (*it).data_length;
446 auto encoding = stream.read_short();
450 read_raw_image_data(stream, (*it).data_length-2, target, offset);
451 else if(encoding == 1)
452 read_rle_image_data(stream, src_rect, (target == bitmap) ? clip_rect : mask_.rect_, target, offset);
454 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Unhandled image data encoding: " + boost::lexical_cast<std::string>(encoding)));
457 stream.set_position(end_of_data);
460 if(bitmap && has_transparency)
462 caspar::image::image_view<caspar::image::bgra_pixel> view(bitmap->data(), bitmap->width(), bitmap->height());
463 caspar::image::premultiply(view);
467 bitmap_rect_ = clip_rect;
468 mask_.bitmap_ = mask;
471 void read_raw_image_data(bigendian_file_input_stream& stream, int data_length, image8bit_ptr target, int offset)
473 auto total_length = target->width() * target->height();
474 if (total_length != data_length)
475 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("total_length != data_length"));
477 auto data = target->data();
478 auto stride = target->channel_count();
481 stream.read(reinterpret_cast<char*>(data + offset), total_length);
484 for(int index = 0; index < total_length; ++index)
485 data[index * stride + offset] = stream.read_byte();
489 void read_rle_image_data(bigendian_file_input_stream& stream, const rect<int>&src_rect, const rect<int>&clip_rect, image8bit_ptr target, int offset)
491 auto width = src_rect.size.width;
492 auto height = src_rect.size.height;
493 auto stride = target->channel_count();
495 int offset_x = clip_rect.location.x - src_rect.location.x;
496 int offset_y = clip_rect.location.y - src_rect.location.y;
498 std::vector<int> scanline_lengths;
499 scanline_lengths.reserve(height);
501 for (int scanlineIndex = 0; scanlineIndex < height; ++scanlineIndex)
502 scanline_lengths.push_back(static_cast<std::int16_t>(stream.read_short()));
504 auto target_data = target->data();
506 std::vector<std::uint8_t> line(width);
508 for(int scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
510 if(scanlineIndex >= target->height()+offset_y)
520 char controlByte = static_cast<char>(stream.read_byte());
523 //Read uncompressed string
524 length = controlByte+1;
525 for(int index=0; index < length; ++index)
526 line[colIndex+index] = stream.read_byte();
528 else if(controlByte > -128)
531 length = -controlByte+1;
532 auto value = stream.read_byte();
533 for(int index=0; index < length; ++index)
534 line[colIndex+index] = value;
539 while(colIndex < width);
541 //use line to populate target
542 if(scanlineIndex >= offset_y)
543 for(int index = offset_x; index < target->width(); ++index)
545 target_data[((scanlineIndex-offset_y)*target->width()+index-offset_x) * stride + offset] = line[index];
551 layer::layer() : impl_(spl::make_shared<impl>()) {}
553 void layer::populate(bigendian_file_input_stream& stream, const psd_document& doc) { impl_->populate(stream, doc); }
554 void layer::read_channel_data(bigendian_file_input_stream& stream) { impl_->read_channel_data(stream); }
556 const std::wstring& layer::name() const { return impl_->name_; }
557 int layer::opacity() const { return impl_->opacity_; }
558 int layer::sheet_color() const { return impl_->sheet_color_; }
560 bool layer::is_visible() { return (impl_->flags_ & 2) == 0; } //the (PSD file-format) documentation is is saying the opposite but what the heck
561 bool layer::is_position_protected() { return (impl_->protection_flags_& 4) == 4; }
563 double layer::text_scale() const { return impl_->text_scale_; }
564 bool layer::is_text() const { return !impl_->text_layer_info_.empty(); }
565 const boost::property_tree::wptree& layer::text_data() const { return impl_->text_layer_info_; }
567 bool layer::has_timeline() const { return !impl_->timeline_info_.empty(); }
568 const boost::property_tree::wptree& layer::timeline_data() const { return impl_->timeline_info_; }
570 bool layer::is_solid() const { return impl_->solid_color_.alpha != 0; }
571 color<std::uint8_t> layer::solid_color() const { return impl_->solid_color_; }
573 const point<int>& layer::location() const { return impl_->bitmap_rect_.location; }
574 const image8bit_ptr& layer::bitmap() const { return impl_->bitmap_; }
576 int layer::link_group_id() const { return impl_->link_group_id_; }
577 void layer::set_link_group_id(int id) { impl_->link_group_id_ = id; }