forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes_sequence.cpp
207 lines (171 loc) · 4.82 KB
/
frames_sequence.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Aseprite Document Library
// Copyright (c) 2023 Igara Studio S.A.
//
// 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/frames_sequence.h"
#include "base/base.h"
#include "base/debug.h"
#include "base/serialization.h"
#include <algorithm>
#include <iostream>
namespace doc {
using namespace base::serialization;
using namespace base::serialization::little_endian;
FramesSequence::FramesSequence(const SelectedFrames& selectedFrames)
{
// TODO: This might be optimized by copying each range directly, but
// for this I would need that SelectedFrames make this class its friend.
for(auto frame : selectedFrames) {
this->insert(frame);
}
}
std::size_t FramesSequence::size() const
{
std::size_t size = 0;
for (auto& range : m_ranges)
size += ABS(range.toFrame - range.fromFrame) + 1;
return size;
}
void FramesSequence::clear()
{
m_ranges.clear();
}
void FramesSequence::insert(frame_t frame)
{
ASSERT(frame >= 0);
if (m_ranges.empty()) {
m_ranges.push_back(FrameRange(frame));
return;
}
auto& lastRange = m_ranges.back();
int rangeStep = (lastRange.fromFrame < lastRange.toFrame) -
(lastRange.fromFrame > lastRange.toFrame);
int step = (lastRange.toFrame < frame) - (lastRange.toFrame > frame);
if (step &&
(rangeStep == step || !rangeStep) &&
frame-lastRange.toFrame == step)
lastRange.toFrame = frame;
else
m_ranges.push_back(FrameRange(frame));
}
void FramesSequence::insert(frame_t fromFrame, frame_t toFrame)
{
int step = (fromFrame <= toFrame ? 1 : -1);
for (frame_t frame = fromFrame; frame != toFrame; frame+=step) {
insert(frame);
}
insert(toFrame);
}
FramesSequence FramesSequence::filter(frame_t fromFrame, frame_t toFrame) const
{
FramesSequence f;
if (fromFrame > toFrame)
std::swap(fromFrame, toFrame);
for (const auto& range : m_ranges) {
FrameRange r(range);
const bool isForward = (r.fromFrame <= r.toFrame);
if (isForward) {
if (r.fromFrame < fromFrame) r.fromFrame = fromFrame;
if (r.toFrame > toFrame) r.toFrame = toFrame;
}
else {
if (r.fromFrame > toFrame) r.fromFrame = toFrame;
if (r.toFrame < fromFrame) r.toFrame = fromFrame;
}
if (( isForward && r.fromFrame <= r.toFrame) ||
(!isForward && r.fromFrame >= r.toFrame))
f.m_ranges.push_back(r);
}
return f;
}
bool FramesSequence::contains(frame_t frame) const
{
return std::find_if(
m_ranges.begin(),
m_ranges.end(),
[frame](const FrameRange& r) -> bool {
return (r.fromFrame <= frame && frame <= r.toFrame) ||
(r.fromFrame >= frame && frame >= r.toFrame);
}) != m_ranges.end();
}
frame_t FramesSequence::lowestFrame() const
{
frame_t lof = this->firstFrame();
for (auto range : m_ranges) {
lof = std::min<frame_t>(range.fromFrame, std::min<frame_t>(range.toFrame, lof));
}
return lof;
}
void FramesSequence::displace(frame_t frameDelta)
{
// Avoid setting negative numbers in frame ranges
auto lof = lowestFrame();
if (lof+frameDelta < 0)
frameDelta = -lof;
for (auto& range : m_ranges) {
range.fromFrame += frameDelta;
range.toFrame += frameDelta;
ASSERT(range.fromFrame >= 0);
ASSERT(range.toFrame >= 0);
}
}
FramesSequence FramesSequence::makeReverse() const
{
FramesSequence newFrames;
for (const FrameRange& range : m_ranges)
newFrames.m_ranges.insert(
newFrames.m_ranges.begin(),
FrameRange(range.toFrame, range.fromFrame));
return newFrames;
}
FramesSequence FramesSequence::makePingPong() const
{
FramesSequence newFrames = *this;
const int n = m_ranges.size();
int i = 0;
int j = m_ranges.size()-1;
for (const FrameRange& range : m_ranges) {
// Discard first or last range if it contains just one frame.
if (range.toFrame == range.fromFrame && (i == 0 || j == 0)) {
++i;
--j;
continue;
}
FrameRange reversedRange(range.toFrame,
range.fromFrame);
int step = (range.fromFrame < range.toFrame) -
(range.fromFrame > range.toFrame);
if (i == 0) reversedRange.toFrame+=step;
if (j == 0) reversedRange.fromFrame-=step;
if (reversedRange != range)
newFrames.m_ranges.insert(
newFrames.m_ranges.begin() + n,
reversedRange);
++i;
--j;
}
return newFrames;
}
bool FramesSequence::write(std::ostream& os) const
{
write32(os, size());
for (const frame_t frame : *this) {
write32(os, frame);
}
return os.good();
}
bool FramesSequence::read(std::istream& is)
{
clear();
int nframes = read32(is);
for (int i=0; i<nframes && is; ++i) {
frame_t frame = read32(is);
insert(frame);
}
return is.good();
}
} // namespace doc