1 // Aseprite Document Library
2 // Copyright (c) 2001-2016 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "doc/layer_io.h"
12 
13 #include "base/serialization.h"
14 #include "base/unique_ptr.h"
15 #include "doc/cel.h"
16 #include "doc/cel_data.h"
17 #include "doc/cel_data_io.h"
18 #include "doc/cel_io.h"
19 #include "doc/image_io.h"
20 #include "doc/layer.h"
21 #include "doc/layer_io.h"
22 #include "doc/sprite.h"
23 #include "doc/string_io.h"
24 #include "doc/subobjects_io.h"
25 #include "doc/user_data_io.h"
26 
27 #include <iostream>
28 #include <vector>
29 
30 namespace doc {
31 
32 using namespace base::serialization;
33 using namespace base::serialization::little_endian;
34 
35 // Serialized Layer data:
36 
write_layer(std::ostream & os,const Layer * layer)37 void write_layer(std::ostream& os, const Layer* layer)
38 {
39   write32(os, layer->id());
40   write_string(os, layer->name());
41 
42   write32(os, static_cast<int>(layer->flags())); // Flags
43   write16(os, static_cast<int>(layer->type()));  // Type
44 
45   switch (layer->type()) {
46 
47     case ObjectType::LayerImage: {
48       const LayerImage* imgLayer = static_cast<const LayerImage*>(layer);
49       CelConstIterator it, begin = imgLayer->getCelBegin();
50       CelConstIterator end = imgLayer->getCelEnd();
51 
52       // Blend mode & opacity
53       write16(os, (int)imgLayer->blendMode());
54       write8(os, imgLayer->opacity());
55 
56       // Images
57       int images = 0;
58       int celdatas = 0;
59       for (it=begin; it != end; ++it) {
60         Cel* cel = *it;
61         if (!cel->link()) {
62           ++images;
63           ++celdatas;
64         }
65       }
66 
67       write16(os, images);
68       for (it=begin; it != end; ++it) {
69         Cel* cel = *it;
70         if (!cel->link())
71           write_image(os, cel->image());
72       }
73 
74       write16(os, celdatas);
75       for (it=begin; it != end; ++it) {
76         Cel* cel = *it;
77         if (!cel->link())
78           write_celdata(os, cel->dataRef().get());
79       }
80 
81       // Cels
82       write16(os, imgLayer->getCelsCount());
83       for (it=begin; it != end; ++it) {
84         const Cel* cel = *it;
85         write_cel(os, cel);
86       }
87       break;
88     }
89 
90     case ObjectType::LayerGroup: {
91       // Number of sub-layers
92       write16(os, static_cast<const LayerGroup*>(layer)->layersCount());
93 
94       for (const Layer* child : static_cast<const LayerGroup*>(layer)->layers())
95         write_layer(os, child);
96       break;
97     }
98 
99   }
100 
101   write_user_data(os, layer->userData());
102 }
103 
read_layer(std::istream & is,SubObjectsFromSprite * subObjects)104 Layer* read_layer(std::istream& is, SubObjectsFromSprite* subObjects)
105 {
106   ObjectId id = read32(is);
107   std::string name = read_string(is);
108   uint32_t flags = read32(is);                     // Flags
109   uint16_t layer_type = read16(is);                // Type
110   base::UniquePtr<Layer> layer;
111 
112   switch (static_cast<ObjectType>(layer_type)) {
113 
114     case ObjectType::LayerImage: {
115       LayerImage* imgLayer = new LayerImage(subObjects->sprite());
116 
117       // Create layer
118       layer.reset(imgLayer);
119 
120       // Blend mode & opacity
121       imgLayer->setBlendMode((BlendMode)read16(is));
122       imgLayer->setOpacity(read8(is));
123 
124       // Read images
125       int images = read16(is);  // Number of images
126       for (int c=0; c<images; ++c) {
127         ImageRef image(read_image(is));
128         subObjects->addImageRef(image);
129       }
130 
131       // Read celdatas
132       int celdatas = read16(is);
133       for (int c=0; c<celdatas; ++c) {
134         CelDataRef celdata(read_celdata(is, subObjects));
135         subObjects->addCelDataRef(celdata);
136       }
137 
138       // Read cels
139       int cels = read16(is);                      // Number of cels
140       for (int c=0; c<cels; ++c) {
141         // Read the cel
142         Cel* cel = read_cel(is, subObjects);
143 
144         // Add the cel in the layer
145         imgLayer->addCel(cel);
146       }
147       break;
148     }
149 
150     case ObjectType::LayerGroup: {
151       // Create the layer set
152       layer.reset(new LayerGroup(subObjects->sprite()));
153 
154       // Number of sub-layers
155       int layers = read16(is);
156       for (int c=0; c<layers; c++) {
157         Layer* child = read_layer(is, subObjects);
158         if (child)
159           static_cast<LayerGroup*>(layer.get())->addLayer(child);
160         else
161           break;
162       }
163       break;
164     }
165 
166     default:
167       throw InvalidLayerType("Invalid layer type found in stream");
168 
169   }
170 
171   UserData userData = read_user_data(is);
172 
173   if (layer) {
174     layer->setName(name);
175     layer->setFlags(static_cast<LayerFlags>(flags));
176     layer->setId(id);
177     layer->setUserData(userData);
178   }
179 
180   return layer.release();
181 }
182 
183 }
184