]> git.sesse.net Git - casparcg/blob - modules/psd/layer.cpp
added a pdf-parser for the psd-import
[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 "descriptor.h"
24 //#include <iostream>
25
26 typedef unsigned char uint8_t;
27 #include <algorithm>
28 #include "../image/util/image_algorithms.h"
29 #include "../image/util/image_view.h"
30
31 namespace caspar { namespace psd {
32
33 void read_raw_image_data(BEFileInputStream& stream, const channel_ptr& channel, image8bit_ptr target, unsigned char offset);
34 void read_rle_image_data(BEFileInputStream& stream, const channel_ptr& channel, image8bit_ptr target, unsigned char offset);
35
36 layer_ptr layer::create(BEFileInputStream& stream)
37 {
38         layer_ptr result(std::make_shared<layer>());
39         result->rect_.top = stream.read_long();
40         result->rect_.left = stream.read_long();
41         result->rect_.bottom = stream.read_long();
42         result->rect_.right = stream.read_long();
43
44         //Get info about the channels in the layer
45         unsigned short channelCount = stream.read_short();
46         for(int channelIndex = 0; channelIndex < channelCount; ++channelIndex)
47         {
48                 short channel_id = static_cast<short>(stream.read_short());
49                 unsigned long data_length = stream.read_long();
50
51                 if(channel_id <= -2)
52                         result->masks_++;
53
54                 channel_ptr channel(std::make_shared<Channel>(channel_id, data_length));
55                 result->channels_.push_back(channel);
56         }
57
58         unsigned long blendModeSignature = stream.read_long();
59         if(blendModeSignature != '8BIM')
60                 throw PSDFileFormatException();
61
62         unsigned long blendModeKey = stream.read_long();
63         result->blend_mode_ = int_to_blend_mode(blendModeKey);
64
65         result->opacity_ = stream.read_byte();
66         result->baseClipping_ = stream.read_byte() == 1 ? false : true;
67         result->flags_ = stream.read_byte();
68
69         stream.discard_bytes(1);
70
71         unsigned long extraDataSize = stream.read_long();
72         long position1 = stream.current_position();
73         result->mask_.read_mask_data(stream);
74         result->read_blending_ranges(stream);
75
76         result->name_ = stream.read_pascal_string(4);
77
78         //Aditional Layer Information
79         unsigned long end_of_layer_info = position1 + extraDataSize;
80         
81         try
82         {
83                 while(stream.current_position() < end_of_layer_info)
84                 {
85                         unsigned long signature = stream.read_long();
86                         if(signature != '8BIM' && signature != '8B64')
87                                 throw PSDFileFormatException();
88
89                         unsigned long key = stream.read_long();
90                         unsigned long length = stream.read_long();
91                         unsigned long end_of_chunk = stream.current_position() + length;
92
93                         if(key == 'TySh')       //type tool object settings
94                         {
95                                 std::wstring text;      //the text in the layer
96
97                                 stream.read_short();    //should be 1
98                                 stream.discard_bytes(6*8);      //just throw transformation info for now
99                                 stream.read_short();    //"text version" should be 50
100                                 stream.read_long();             //"descriptor version" should be 16
101
102                                 //text data descriptor ('descriptor structure')
103                                 descriptor text_descriptor;
104                                 if(!text_descriptor.populate(stream))
105                                         throw PSDFileFormatException();
106
107                                 stream.read_short();    //"warp version" should be 1
108                                 stream.read_long();             //"descriptor version" should be 16
109
110                                 //warp data descriptor ('descriptor structure')
111                                 descriptor warp_descriptor;
112                                 if(!text_descriptor.populate(stream))
113                                         throw PSDFileFormatException();
114
115                                 stream.discard_bytes(4*8);      //top, left, right, bottom
116                         }
117
118                         stream.set_position(end_of_chunk);
119                 }
120         }
121         catch(PSDFileFormatException& ex)
122         {
123                 stream.set_position(end_of_layer_info);
124         }
125         
126         return result;
127 }
128
129 void layer::read_mask_data(BEFileInputStream& stream)
130 {
131         unsigned long length = stream.read_long();
132         switch(length)
133         {
134         case 0:
135                 break;
136
137         case 20:
138                 mask_.rect_.top = stream.read_long();
139                 mask_.rect_.left = stream.read_long();
140                 mask_.rect_.bottom = stream.read_long();
141                 mask_.rect_.right = stream.read_long();
142
143                 mask_.default_value_ = stream.read_byte();
144                 mask_.flags_ = stream.read_byte();
145                 stream.discard_bytes(2);
146                 break;
147
148         case 36:
149                 stream.discard_bytes(18);       //we don't care about the user mask if there is a "total user mask"
150                 mask_.flags_ = stream.read_byte();
151                 mask_.default_value_ = stream.read_byte();
152                 mask_.rect_.top = stream.read_long();
153                 mask_.rect_.left = stream.read_long();
154                 mask_.rect_.bottom = stream.read_long();
155                 mask_.rect_.right = stream.read_long();
156                 break;
157
158         default:
159                 stream.discard_bytes(length);
160                 break;
161         };
162 }
163
164 //TODO: implement
165 void layer::read_blending_ranges(BEFileInputStream& stream)
166 {
167         unsigned long length = stream.read_long();
168         stream.discard_bytes(length);
169 }
170
171 channel_ptr layer::get_channel(channel_type type)
172 {
173         auto end = channels_.end();
174         for(auto it = channels_.begin(); it != end; ++it)
175         {
176                 if((*it)->id() == type)
177                 {
178                         return (*it);
179                 }
180         }
181
182         return NULL;
183 }
184
185 void layer::read_channel_data(BEFileInputStream& stream)
186 {
187         image8bit_ptr img;
188         image8bit_ptr mask;
189
190         bool has_transparency(get_channel(psd::Transparency));
191
192         //std::clog << std::endl << "layer: " << std::string(name().begin(), name().end()) << std::endl;
193         
194         if(rect_.width() > 0 && rect_.height() > 0)
195         {
196                 img = std::make_shared<image8bit>(rect_.width(), rect_.height(), std::min<unsigned char>(channels_.size() - masks_, 4));
197                 //std::clog << std::dec << "has image: [width: " << rect_.width() << " height: " << rect_.height() << "]" << std::endl;
198
199                 if(!has_transparency)
200                         std::memset(img->data(), (unsigned long)(255<<24), rect_.width()*rect_.height());
201         }
202
203         if(masks_ > 0 && mask_.rect_.width() > 0 && mask_.rect_.height() > 0)
204         {
205                 mask = std::make_shared<image8bit>(mask_.rect_.width(), mask_.rect_.height(), 1);
206                 //std::clog << std::dec << "has mask: [width: " << mask_rect_.width() << " height: " << mask_rect_.height() << "]" << std::endl;
207         }
208
209         auto end = channels_.end();
210         for(auto it = channels_.begin(); it != end; ++it)
211         {
212                 image8bit_ptr target;
213                 unsigned char offset;
214                 bool discard_channel = false;
215
216                 //determine target bitmap and offset
217                 if((*it)->id() >= 3)
218                         discard_channel = true; //discard channels that doesn't contribute to the final image
219                 else if((*it)->id() >= -1)      //BGRA-data
220                 {
221                         target = img;
222                         offset = ((*it)->id() >= 0) ? 2 - (*it)->id() : 3;
223                 }
224                 else if(mask)   //mask
225                 {
226                         if((*it)->id() == -2 && masks_ == 2)    //if there are two mask-channels, discard the the one that's not the total mask
227                                 discard_channel = true;
228                         else
229                         {
230                                 target = mask;
231                                 offset = 0;
232                         }
233                 }
234
235                 //unsigned long cp = stream.current_position(); //for debug purposes only
236                 //std::clog << std::dec << "channel_id: " << (*it)->id() << ", reading data from: " << std::hex << cp << ", data_length: " << (*it)->data_length() << std::endl;
237
238                 if(!target)
239                         discard_channel = true;
240
241                 if(discard_channel)
242                 {
243                         //std::clog << "        -> discarding" << std::endl;
244                         stream.discard_bytes((*it)->data_length());
245                 }
246                 else
247                 {
248                         //std::clog << "        -> reading...";
249                         unsigned short encoding = stream.read_short();
250                         if(target)
251                         {
252                                 if(encoding == 0)
253                                         read_raw_image_data(stream, *it, target, offset);
254                                 else if(encoding == 1)
255                                         read_rle_image_data(stream, *it, target, offset);
256                                 else
257                                         throw PSDFileFormatException();
258                         }
259                         //std::clog << " " << std::hex << (stream.current_position() - cp) << " bytes read" << std::endl;
260                 }
261         }
262
263         if(img && has_transparency)
264         {
265                 caspar::image::image_view<caspar::image::bgra_pixel> view(img->data(), img->width(), img->height());
266                 caspar::image::premultiply(view);
267         }
268
269         image_ = img;
270         mask_.mask_ = mask;
271 }
272
273 void read_raw_image_data(BEFileInputStream& stream, const channel_ptr& channel, image8bit_ptr target, unsigned char offset)
274 {
275         unsigned long total_length = target->width() * target->height();
276         if(total_length != (channel->data_length() - 2))
277                 throw PSDFileFormatException();
278
279         unsigned char* data = target->data();
280
281         unsigned char stride = target->channel_count();
282         if(stride == 1)
283                 stream.read(reinterpret_cast<char*>(data + offset), total_length);
284         else
285         {
286                 for(unsigned long index=0; index < total_length; ++index)
287                         data[index*stride+offset] = stream.read_byte();
288         }
289 }
290
291 void read_rle_image_data(BEFileInputStream& stream, const channel_ptr& channel, image8bit_ptr target, unsigned char offset)
292 {
293         unsigned long width = target->width();
294         unsigned char stride = target->channel_count();
295         unsigned long height = target->height();
296
297         std::vector<unsigned short> scanline_lengths;
298         scanline_lengths.reserve(height);
299
300         for(unsigned long scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
301                 scanline_lengths.push_back(stream.read_short());
302
303         unsigned char* data = target->data();
304
305         for(unsigned long scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
306         {
307                 unsigned long colIndex = 0;
308                 unsigned char length = 0;
309                 do
310                 {
311                         length = 0;
312
313                         //Get controlbyte
314                         char controlByte = static_cast<char>(stream.read_byte());
315                         if(controlByte >= 0)
316                         {
317                                 //Read uncompressed string
318                                 length = controlByte+1;
319                                 for(unsigned long index=0; index < length; ++index)
320                                         data[(scanlineIndex*width+colIndex+index) * stride + offset] = stream.read_byte();
321                         }
322                         else if(controlByte > -128)
323                         {
324                                 //Repeat next byte
325                                 length = -controlByte+1;
326                                 unsigned value = stream.read_byte();
327                                 for(unsigned long index=0; index < length; ++index)
328                                         data[(scanlineIndex*width+colIndex+index) * stride + offset] = value;
329                         }
330
331                         colIndex += length;
332                 }
333                 while(colIndex < width);
334         }
335 }
336
337 }       //namespace psd
338 }       //namespace caspar