-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchprocessor.cpp
More file actions
404 lines (344 loc) · 10.8 KB
/
batchprocessor.cpp
File metadata and controls
404 lines (344 loc) · 10.8 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "batchprocessor.h"
#include "canvaswidget-opencl.h"
#include "canvasstack.h"
#include "canvasstrokepoint.h"
#include "imagefiles.h"
#include "ora.h"
#include "toolfactory.h"
#include <QApplication>
#include <QFile>
#include <QImageWriter>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QDebug>
struct BatchProcessorContext {
CanvasStack layers;
std::unique_ptr<CanvasLayer> currentLayerCopy;
};
class BatchCommand
{
public:
virtual ~BatchCommand() {}
virtual void apply(BatchProcessorContext *) {}
};
class BatchCommandStroke : public BatchCommand
{
public:
BatchCommandStroke(QJsonObject const &json);
void apply(BatchProcessorContext *ctx) override;
QString toolPath;
QColor color;
QMap<QString, QVariant> toolSettings;
QPointF pointsOffset;
std::vector<CanvasStrokePoint> points;
};
class BatchCommandRotate : public BatchCommand
{
public:
BatchCommandRotate(QJsonObject const &json);
void apply(BatchProcessorContext *ctx) override;
QPoint origin;
double angle = 0.0;
QPointF offset;
};
class BatchCommandScale : public BatchCommand
{
public:
BatchCommandScale(QJsonObject const &json);
void apply(BatchProcessorContext *ctx) override;
QPoint origin;
QPointF scale;
QPointF offset;
};
class BatchCommandExport : public BatchCommand
{
public:
BatchCommandExport(QJsonObject const &json);
void apply(BatchProcessorContext *ctx) override;
QString path;
};
class BatchCommandOpen : public BatchCommand
{
public:
BatchCommandOpen(QJsonObject const &json);
void apply(BatchProcessorContext *ctx) override;
QString path;
};
BatchCommandStroke::BatchCommandStroke(const QJsonObject &json)
{
if (!json.contains("points") || !json["points"].isArray())
throw QString("Stroke command without points");
toolPath = json["path"].toString();
QJsonArray const &offsetObj = json["offset"].toArray();
if (offsetObj.size() == 2)
{
pointsOffset.rx() = offsetObj.at(0).toDouble();
pointsOffset.ry() = offsetObj.at(1).toDouble();
}
QJsonArray const &colorObj = json["color"].toArray();
if (colorObj.size() == 3)
{
float r = colorObj.at(0).toDouble();
float g = colorObj.at(1).toDouble();
float b = colorObj.at(2).toDouble();
color = QColor::fromRgbF(r, g, b);
}
QJsonObject const &settingsObj = json["settings"].toObject();
for (auto iter = settingsObj.begin(); iter != settingsObj.end(); ++iter)
{
toolSettings[iter.key()] = iter.value().toVariant();
}
QJsonArray const &pointsArray = json["points"].toArray();
points = CanvasStrokePoint::pointsFromJSON(pointsArray);
}
void BatchCommandStroke::apply(BatchProcessorContext *ctx)
{
CanvasLayer *layer = ctx->layers.layers.at(0);
std::unique_ptr<BaseTool> tool;
if (!toolPath.isEmpty())
tool = ToolFactory::loadTool(toolPath);
if (!tool)
tool = ToolFactory::loadTool(ToolFactory::defaultToolName());
if (color.isValid())
tool->setColor(color);
for (auto iter = toolSettings.begin(); iter != toolSettings.end(); ++iter)
tool->setToolSetting(iter.key(), iter.value());
StrokeContextArgs args = {layer, ctx->currentLayerCopy.get()};
std::unique_ptr<StrokeContext> stroke(tool->newStroke(args));
auto pointsIter = points.begin();
if (pointsIter != points.end())
{
QPointF xy(pointsIter->x, pointsIter->y);
stroke->startStroke(xy + pointsOffset, pointsIter->p);
++pointsIter;
}
while (pointsIter != points.end())
{
QPointF xy(pointsIter->x, pointsIter->y);
stroke->strokeTo(xy + pointsOffset, pointsIter->p, pointsIter->dt);
++pointsIter;
}
stroke.reset();
//FIXME: Copy only modified tiles
ctx->currentLayerCopy = ctx->layers.layers.at(0)->deepCopy();
}
BatchCommandRotate::BatchCommandRotate(const QJsonObject &json)
{
angle = json["angle"].toDouble(0.0);
QJsonArray jsonOrigin = json["origin"].toArray();
if (jsonOrigin.size() == 2)
{
origin.rx() = jsonOrigin.at(0).toInt();
origin.ry() = jsonOrigin.at(1).toInt();
}
else
{
throw QString("Invalid origin for rotatation");
}
QJsonArray jsonOffset = json["offset"].toArray();
if (jsonOffset.size() == 2)
{
offset.rx() = jsonOffset.at(0).toDouble();
offset.ry() = jsonOffset.at(1).toDouble();
}
else if (jsonOffset.size() != 0)
{
throw QString("Invalid offset for rotatation");
}
}
void BatchCommandRotate::apply(BatchProcessorContext *ctx)
{
QMatrix transform;
transform.translate(offset.x(), offset.y());
transform.translate(origin.x(), origin.y());
transform.rotate(angle);
transform.translate(-origin.x(), -origin.y());
CanvasLayer *layer = ctx->layers.layers.at(0);
std::unique_ptr<CanvasLayer> rotated = layer->applyMatrix(transform);
layer->takeTiles(rotated.get());
layer->prune();
ctx->currentLayerCopy = ctx->layers.layers.at(0)->deepCopy();
}
BatchCommandScale::BatchCommandScale(const QJsonObject &json)
{
QJsonArray jsonOrigin = json["origin"].toArray();
if (jsonOrigin.size() == 2)
{
origin.rx() = jsonOrigin.at(0).toInt();
origin.ry() = jsonOrigin.at(1).toInt();
}
else
{
throw QString("Invalid origin for scale");
}
QJsonArray jsonScale = json["scale"].toArray();
if (jsonScale.size() == 2)
{
scale.rx() = jsonScale.at(0).toDouble();
scale.ry() = jsonScale.at(1).toDouble();
}
else
{
throw QString("Invalid ratio for scale");
}
QJsonArray jsonOffset = json["offset"].toArray();
if (jsonOffset.size() == 2)
{
offset.rx() = jsonOffset.at(0).toDouble();
offset.ry() = jsonOffset.at(1).toDouble();
}
else if (jsonOffset.size() != 0)
{
throw QString("Invalid offset for scale");
}
}
void BatchCommandScale::apply(BatchProcessorContext *ctx)
{
QMatrix transform;
transform.translate(offset.x(), offset.y());
transform.translate(origin.x(), origin.y());
transform.scale(scale.x(), scale.y());
transform.translate(-origin.x(), -origin.y());
CanvasLayer *layer = ctx->layers.layers.at(0);
std::unique_ptr<CanvasLayer> scaled = layer->applyMatrix(transform);
layer->takeTiles(scaled.get());
layer->prune();
ctx->currentLayerCopy = ctx->layers.layers.at(0)->deepCopy();
}
BatchCommandExport::BatchCommandExport(const QJsonObject &json)
{
if (!json.contains("path"))
throw QString("Export command without path");
QString pathStr = json["path"].toString();
if (pathStr.isEmpty())
throw QString("Export command with invalid path");
path = pathStr;
}
void BatchCommandExport::apply(BatchProcessorContext *ctx)
{
if (path.endsWith(".ora"))
{
saveStackAs(&ctx->layers, QRect(), path);
}
else
{
QImage output = stackToImage(&ctx->layers);
if (output.isNull())
{
qWarning() << "Failed to export, image is empty";
return;
}
QImageWriter writer(path);
if (path.endsWith(".jpg", Qt::CaseInsensitive) || path.endsWith(".jpeg", Qt::CaseInsensitive))
writer.setQuality(90);
else if (path.endsWith(".png", Qt::CaseInsensitive))
writer.setQuality(9);
if (!writer.write(output))
{
qWarning() << "Export failed" << writer.errorString();
}
}
}
BatchCommandOpen::BatchCommandOpen(const QJsonObject &json)
{
if (!json.contains("path"))
throw QString("Open command without path");
QString pathStr = json["path"].toString();
if (pathStr.isEmpty() || !QFile::exists(pathStr))
throw QString("Open command with invalid path");
path = pathStr;
}
void BatchCommandOpen::apply(BatchProcessorContext *ctx)
{
if (path.endsWith(".ora"))
{
loadStackFromORA(&ctx->layers, nullptr, path);
}
else
{
QImage image(path);
if (!image.isNull())
{
ctx->layers.clearLayers();
std::unique_ptr<CanvasLayer> imageLayer = layerFromImage(image);
imageLayer->name = path;
ctx->layers.layers.append(imageLayer.release());
}
else
{
qWarning() << "Failed to load" << path << ": invalid file format";
}
}
}
BatchProcessor::BatchProcessor(QObject *parent) :
QObject(parent)
{
}
void BatchProcessor::execute(QString path)
{
QFile file(path);
std::vector<std::unique_ptr<BatchCommand>> commandList;
try
{
if (!file.open(QIODevice::ReadOnly))
throw QString("Failed to open file: ") + file.errorString();
QByteArray source = file.readAll();
if (source.isNull())
throw QString("Failed to read file");
QJsonDocument const batchJsonDoc = QJsonDocument::fromJson(source);
if (!batchJsonDoc.isArray())
throw QString("JSON parse failed");
for (QJsonValue const &iterValue: batchJsonDoc.array())
{
QJsonObject const &commandObject = iterValue.toObject();
QString commandName = commandObject["command"].toString();
if (commandName.isEmpty())
{
throw QString("Missing command");
}
else if (commandName == QStringLiteral("stroke"))
{
commandList.emplace_back(new BatchCommandStroke(commandObject));
}
else if (commandName == QStringLiteral("rotate"))
{
commandList.emplace_back(new BatchCommandRotate(commandObject));
}
else if (commandName == QStringLiteral("scale"))
{
commandList.emplace_back(new BatchCommandScale(commandObject));
}
else if (commandName == QStringLiteral("export"))
{
commandList.emplace_back(new BatchCommandExport(commandObject));
}
else if (commandName == QStringLiteral("open"))
{
commandList.emplace_back(new BatchCommandOpen(commandObject));
}
else
{
throw QString("Invalid command: ") + commandName;
}
}
}
catch (QString err)
{
qWarning() << path << err;
QApplication::quit();
return;
}
qDebug() << "Parsed" << commandList.size() << "batch commands.";
if (!commandList.empty())
{
SharedOpenCL::getSharedOpenCL();
BatchProcessorContext ctx;
ctx.layers.layers.append(new CanvasLayer());
ctx.currentLayerCopy = ctx.layers.layers.at(0)->deepCopy();
for (auto &commandIter: commandList)
commandIter->apply(&ctx);
}
QApplication::quit();
}