forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_brushes.cpp
515 lines (440 loc) · 13.7 KB
/
app_brushes.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Aseprite
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/app_brushes.h"
#include "app/resource_finder.h"
#include "app/tools/ink_type.h"
#include "app/xml_document.h"
#include "app/xml_exception.h"
#include "base/base64.h"
#include "base/convert_to.h"
#include "base/fs.h"
#include "base/serialization.h"
#include "doc/brush.h"
#include "doc/color.h"
#include "doc/image.h"
#include "doc/image_impl.h"
#include <fstream>
namespace app {
using namespace doc;
using namespace base::serialization;
using namespace base::serialization::little_endian;
namespace {
ImageRef load_xml_image(const TiXmlElement* imageElem)
{
ImageRef image;
int w, h;
if (imageElem->QueryIntAttribute("width", &w) != TIXML_SUCCESS ||
imageElem->QueryIntAttribute("height", &h) != TIXML_SUCCESS ||
w < 0 || w > 9999 ||
h < 0 || h > 9999)
return image;
auto formatValue = imageElem->Attribute("format");
if (!formatValue)
return image;
const char* pixels_base64 = imageElem->GetText();
if (!pixels_base64)
return image;
base::buffer data;
base::decode_base64(pixels_base64, data);
auto it = data.begin(), end = data.end();
std::string formatStr = formatValue;
if (formatStr == "rgba") {
image.reset(Image::create(IMAGE_RGB, w, h));
LockImageBits<RgbTraits> pixels(image.get());
for (auto& pixel : pixels) {
if ((end - it) < 4)
break;
int r = *it; ++it;
int g = *it; ++it;
int b = *it; ++it;
int a = *it; ++it;
pixel = doc::rgba(r, g, b, a);
}
}
else if (formatStr == "grayscale") {
image.reset(Image::create(IMAGE_GRAYSCALE, w, h));
LockImageBits<GrayscaleTraits> pixels(image.get());
for (auto& pixel : pixels) {
if ((end - it) < 2)
break;
int v = *it; ++it;
int a = *it; ++it;
pixel = doc::graya(v, a);
}
}
else if (formatStr == "indexed") {
image.reset(Image::create(IMAGE_INDEXED, w, h));
LockImageBits<IndexedTraits> pixels(image.get());
for (auto& pixel : pixels) {
if (it == end)
break;
pixel = *it;
++it;
}
}
else if (formatStr == "bitmap") {
image.reset(Image::create(IMAGE_BITMAP, w, h));
LockImageBits<BitmapTraits> pixels(image.get());
for (auto& pixel : pixels) {
if (it == end)
break;
pixel = *it;
++it;
}
}
return image;
}
void save_xml_image(TiXmlElement* imageElem, const Image* image)
{
int w = image->width();
int h = image->height();
imageElem->SetAttribute("width", w);
imageElem->SetAttribute("height", h);
std::string format;
switch (image->pixelFormat()) {
case IMAGE_RGB: format = "rgba"; break;
case IMAGE_GRAYSCALE: format = "grayscale"; break;
case IMAGE_INDEXED: format = "indexed"; break;
case IMAGE_BITMAP: format = "bitmap"; break; // TODO add "bitmap" format
}
ASSERT(!format.empty());
if (!format.empty())
imageElem->SetAttribute("format", format.c_str());
base::buffer data;
data.reserve(h * image->getRowStrideSize());
switch (image->pixelFormat()) {
case IMAGE_RGB:{
const LockImageBits<RgbTraits> pixels(image);
for (const auto& pixel : pixels) {
data.push_back(doc::rgba_getr(pixel));
data.push_back(doc::rgba_getg(pixel));
data.push_back(doc::rgba_getb(pixel));
data.push_back(doc::rgba_geta(pixel));
}
break;
}
case IMAGE_GRAYSCALE:{
const LockImageBits<GrayscaleTraits> pixels(image);
for (const auto& pixel : pixels) {
data.push_back(doc::graya_getv(pixel));
data.push_back(doc::graya_geta(pixel));
}
break;
}
case IMAGE_INDEXED: {
const LockImageBits<IndexedTraits> pixels(image);
for (const auto& pixel : pixels) {
data.push_back(pixel);
}
break;
}
case IMAGE_BITMAP: {
// Here we save bitmap format as indexed
const LockImageBits<BitmapTraits> pixels(image);
for (const auto& pixel : pixels) {
data.push_back(pixel); // TODO save bitmap format as bitmap
}
break;
}
}
std::string data_base64;
base::encode_base64(data, data_base64);
TiXmlText textElem(data_base64.c_str());
imageElem->InsertEndChild(textElem);
}
} // anonymous namespace
AppBrushes::AppBrushes()
{
m_standard.push_back(BrushRef(new Brush(kCircleBrushType, 7, 0)));
m_standard.push_back(BrushRef(new Brush(kSquareBrushType, 7, 0)));
m_standard.push_back(BrushRef(new Brush(kLineBrushType, 7, 44)));
try {
std::string fn = userBrushesFilename();
if (base::is_file(fn))
load(fn);
}
catch (const std::exception& ex) {
LOG(ERROR) << "BRSH: Error loading user brushes: " << ex.what() << "\n";
}
}
AppBrushes::~AppBrushes()
{
save(userBrushesFilename());
}
AppBrushes::slot_id AppBrushes::addBrushSlot(const BrushSlot& brush)
{
// Use an empty slot
for (size_t i=0; i<m_slots.size(); ++i) {
if (!m_slots[i].locked() || m_slots[i].isEmpty()) {
m_slots[i] = brush;
return i+1;
}
}
m_slots.push_back(brush);
ItemsChange();
return slot_id(m_slots.size()); // Returns the slot
}
void AppBrushes::removeBrushSlot(slot_id slot)
{
--slot;
if (slot >= 0 && slot < (int)m_slots.size()) {
m_slots[slot] = BrushSlot();
// Erase empty trailing slots
while (!m_slots.empty() &&
m_slots[m_slots.size()-1].isEmpty())
m_slots.erase(--m_slots.end());
ItemsChange();
}
}
void AppBrushes::removeAllBrushSlots()
{
while (!m_slots.empty())
m_slots.erase(--m_slots.end());
ItemsChange();
}
bool AppBrushes::hasBrushSlot(slot_id slot) const
{
--slot;
return (slot >= 0 && slot < (int)m_slots.size() &&
!m_slots[slot].isEmpty());
}
BrushSlot AppBrushes::getBrushSlot(slot_id slot) const
{
--slot;
if (slot >= 0 && slot < (int)m_slots.size())
return m_slots[slot];
else
return BrushSlot();
}
void AppBrushes::setBrushSlot(slot_id slot, const BrushSlot& brush)
{
--slot;
if (slot >= 0 && slot < (int)m_slots.size()) {
m_slots[slot] = brush;
ItemsChange();
}
}
void AppBrushes::lockBrushSlot(slot_id slot)
{
--slot;
if (slot >= 0 && slot < (int)m_slots.size() &&
!m_slots[slot].isEmpty()) {
m_slots[slot].setLocked(true);
}
}
void AppBrushes::unlockBrushSlot(slot_id slot)
{
--slot;
if (slot >= 0 && slot < (int)m_slots.size() &&
!m_slots[slot].isEmpty()) {
m_slots[slot].setLocked(false);
}
}
bool AppBrushes::isBrushSlotLocked(slot_id slot) const
{
--slot;
if (slot >= 0 && slot < (int)m_slots.size() &&
!m_slots[slot].isEmpty()) {
return m_slots[slot].locked();
}
else
return false;
}
static const int kBrushFlags =
int(BrushSlot::Flags::BrushType) |
int(BrushSlot::Flags::BrushSize) |
int(BrushSlot::Flags::BrushAngle);
void AppBrushes::load(const std::string& filename)
{
XmlDocumentRef doc = app::open_xml(filename);
TiXmlHandle handle(doc.get());
TiXmlElement* brushElem = handle
.FirstChild("brushes")
.FirstChild("brush").ToElement();
while (brushElem) {
// flags
int flags = 0;
BrushRef brush;
app::Color fgColor;
app::Color bgColor;
tools::InkType inkType = tools::InkType::DEFAULT;
int inkOpacity = 255;
Shade shade;
bool pixelPerfect = false;
// Brush
const char* type = brushElem->Attribute("type");
const char* size = brushElem->Attribute("size");
const char* angle = brushElem->Attribute("angle");
if (type || size || angle) {
if (type) flags |= int(BrushSlot::Flags::BrushType);
if (size) flags |= int(BrushSlot::Flags::BrushSize);
if (angle) flags |= int(BrushSlot::Flags::BrushAngle);
brush.reset(
new Brush(
(type ? string_id_to_brush_type(type): kFirstBrushType),
(size ? base::convert_to<int>(std::string(size)): 1),
(angle ? base::convert_to<int>(std::string(angle)): 0)));
}
// Brush image
ImageRef image, mask;
if (TiXmlElement* imageElem = brushElem->FirstChildElement("image"))
image = load_xml_image(imageElem);
if (TiXmlElement* maskElem = brushElem->FirstChildElement("mask"))
mask = load_xml_image(maskElem);
if (image) {
if (!brush)
brush.reset(new Brush());
brush->setImage(image.get(), mask.get());
}
// Colors
if (TiXmlElement* fgcolorElem = brushElem->FirstChildElement("fgcolor")) {
if (auto value = fgcolorElem->Attribute("value")) {
fgColor = app::Color::fromString(value);
flags |= int(BrushSlot::Flags::FgColor);
}
}
if (TiXmlElement* bgcolorElem = brushElem->FirstChildElement("bgcolor")) {
if (auto value = bgcolorElem->Attribute("value")) {
bgColor = app::Color::fromString(value);
flags |= int(BrushSlot::Flags::BgColor);
}
}
// Ink
if (TiXmlElement* inkTypeElem = brushElem->FirstChildElement("inktype")) {
if (auto value = inkTypeElem->Attribute("value")) {
inkType = app::tools::string_id_to_ink_type(value);
flags |= int(BrushSlot::Flags::InkType);
}
}
if (TiXmlElement* inkOpacityElem = brushElem->FirstChildElement("inkopacity")) {
if (auto value = inkOpacityElem->Attribute("value")) {
inkOpacity = base::convert_to<int>(std::string(value));
flags |= int(BrushSlot::Flags::InkOpacity);
}
}
// Shade
if (TiXmlElement* shadeElem = brushElem->FirstChildElement("shade")) {
if (auto value = shadeElem->Attribute("value")) {
shade = shade_from_string(value);
flags |= int(BrushSlot::Flags::Shade);
}
}
// Pixel-perfect
if (TiXmlElement* pixelPerfectElem = brushElem->FirstChildElement("pixelperfect")) {
pixelPerfect = bool_attr_is_true(pixelPerfectElem, "value");
flags |= int(BrushSlot::Flags::PixelPerfect);
}
// Image color (enabled by default for backward compatibility)
if (!brushElem->Attribute("imagecolor") ||
bool_attr_is_true(brushElem, "imagecolor"))
flags |= int(BrushSlot::Flags::ImageColor);
if (flags != 0)
flags |= int(BrushSlot::Flags::Locked);
BrushSlot brushSlot(BrushSlot::Flags(flags),
brush, fgColor, bgColor,
inkType, inkOpacity, shade,
pixelPerfect);
m_slots.push_back(brushSlot);
brushElem = brushElem->NextSiblingElement();
}
}
void AppBrushes::save(const std::string& filename) const
{
XmlDocumentRef doc(new TiXmlDocument());
TiXmlElement brushesElem("brushes");
//<?xml version="1.0" encoding="utf-8"?>
for (const auto& slot : m_slots) {
TiXmlElement brushElem("brush");
if (slot.locked()) {
// Flags
int flags = int(slot.flags());
// This slot might not have a brush. (E.g. a slot that changes
// the pixel perfect mode only.)
if (!slot.hasBrush())
flags &= ~kBrushFlags;
// Brush type
if (slot.hasBrush()) {
ASSERT(slot.brush());
if (flags & int(BrushSlot::Flags::BrushType)) {
brushElem.SetAttribute(
"type", brush_type_to_string_id(slot.brush()->type()).c_str());
}
if (flags & int(BrushSlot::Flags::BrushSize)) {
brushElem.SetAttribute("size", slot.brush()->size());
}
if (flags & int(BrushSlot::Flags::BrushAngle)) {
brushElem.SetAttribute("angle", slot.brush()->angle());
}
if (slot.brush()->type() == kImageBrushType &&
slot.brush()->image()) {
TiXmlElement elem("image");
save_xml_image(&elem, slot.brush()->image());
brushElem.InsertEndChild(elem);
if (slot.brush()->maskBitmap()) {
TiXmlElement maskElem("mask");
save_xml_image(&maskElem, slot.brush()->maskBitmap());
brushElem.InsertEndChild(maskElem);
}
// Image color
brushElem.SetAttribute(
"imagecolor",
(flags & int(BrushSlot::Flags::ImageColor)) ? "true": "false");
}
}
// Colors
if (flags & int(BrushSlot::Flags::FgColor)) {
TiXmlElement elem("fgcolor");
elem.SetAttribute("value", slot.fgColor().toString().c_str());
brushElem.InsertEndChild(elem);
}
if (flags & int(BrushSlot::Flags::BgColor)) {
TiXmlElement elem("bgcolor");
elem.SetAttribute("value", slot.bgColor().toString().c_str());
brushElem.InsertEndChild(elem);
}
// Ink
if (flags & int(BrushSlot::Flags::InkType)) {
TiXmlElement elem("inktype");
elem.SetAttribute(
"value", app::tools::ink_type_to_string_id(slot.inkType()).c_str());
brushElem.InsertEndChild(elem);
}
if (flags & int(BrushSlot::Flags::InkOpacity)) {
TiXmlElement elem("inkopacity");
elem.SetAttribute("value", slot.inkOpacity());
brushElem.InsertEndChild(elem);
}
// Shade
if (flags & int(BrushSlot::Flags::Shade)) {
TiXmlElement elem("shade");
elem.SetAttribute("value", shade_to_string(slot.shade()).c_str());
brushElem.InsertEndChild(elem);
}
// Pixel-perfect
if (flags & int(BrushSlot::Flags::PixelPerfect)) {
TiXmlElement elem("pixelperfect");
elem.SetAttribute("value", slot.pixelPerfect() ? "true": "false");
brushElem.InsertEndChild(elem);
}
}
brushesElem.InsertEndChild(brushElem);
}
TiXmlDeclaration declaration("1.0", "utf-8", "");
doc->InsertEndChild(declaration);
doc->InsertEndChild(brushesElem);
save_xml(doc, filename);
}
// static
std::string AppBrushes::userBrushesFilename()
{
ResourceFinder rf;
rf.includeUserDir("user.aseprite-brushes");
return rf.getFirstOrCreateDefault();
}
} // namespace app