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