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
22 #include "psd_document.h"
23 #include "descriptor.h"
26 namespace caspar { namespace psd {
28 psd_document::psd_document()
33 , color_mode_(psd::color_mode::InvalidColorMode)
37 void psd_document::parse(const std::wstring& filename)
40 input_.open(filename_);
43 read_image_resources();
47 void psd_document::read_header()
49 auto signature = input_.read_long();
50 auto version = input_.read_short();
51 if(!(signature == '8BPS' && version == 1))
52 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("!(signature == '8BPS' && version == 1)"));
54 input_.discard_bytes(6);
55 channels_= input_.read_short();
56 height_ = input_.read_long();
57 width_ = input_.read_long();
58 depth_ = input_.read_short(); //bits / channel
60 color_mode_ = int_to_color_mode(input_.read_short());
63 void psd_document::read_color_mode()
65 auto length = input_.read_long();
66 input_.discard_bytes(length);
69 void psd_document::read_image_resources()
71 unsigned long section_length = input_.read_long();
73 if(section_length > 0)
75 std::streamoff end_of_section = input_.current_position() + section_length;
79 while(input_.current_position() < end_of_section)
81 auto signature = input_.read_long();
82 if(signature != '8BIM')
83 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("signature != '8BIM'"));
85 auto resource_id = input_.read_short();
87 std::wstring name = input_.read_pascal_string(2);
89 auto resource_length = input_.read_long();
90 auto end_of_chunk = input_.current_position() + resource_length;
94 //TODO: read actual data
97 case 1026: //layer group information. describes linked layers
99 int layer_count = resource_length / 2;
100 for(int i = 0; i < layer_count; ++i)
102 short id = input_.read_short();
104 if(i == layers_.size())
105 layers_.push_back(std::make_shared<layer>());
107 layers_[i]->set_link_group_id(id);
112 case 1075: //timeline information
114 input_.read_long(); //descriptor version, should be 16
115 descriptor timeline_descriptor;
116 timeline_descriptor.populate(input_);
117 timeline_desc_.swap(timeline_descriptor.items());
124 //struct ResolutionInfo
134 case 1006: //names of alpha channels
136 case 1010: //background color
137 case 1024: //layer state info (2 bytes containing the index of target layer (0 = bottom layer))
138 case 1028: //IPTC-NAA. File Info...
139 case 1029: //image for raw format files
140 case 1036: //thumbnail resource
141 case 1045: //unicode Alpha names (Unicode string (4 bytes length followed by string))
142 case 1053: //alpha identifiers (4 bytes of length, followed by 4 bytes each for every alpha identifier.)
143 case 1060: //XMP metadata
144 case 1065: //layer comps
145 case 1069: //layer selection ID(s)
146 case 1072: //layer group(s) enabled id
147 case 1077: //DisplayInfo
148 case 2999: //name of clipping path
151 if(resource_id >= 2000 && resource_id <=2997) //path information
154 else if(resource_id >= 4000 && resource_id <= 4999) //plug-in resources
158 input_.discard_bytes(resource_length);
162 if((resource_length & 1) == 1) //each resource is padded to an even amount of bytes
163 input_.discard_bytes(1);
165 catch(psd_file_format_exception&)
167 input_.set_position(end_of_chunk);
171 catch(psd_file_format_exception&)
173 //if an error occurs, just skip this section
174 input_.set_position(end_of_section);
180 void psd_document::read_layers()
182 //"Layer And Mask information"
183 auto total_length = input_.read_long(); //length of "Layer and Mask information"
184 auto end_of_layers = input_.current_position() + total_length;
188 //"Layer info section"
190 auto layer_info_length = input_.read_long(); //length of "Layer info" section
191 auto end_of_layers_info = input_.current_position() + layer_info_length;
193 auto layers_count = std::abs(static_cast<short>(input_.read_short()));
194 //std::clog << "Expecting " << layers_count << " layers" << std::endl;
196 for(int layer_index = 0; layer_index < layers_count; ++layer_index)
198 if(layer_index == layers_.size())
199 layers_.push_back(std::make_shared<layer>());
201 layers_[layer_index]->populate(input_, *this);
202 //std::clog << "Added layer: " << std::string(layers_[layerIndex]->name().begin(), layers_[layerIndex]->name().end()) << std::endl;
205 auto end = layers_.end();
206 for(auto layer_it = layers_.begin(); layer_it != end; ++layer_it)
208 (*layer_it)->read_channel_data(input_); //each layer reads it's "image data"
211 input_.set_position(end_of_layers_info);
214 //global layer mask info
215 auto global_layer_mask_length = input_.read_long();
216 input_.discard_bytes(global_layer_mask_length);
219 catch(std::exception&)
221 input_.set_position(end_of_layers);