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 "descriptor.h"
24 #include "util\pdf_reader.h"
26 typedef unsigned char uint8_t;
28 #include "../image/util/image_algorithms.h"
29 #include "../image/util/image_view.h"
31 namespace caspar { namespace psd {
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);
36 layer_ptr layer::create(BEFileInputStream& stream)
38 layer_ptr result(std::make_shared<layer>());
39 result->populate(stream);
43 void layer::populate(BEFileInputStream& stream)
45 rect_.top = stream.read_long();
46 rect_.left = stream.read_long();
47 rect_.bottom = stream.read_long();
48 rect_.right = stream.read_long();
50 //Get info about the channels in the layer
51 unsigned short channelCount = stream.read_short();
52 for(int channelIndex = 0; channelIndex < channelCount; ++channelIndex)
54 short channel_id = static_cast<short>(stream.read_short());
55 unsigned long data_length = stream.read_long();
60 channel_ptr channel(std::make_shared<Channel>(channel_id, data_length));
61 channels_.push_back(channel);
64 unsigned long blendModeSignature = stream.read_long();
65 if(blendModeSignature != '8BIM')
66 throw PSDFileFormatException();
68 unsigned long blendModeKey = stream.read_long();
69 blend_mode_ = int_to_blend_mode(blendModeKey);
71 opacity_ = stream.read_byte();
72 baseClipping_ = stream.read_byte() == 1 ? false : true;
73 flags_ = stream.read_byte();
75 stream.discard_bytes(1);
77 unsigned long extraDataSize = stream.read_long();
78 long position1 = stream.current_position();
79 mask_.read_mask_data(stream);
80 read_blending_ranges(stream);
82 name_ = stream.read_pascal_string(4);
84 //Aditional Layer Information
85 unsigned long end_of_layer_info = position1 + extraDataSize;
89 while(stream.current_position() < end_of_layer_info)
91 unsigned long signature = stream.read_long();
92 if(signature != '8BIM' && signature != '8B64')
93 throw PSDFileFormatException();
95 unsigned long key = stream.read_long();
96 unsigned long length = stream.read_long();
97 unsigned long end_of_chunk = stream.current_position() + length;
101 protection_flags_ = stream.read_long();
103 else if(key == 'TySh') //type tool object settings
105 std::wstring text; //the text in the layer
107 stream.read_short(); //should be 1
108 stream.discard_bytes(6*8); //just throw transformation info for now
109 stream.read_short(); //"text version" should be 50
110 stream.read_long(); //"descriptor version" should be 16
112 //text data descriptor ('descriptor structure')
113 descriptor text_descriptor;
114 if(!text_descriptor.populate(stream))
115 throw PSDFileFormatException();
118 if(text_descriptor.has_item(L"EngineData"))
120 const descriptor_item& text_data = text_descriptor.get_item(L"EngineData");
121 read_pdf(text_layer_info_, text_data.rawdata_data);
125 stream.read_short(); //"warp version" should be 1
126 stream.read_long(); //"descriptor version" should be 16
128 //warp data descriptor ('descriptor structure')
129 descriptor warp_descriptor;
130 if(!text_descriptor.populate(stream))
131 throw PSDFileFormatException();
133 stream.discard_bytes(4*8); //top, left, right, bottom
136 stream.set_position(end_of_chunk);
139 catch(PSDFileFormatException& ex)
141 stream.set_position(end_of_layer_info);
145 void layer::layer_mask::read_mask_data(BEFileInputStream& stream)
147 unsigned long length = stream.read_long();
154 rect_.top = stream.read_long();
155 rect_.left = stream.read_long();
156 rect_.bottom = stream.read_long();
157 rect_.right = stream.read_long();
159 default_value_ = stream.read_byte();
160 flags_ = stream.read_byte();
161 stream.discard_bytes(2);
165 stream.discard_bytes(18); //we don't care about the user mask if there is a "total user mask"
166 flags_ = stream.read_byte();
167 default_value_ = stream.read_byte();
168 rect_.top = stream.read_long();
169 rect_.left = stream.read_long();
170 rect_.bottom = stream.read_long();
171 rect_.right = stream.read_long();
175 stream.discard_bytes(length);
180 bool layer::is_text() const
182 return !text_layer_info_.empty();
186 void layer::read_blending_ranges(BEFileInputStream& stream)
188 unsigned long length = stream.read_long();
189 stream.discard_bytes(length);
192 channel_ptr layer::get_channel(channel_type type)
194 auto end = channels_.end();
195 for(auto it = channels_.begin(); it != end; ++it)
197 if((*it)->id() == type)
206 void layer::read_channel_data(BEFileInputStream& stream)
211 bool has_transparency(get_channel(psd::Transparency));
213 //std::clog << std::endl << "layer: " << std::string(name().begin(), name().end()) << std::endl;
215 if(rect_.width() > 0 && rect_.height() > 0)
217 img = std::make_shared<image8bit>(rect_.width(), rect_.height(), 4);
218 //std::clog << std::dec << "has image: [width: " << rect_.width() << " height: " << rect_.height() << "]" << std::endl;
220 if(!has_transparency)
221 std::memset(img->data(), 255, img->width()*img->height()*img->channel_count());
224 if(masks_ > 0 && mask_.rect_.width() > 0 && mask_.rect_.height() > 0)
226 mask = std::make_shared<image8bit>(mask_.rect_.width(), mask_.rect_.height(), 1);
227 //std::clog << std::dec << "has mask: [width: " << mask_rect_.width() << " height: " << mask_rect_.height() << "]" << std::endl;
230 auto end = channels_.end();
231 for(auto it = channels_.begin(); it != end; ++it)
233 image8bit_ptr target;
234 unsigned char offset;
235 bool discard_channel = false;
237 //determine target bitmap and offset
239 discard_channel = true; //discard channels that doesn't contribute to the final image
240 else if((*it)->id() >= -1) //BGRA-data
243 offset = ((*it)->id() >= 0) ? 2 - (*it)->id() : 3;
247 if((*it)->id() == -2 && masks_ == 2) //if there are two mask-channels, discard the the one that's not the total mask
248 discard_channel = true;
256 //unsigned long cp = stream.current_position(); //for debug purposes only
257 //std::clog << std::dec << "channel_id: " << (*it)->id() << ", reading data from: " << std::hex << cp << ", data_length: " << (*it)->data_length() << std::endl;
260 discard_channel = true;
264 //std::clog << " -> discarding" << std::endl;
265 stream.discard_bytes((*it)->data_length());
269 //std::clog << " -> reading...";
270 unsigned short encoding = stream.read_short();
274 read_raw_image_data(stream, *it, target, offset);
275 else if(encoding == 1)
276 read_rle_image_data(stream, *it, target, offset);
278 throw PSDFileFormatException();
280 //std::clog << " " << std::hex << (stream.current_position() - cp) << " bytes read" << std::endl;
284 if(img && has_transparency)
286 caspar::image::image_view<caspar::image::bgra_pixel> view(img->data(), img->width(), img->height());
287 caspar::image::premultiply(view);
294 void read_raw_image_data(BEFileInputStream& stream, const channel_ptr& channel, image8bit_ptr target, unsigned char offset)
296 unsigned long total_length = target->width() * target->height();
297 if(total_length != (channel->data_length() - 2))
298 throw PSDFileFormatException();
300 unsigned char* data = target->data();
302 unsigned char stride = target->channel_count();
304 stream.read(reinterpret_cast<char*>(data + offset), total_length);
307 for(unsigned long index=0; index < total_length; ++index)
308 data[index*stride+offset] = stream.read_byte();
312 void read_rle_image_data(BEFileInputStream& stream, const channel_ptr& channel, image8bit_ptr target, unsigned char offset)
314 unsigned long width = target->width();
315 unsigned char stride = target->channel_count();
316 unsigned long height = target->height();
318 std::vector<unsigned short> scanline_lengths;
319 scanline_lengths.reserve(height);
321 for(unsigned long scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
322 scanline_lengths.push_back(stream.read_short());
324 unsigned char* data = target->data();
326 for(unsigned long scanlineIndex=0; scanlineIndex < height; ++scanlineIndex)
328 unsigned long colIndex = 0;
329 unsigned char length = 0;
335 char controlByte = static_cast<char>(stream.read_byte());
338 //Read uncompressed string
339 length = controlByte+1;
340 for(unsigned long index=0; index < length; ++index)
341 data[(scanlineIndex*width+colIndex+index) * stride + offset] = stream.read_byte();
343 else if(controlByte > -128)
346 length = -controlByte+1;
347 unsigned value = stream.read_byte();
348 for(unsigned long index=0; index < length; ++index)
349 data[(scanlineIndex*width+colIndex+index) * stride + offset] = value;
354 while(colIndex < width);