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