-
Notifications
You must be signed in to change notification settings - Fork 3
/
bsv3file.cpp
269 lines (229 loc) · 8.15 KB
/
bsv3file.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// ///////////////////////////////////////////////////////////////////////// //
// //
// Copyright (C) 2014, 2015 by Oleg Polivets //
// [email protected] //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// ///////////////////////////////////////////////////////////////////////// //
#include "bsv3file.h"
#include "rgbfile.h"
#include <QFile>
#include <QImage>
#include <QPainter>
#include <QTransform>
#include <QDebug>
#include <limits>
#include <iostream>
//#define ENABLE_LOGS
namespace op {
Bsv3File::~Bsv3File()
{}
Bsv3File::Bsv3File(QIODevice & bsvFile, const QImage & rgbImage)
: mPict(rgbImage)
{
loadFromIODevice(bsvFile);
}
bool Bsv3File::loadFromIODevice(QIODevice & file) {
if (!file.open(QIODevice::ReadOnly)) {
std::cout << "Bsv3: can't open file\n";
return false;
}
int sign = 0;
file.read((char*)&sign, 2);
if (sign == 0x0104) {
std::cout << file.read(5).toHex().data() << "\n";
}
#ifdef ENABLE_LOGS
std::cout << "signature=" << sign << "\n";
#endif
int regionsCount = 0;
file.read((char*)®ionsCount, 2);
#ifdef ENABLE_LOGS
std::cout << "regionsCount=0x" << std::hex << regionsCount << "\n";
#endif
// read flags
uchar len=0;
file.read((char*)&len, 1);
bool hasOpacity = ((len & 1) == 1);
int transCnt = 24 + (hasOpacity ? 1 : 0);
if (sign == 0x0104) {
transCnt += 1;
}
// set of image regions
int x,y,w,h;
char name[256];
for (int i = 0; i < regionsCount; ++i) {
len=0;
file.read((char*)&len, 1);
file.read(name, len);
name[len] = '\0';
x=y=w=h=0;
file.read((char*)&x, 2);
file.read((char*)&y, 2);
file.read((char*)&w, 2);
file.read((char*)&h, 2);
mRects.push_back(Scrap(QString(name), QRect(x, y, w, h)));
#ifdef ENABLE_LOGS
std::cout << name << ":"
<< x << ":"
<< y << ":"
<< w << ":"
<< h << "\n";
#endif
}
if (sign == 0x0104) {
std::cout << file.read(2).toHex().data() << "\n";
}
// set of transformations
int count, sub;
count = 0;
file.read((char*)&count, 2);
#ifdef ENABLE_LOGS
std::cout << "transformationsCount=" << count << "\n";
#endif
for (int i = 0; i < count; ++i) {
#ifdef ENABLE_LOGS
std::cout << "offset=0x" << std::hex << file.pos() << std::endl;
#endif
sub = 0;
file.read((char*)&sub, 2);
len = 0;
file.read((char*)&len, 1);
#ifdef ENABLE_LOGS
std::cout << "[" << i << "]" << sub << "\n";
#endif
mTransformations.push_back(QVector<ScrapTransform>());
int regId = 0;
for (int j = 0; j < sub; ++j) {
regId = 0;
file.read((char*)®Id, 2);
QByteArray data(file.read(transCnt));
Q_ASSERT(data.size() == transCnt);
ScrapDrawData * sdd = (ScrapDrawData *) data.data();
#ifdef ENABLE_LOGS
std::cout << " "
<< regId << " "
<< sdd->x << " "
<< sdd->y << " "
<< sdd->xscale << " "
<< sdd->skew_h << " "
<< sdd->skew_v << " "
<< sdd->yscale << std::endl;
#endif
mTransformations[i].push_back(ScrapTransform(regId, sdd, hasOpacity ? sdd->opacity : 0xff));
}
}
// animation indexes
int ib, ie;
count = 0;
file.read((char*)&count, 2);
#ifdef ENABLE_LOGS
std::cout << "animsCount = " << count << std::endl;
#endif
for (int i = 0; i < count; ++i) {
len = 0;
file.read((char*)&len, 1);
file.read(name, len);
name[len] = '\0';
ib = ie = 0;
file.read((char*)&ib, 2);
file.read((char*)&ie, 2);
#ifdef ENABLE_LOGS
std::cout << name << ":" << ib << ":" << ie << "\n";
#endif
mAnimations.insert(name, QPair<int, int>(ib, ie));
}
file.close();
return true;
}
QImage createSubImage(const QImage & image, const QRect & rect) {
size_t offset = rect.x() * image.depth() / 8
+ rect.y() * image.bytesPerLine();
return QImage(image.bits() + offset,
rect.width(),
rect.height(),
image.bytesPerLine(),
image.format());
}
QVector< QImage > Bsv3File::getFrames(const QString & framesName) const {
Q_ASSERT(!mPict.isNull());
QVector< QImage > frames;
if (mAnimations.contains(framesName)) {
const int ib = mAnimations[framesName].first;
const int ie = mAnimations[framesName].second;
Q_ASSERT(ib >= 0 && ib < mTransformations.size());
Q_ASSERT(ie >= 0 && ie < mTransformations.size());
// calculate width and height for set of frames
float baseX, w, h;
float minX = std::numeric_limits<float>::max();
float minY = std::numeric_limits<float>::max();
float maxX = -400;
float maxY = -400;
for (int index = ib; index <= ie; ++index) {
for (int i = mTransformations[index].size()-1; i >= 0; --i) {
const ScrapTransform & ts = mTransformations[index][i];
const QRect & scrapRect = mRects[ ts.mIndex ].mRect;
if (ts.mX < minX) minX = ts.mX;
if (ts.mY < minY) minY = ts.mY;
float t = ts.mX + scrapRect.width();
if (t > maxX) {
maxX = t;
}
if (ts.mY > maxY) {
maxY = ts.mY;
}
}
}
baseX = minX * -1;
w = abs(maxX) + abs(minX);
h = abs(maxY) + abs(minY);
qDebug() << framesName
<< "baseX =" << baseX
<< "w =" << w
<< "h =" << h;
// make frames
for (int index = ib; index <= ie; ++index) {
// build single frame with transfomations data
QImage img(w, h, mPict.format());
Q_ASSERT(!img.isNull());
img.fill(Qt::transparent);
QPainter painter(&img);
for (int i = mTransformations[index].size()-1; i >= 0; --i) {
const ScrapTransform & ts = mTransformations[index][i];
const QRect & scrapRect = mRects[ts.mIndex].mRect;
QImage scrap(createSubImage(mPict, scrapRect));
QTransform transform;
transform.scale(ts.mFactor, ts.mFactor2);
transform.rotate(ts.mRX, Qt::XAxis);
transform.rotate(ts.mRY, Qt::YAxis);
scrap = scrap.transformed(transform);
float x = ts.mX + baseX;
float y = ts.mY + h;
Q_ASSERT( x >= 0 && y >= 0 );
qDebug() << "x =" << x << "y =" << y;
painter.setOpacity(ts.mOpacity);
painter.drawImage(QPointF(x, y), scrap);
}
painter.end();
frames.push_back(img);
}
}
return frames;
}
QImage Bsv3File::transImage() const {
const QVector< QImage > & frames = getFrames("Neutral");
return frames.size() == 0 ? QImage() : frames[0];
}
QList<QString> Bsv3File::animationNames() const {
return mAnimations.keys();
}
} // namespace op