]> git.sesse.net Git - casparcg/blob - modules/psd/layer.cpp
* don't throw on not finding a font file, just ignore that font
[casparcg] / modules / psd / layer.cpp
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 #include "layer.h"
23 #include "psd_document.h"
24 #include "descriptor.h"
25 #include "util/pdf_reader.h"
26
27 #include "../image/util/image_algorithms.h"
28 #include "../image/util/image_view.h"
29
30 #include <common/log.h>
31
32 #include <boost/property_tree/ptree.hpp>
33 #include <boost/lexical_cast.hpp>
34
35 #include <algorithm>
36
37 namespace caspar { namespace psd {
38
39 void layer::mask_info::read_mask_data(bigendian_file_input_stream& stream)
40 {
41         auto length = stream.read_long();
42         switch(length)
43         {
44         case 0:
45                 break;
46
47         case 20:
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;
53
54                 default_value_ = stream.read_byte();
55                 flags_ = stream.read_byte();
56                 stream.discard_bytes(2);
57                 
58                 if(length == 36)
59                 {
60                         //we save the information about the total mask in case the vector-mask has an unsupported shape
61                         total_mask_.reset(new layer::mask_info);
62                         total_mask_->flags_ = stream.read_byte();
63                         total_mask_->default_value_ = stream.read_byte();
64                         total_mask_->rect_.location.y = stream.read_long();
65                         total_mask_->rect_.location.x = stream.read_long();
66                         total_mask_->rect_.size.height = stream.read_long() - rect_.location.y;
67                         total_mask_->rect_.size.width = stream.read_long() - rect_.location.x;
68                 }
69                 break;
70
71         default:
72                 //TODO: Log that we discard a mask that is not supported
73                 stream.discard_bytes(length);
74                 break;
75         };
76 }
77
78 bool layer::vector_mask_info::populate(int length, bigendian_file_input_stream& stream, int doc_width, int doc_height)
79 {
80         typedef std::pair<int, int> path_point;
81         bool bFail = false;
82
83         stream.read_long(); // version
84         this->flags_ = static_cast<std::uint8_t>(stream.read_long()); // flags
85         int path_records = (length - 8) / 26;
86
87         auto position = stream.current_position();
88
89         std::vector<path_point> knots;
90
91         const int SELECTOR_SIZE = 2;
92         const int PATH_POINT_SIZE = 4 + 4;
93         const int PATH_POINT_RECORD_SIZE = SELECTOR_SIZE + (3 * PATH_POINT_SIZE);
94
95         for (int i = 1; i <= path_records; ++i)
96         {
97                 auto selector = stream.read_short();
98                 if (selector == 2)      //we only concern ourselves with closed paths 
99                 {
100                         auto p_y = stream.read_long();
101                         auto p_x = stream.read_long();
102                         path_point cp_prev(p_x, p_y);
103
104                         auto a_y = stream.read_long();
105                         auto a_x = stream.read_long();
106                         path_point anchor(a_x, a_y);
107
108                         auto n_y = stream.read_long();
109                         auto n_x = stream.read_long();
110                         path_point cp_next(n_x, n_y);
111
112                         if (anchor == cp_prev && anchor == cp_next)
113                                 knots.push_back(anchor);
114                         else
115                         {       //we can't handle smooth curves yet
116                                 bFail = true;
117                         }
118                 }
119
120                 auto offset = PATH_POINT_RECORD_SIZE * i;
121                 stream.set_position(position + offset);
122         }
123
124         if ((knots.size() != 4) || (!(knots[0].first == knots[3].first && knots[1].first == knots[2].first && knots[0].second == knots[1].second && knots[2].second == knots[3].second)))
125                 bFail = true;
126
127         if (bFail) {
128                 rect_.clear();
129                 flags_ = static_cast<std::uint8_t>(flags::unsupported);
130         }
131         else 
132         {
133                 //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.
134                 float x_ratio = doc_width / 16777215.0f;
135                 float y_ratio = doc_height / 16777215.0f;
136                 rect_.location.x = static_cast<int>(knots[0].first * x_ratio + 0.5f);                                                           //add .5 to get propper rounding when converting to integer
137                 rect_.location.y = static_cast<int>(knots[0].second * y_ratio + 0.5f);                                                          //add .5 to get propper rounding when converting to integer
138                 rect_.size.width = static_cast<int>(knots[1].first * x_ratio + 0.5f) - rect_.location.x;                //add .5 to get propper rounding when converting to integer
139                 rect_.size.height = static_cast<int>(knots[2].second * y_ratio + 0.5f) - rect_.location.y;      //add .5 to get propper rounding when converting to integer
140         }
141
142         return !bFail;
143 }
144
145 struct layer::impl
146 {
147         friend class layer;
148
149         impl() : blend_mode_(blend_mode::InvalidBlendMode), layer_type_(layer_type::content), link_group_id_(0), opacity_(255), sheet_color_(0), baseClipping_(false), flags_(0), protection_flags_(0), masks_count_(0), text_scale_(1.0f)
150         {}
151
152 private:
153         std::vector<channel>                    channels_;
154         blend_mode                                              blend_mode_;
155         layer_type                                              layer_type_;
156         int                                                             link_group_id_;
157         int                                                             opacity_;
158         int                                                             sheet_color_;
159         bool                                                    baseClipping_;
160         std::uint8_t                                    flags_;
161         std::uint32_t                                   protection_flags_;
162         std::wstring                                    name_;
163         int                                                             masks_count_;
164         double                                                  text_scale_;
165
166         layer::mask_info                                mask_;
167
168         rect<int>                                               bitmap_rect_;
169         image8bit_ptr                                   bitmap_;
170
171         boost::property_tree::wptree    text_layer_info_;
172         boost::property_tree::wptree    timeline_info_;
173
174         color<std::uint8_t>                             solid_color_;
175
176 public:
177         void populate(bigendian_file_input_stream& stream, const psd_document& doc)
178         {
179                 bitmap_rect_.location.y = stream.read_long();
180                 bitmap_rect_.location.x = stream.read_long();
181                 bitmap_rect_.size.height = stream.read_long() - bitmap_rect_.location.y;
182                 bitmap_rect_.size.width = stream.read_long() - bitmap_rect_.location.x;
183
184                 //Get info about the channels in the layer
185                 auto channelCount = stream.read_short();
186                 for(int channelIndex = 0; channelIndex < channelCount; ++channelIndex)
187                 {
188                         auto id = static_cast<std::int16_t>(stream.read_short());
189                         channel c(id, stream.read_long());
190
191                         if(c.id < -1)
192                                 masks_count_++;
193
194                         channels_.push_back(c);
195                 }
196
197                 auto blendModeSignature = stream.read_long();
198                 if(blendModeSignature != '8BIM')
199                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("blendModeSignature != '8BIM'"));
200
201                 blend_mode_ = int_to_blend_mode(stream.read_long());
202                 opacity_ = stream.read_byte();
203                 baseClipping_ = stream.read_byte() == 1 ? false : true;
204                 flags_ = stream.read_byte();
205
206                 stream.discard_bytes(1);        //padding
207
208                 auto extras_size = stream.read_long();
209                 auto position = stream.current_position();
210                 mask_.read_mask_data(stream);
211                 read_blending_ranges(stream);
212                 name_ = stream.read_pascal_string(4);
213
214                 //Aditional Layer Information
215                 auto end_of_layer_info = position + extras_size;
216                 try
217                 {
218                         while(stream.current_position() < end_of_layer_info)
219                         {
220                                 read_chunk(stream, doc);
221                         }
222                 }
223                 catch(psd_file_format_exception&)
224                 {
225                         stream.set_position(end_of_layer_info);
226                 }
227         }
228
229         void read_chunk(bigendian_file_input_stream& stream, const psd_document& doc, bool isMetadata = false)
230         {
231                 auto signature = stream.read_long();
232                 if(signature != '8BIM' && signature != '8B64')
233                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("signature != '8BIM' && signature != '8B64'"));
234
235                 auto key = stream.read_long();
236
237                 if(isMetadata) stream.read_long();
238
239                 auto length = stream.read_long();
240                 auto end_of_chunk = stream.current_position() + length;
241
242                 try
243                 {
244                         switch(key)
245                         {
246                         case 'SoCo':
247                                 read_solid_color(stream);
248                                 break;
249
250                         case 'lsct':    //group settings (folders)
251                                 read_group_settings(stream, length);
252
253                         case 'lspf':    //protection settings
254                                 protection_flags_ = stream.read_long();
255                                 break;
256
257                         case 'Txt2':    //text engine data
258                                 break;
259
260                         case 'luni':
261                                 name_ = stream.read_unicode_string();
262                                 break;
263
264                         case 'TySh':    //type tool object settings
265                                 read_text_data(stream);
266                                 break;
267                                 
268                         case 'shmd':    //metadata
269                                 read_metadata(stream, doc);
270                                 break;
271
272                         case 'lclr':
273                                 sheet_color_ = static_cast<std::int16_t>(stream.read_short());
274                                 break;
275                                 
276                         case 'lyvr':    //layer version
277                                 break;
278
279                         case 'lnkD':    //linked layer
280                         case 'lnk2':    //linked layer
281                         case 'lnk3':    //linked layer
282                                 break;
283
284                         case 'vmsk':
285                                 mask_.read_vector_mask_data(length, stream, doc.width(), doc.height());
286                                 break;
287                                 
288                         case 'tmln':
289                                 read_timeline_data(stream);
290                                 break;
291
292                         default:
293                                 break;
294                         }
295                 }
296                 catch(psd_file_format_exception& ex)
297                 {
298                         //ignore failed chunks silently
299                         CASPAR_LOG(warning) << ex.what();
300                 }
301
302                 stream.set_position(end_of_chunk);
303         }
304
305         void read_solid_color(bigendian_file_input_stream& stream)
306         {
307                 if(stream.read_long() != 16)    //"descriptor version" should be 16
308                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor version should be 16"));
309
310                 descriptor solid_descriptor(L"solid_color");
311                 solid_descriptor.populate(stream);
312                 solid_color_.red = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Rd  ", 0.0) + 0.5);
313                 solid_color_.green = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Grn ", 0.0) + 0.5);
314                 solid_color_.blue = static_cast<std::uint8_t>(solid_descriptor.items().get(L"Clr .Bl  ", 0.0) + 0.5);
315                 solid_color_.alpha = 255;
316         }
317
318         void read_group_settings(bigendian_file_input_stream& stream, unsigned int length) 
319         {
320                 auto type = stream.read_long();
321                 unsigned int sub_type = 0;
322
323                 if (length >= 12)
324                 {
325                         auto signature = stream.read_long();
326                         if (signature != '8BIM')
327                                 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("signature != '8BIM'"));
328
329                         blend_mode_ = int_to_blend_mode(stream.read_long());
330                         
331                         if (length >= 16)
332                                 sub_type = stream.read_long();
333                 }
334
335                 layer_type_ = int_to_layer_type(type, sub_type);
336         }
337
338         void read_metadata(bigendian_file_input_stream& stream, const psd_document& doc)
339         {
340                 int count = stream.read_long();
341                 for(int index = 0; index < count; ++index)
342                         read_chunk(stream, doc, true);
343         }
344
345         void read_timeline_data(bigendian_file_input_stream& stream)
346         {
347                 if(stream.read_long() != 16)    //"descriptor version" should be 16
348                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor version should be 16"));
349
350                 descriptor timeline_descriptor(L"timeline");
351                 timeline_descriptor.populate(stream);
352                 timeline_info_.swap(timeline_descriptor.items());
353         }
354
355         void read_text_data(bigendian_file_input_stream& stream)
356         {
357                 std::wstring text;      //the text in the layer
358
359                 stream.read_short();    //should be 1
360         
361                 //transformation info
362                 auto xx = stream.read_double();
363                 auto xy = stream.read_double();
364                 auto yx = stream.read_double();
365                 auto yy = stream.read_double();
366                 stream.read_double(); // tx
367                 stream.read_double(); // ty
368                 if(xx != yy || (xy != 0 && yx != 0))
369                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Rotation and non-uniform scaling of dynamic textfields is not supported yet"));
370
371                 text_scale_ = xx;
372
373                 if(stream.read_short() != 50)   //"text version" should be 50
374                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("invalid text version"));
375
376                 if(stream.read_long() != 16)    //"descriptor version" should be 16
377                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Invalid descriptor version while reading text-data"));
378
379                 descriptor text_descriptor(L"text");
380                 text_descriptor.populate(stream);
381                 auto text_info = text_descriptor.items().get_optional<std::wstring>(L"EngineData");
382                 
383                 if (text_info)
384                 {
385                         std::string str(text_info->begin(), text_info->end());
386                         read_pdf(text_layer_info_, str);
387                         log::print_child(boost::log::trivial::trace, L"", L"text_layer_info", text_layer_info_);
388                 }
389
390                 if(stream.read_short() != 1)    //"warp version" should be 1
391                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("invalid warp version"));
392
393                 if(stream.read_long() != 16)    //"descriptor version" should be 16
394                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Invalid descriptor version while reading text warp-data"));
395
396                 descriptor warp_descriptor(L"warp");
397                 warp_descriptor.populate(stream);
398                 stream.read_double(); // w_top
399                 stream.read_double();  // w_left
400                 stream.read_double();  // w_right
401                 stream.read_double();  // w_bottom
402         }
403
404         //TODO: implement
405         void read_blending_ranges(bigendian_file_input_stream& stream)
406         {
407                 auto length = stream.read_long();
408                 stream.discard_bytes(length);
409         }
410
411         bool has_channel(channel_type type)
412         {
413                 return std::find_if(channels_.begin(), channels_.end(), [=](const channel& c) { return c.id == static_cast<int>(type); }) != channels_.end();
414         }
415
416         void read_channel_data(bigendian_file_input_stream& stream)
417         {
418                 image8bit_ptr bitmap;
419
420                 bool has_transparency = has_channel(channel_type::transparency);
421         
422                 if(!bitmap_rect_.empty())
423                 {
424                         bitmap = std::make_shared<image8bit>(bitmap_rect_.size.width, bitmap_rect_.size.height, 4);
425                         if(!has_transparency)
426                                 std::memset(bitmap->data(), 255, bitmap->width()*bitmap->height()*bitmap->channel_count());
427                 }
428
429                 for(auto it = channels_.begin(); it != channels_.end(); ++it)
430                 {
431                         auto channel = (*it);
432                         image8bit_ptr target;
433                         int offset = 0;
434                         bool discard_channel = false;
435
436                         //determine target bitmap and offset
437                         if(channel.id >= 3)
438                                 discard_channel = true; //discard channels that doesn't contribute to the final image
439                         else if(channel.id >= -1)       //BGRA-data
440                         {
441                                 target = bitmap;
442                                 offset = (channel.id >= 0) ? 2 - channel.id : 3;
443                         }
444                         else    //mask
445                         {
446                                 offset = 0;
447                                 if (channel.id == -2)
448                                 {
449                                         mask_.create_bitmap();
450                                         target = mask_.bitmap_;
451                                 }
452                                 else if (channel.id == -3)      //total_mask
453                                 {
454                                         mask_.total_mask_->create_bitmap();
455                                         target = mask_.total_mask_->bitmap_;
456                                         offset = 0;
457                                 }
458                         }
459
460                         if(!target)
461                                 discard_channel = true;
462
463                         auto end_of_data = stream.current_position() + channel.data_length;
464                         if(!discard_channel)
465                         {
466                                 auto encoding = stream.read_short();
467                                 if(target)
468                                 {
469                                         if(encoding == 0)
470                                                 read_raw_image_data(stream, channel.data_length-2, target, offset);
471                                         else if(encoding == 1)
472                                                 read_rle_image_data(stream, target, offset);
473                                         else
474                                                 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("Unhandled image data encoding: " + boost::lexical_cast<std::string>(encoding)));
475                                 }
476                         }
477                         stream.set_position(end_of_data);
478                 }
479
480                 if(bitmap && has_transparency)
481                 {
482                         caspar::image::image_view<caspar::image::bgra_pixel> view(bitmap->data(), bitmap->width(), bitmap->height());
483                         caspar::image::premultiply(view);
484                 }
485
486                 bitmap_ = bitmap;
487         }
488
489         void read_raw_image_data(bigendian_file_input_stream& stream, int data_length, image8bit_ptr target, int offset)
490         {
491                 auto total_length = target->width() * target->height();
492                 if (total_length != data_length)
493                         CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("total_length != data_length"));
494
495                 auto data = target->data();
496                 auto stride = target->channel_count();
497
498                 if (stride == 1)
499                         stream.read(reinterpret_cast<char*>(data + offset), total_length);
500                 else
501                 {
502                         for(int index = 0; index < total_length; ++index)
503                                 data[index * stride + offset] = stream.read_byte();
504                 }
505         }
506
507         void read_rle_image_data(bigendian_file_input_stream& stream, image8bit_ptr target, int offset)
508         {
509                 auto width = target->width();
510                 auto height = target->height();
511                 auto stride = target->channel_count();
512
513                 std::vector<int> scanline_lengths;
514                 scanline_lengths.reserve(height);
515
516                 for (int scanlineIndex = 0; scanlineIndex < height; ++scanlineIndex)
517                         scanline_lengths.push_back(static_cast<std::int16_t>(stream.read_short()));
518
519                 auto target_data = target->data();
520
521                 std::vector<std::uint8_t> line(width);
522
523                 for(int scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
524                 {
525                         int colIndex = 0;
526
527                         do
528                         {
529                                 int length = 0;
530
531                                 //Get controlbyte
532                                 char controlByte = static_cast<char>(stream.read_byte());
533                                 if(controlByte >= 0)
534                                 {
535                                         //Read uncompressed string
536                                         length = controlByte+1;
537                                         for(int index=0; index < length; ++index)
538                                                 line[colIndex+index] = stream.read_byte();
539                                 }
540                                 else if(controlByte > -128)
541                                 {
542                                         //Repeat next byte
543                                         length = -controlByte+1;
544                                         auto value = stream.read_byte();
545                                         for(int index=0; index < length; ++index)
546                                                 line[colIndex+index] = value;
547                                 }
548
549                                 colIndex += length;
550                         }
551                         while(colIndex < width);
552
553                         //use line to populate target
554                         for(int index = 0; index < width; ++index)
555                         {
556                                 target_data[(scanlineIndex*width+index) * stride + offset] = line[index];
557                         }
558                 }
559         }
560 };
561
562 layer::layer() : impl_(spl::make_shared<impl>()) {}
563
564 void layer::populate(bigendian_file_input_stream& stream, const psd_document& doc) { impl_->populate(stream, doc); }
565 void layer::read_channel_data(bigendian_file_input_stream& stream) { impl_->read_channel_data(stream); }
566
567 const std::wstring& layer::name() const { return impl_->name_; }
568 int layer::opacity() const { return impl_->opacity_; }
569 int layer::sheet_color() const { return impl_->sheet_color_; }
570
571 bool layer::is_visible() { return (impl_->flags_ & 2) == 0; }   //the (PSD file-format) documentation is is saying the opposite but what the heck
572 bool layer::is_position_protected() { return (impl_->protection_flags_& 4) == 4; }
573
574 double layer::text_scale() const { return impl_->text_scale_; }
575 bool layer::is_text() const { return !impl_->text_layer_info_.empty(); }
576 const boost::property_tree::wptree& layer::text_data() const { return impl_->text_layer_info_; }
577
578 bool layer::has_timeline() const { return !impl_->timeline_info_.empty(); }
579 const boost::property_tree::wptree& layer::timeline_data() const { return impl_->timeline_info_; }
580
581 bool layer::is_solid() const { return impl_->solid_color_.alpha != 0; }
582 color<std::uint8_t> layer::solid_color() const { return impl_->solid_color_; }
583
584 const point<int>& layer::location() const { return impl_->bitmap_rect_.location; }
585 const size<int>& layer::size() const { return impl_->bitmap_rect_.size; }
586 const image8bit_ptr& layer::bitmap() const { return impl_->bitmap_; }
587
588 layer_type layer::group_mode() const { return impl_->layer_type_; }
589 int layer::link_group_id() const { return impl_->link_group_id_; }
590 void layer::set_link_group_id(int id) { impl_->link_group_id_ = id; }
591
592 }       //namespace psd
593 }       //namespace caspar