forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantization.cpp
508 lines (430 loc) · 13.9 KB
/
quantization.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
// Aseprite Render Library
// Copyright (c) 2019-2022 Igara Studio S.A.
// 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 "render/quantization.h"
#include "doc/image_impl.h"
#include "doc/layer.h"
#include "doc/octree_map.h"
#include "doc/palette.h"
#include "doc/primitives.h"
#include "doc/remap.h"
#include "doc/sprite.h"
#include "render/dithering.h"
#include "render/error_diffusion.h"
#include "render/ordered_dither.h"
#include "render/render.h"
#include "render/task_delegate.h"
#include <algorithm>
#include <limits>
#include <map>
#include <memory>
#include <vector>
namespace render {
using namespace doc;
using namespace gfx;
Palette* create_palette_from_sprite(
const Sprite* sprite,
const frame_t fromFrame,
const frame_t toFrame,
const bool withAlpha,
Palette* palette,
TaskDelegate* delegate,
const bool newBlend,
RgbMapAlgorithm mapAlgo,
const bool calculateWithTransparent)
{
if (mapAlgo == doc::RgbMapAlgorithm::DEFAULT)
mapAlgo = doc::RgbMapAlgorithm::OCTREE;
PaletteOptimizer optimizer;
OctreeMap octreemap;
// Transparent color is needed if we have transparent layers
int maskIndex;
if ((sprite->backgroundLayer() && sprite->allLayersCount() == 1) ||
!calculateWithTransparent)
maskIndex = -1;
else if (sprite->colorMode() == ColorMode::INDEXED)
maskIndex = sprite->transparentColor();
else {
ASSERT(sprite->transparentColor() == 0);
maskIndex = 0; // For RGB/Grayscale images we use index 0 as the transparent index by default
}
// TODO check if how this is used in OctreeMap, if as a RGBA value
// or as an index (here we are using it as an index).
const color_t maskColor = (sprite->backgroundLayer()
&& sprite->allLayersCount() == 1) ? DOC_OCTREE_IS_OPAQUE:
sprite->transparentColor();
if (!palette)
palette = new Palette(fromFrame, 256);
// Add a flat image with the current sprite's frame rendered
ImageRef flat_image(Image::create(IMAGE_RGB,
sprite->width(), sprite->height()));
render::Render render;
render.setNewBlend(newBlend);
// Feed the optimizer with all rendered frames
for (frame_t frame=fromFrame; frame<=toFrame; ++frame) {
render.renderSprite(flat_image.get(), sprite, frame);
switch (mapAlgo) {
case RgbMapAlgorithm::RGB5A3:
optimizer.feedWithImage(flat_image.get(), withAlpha);
break;
case RgbMapAlgorithm::OCTREE:
octreemap.feedWithImage(flat_image.get(), withAlpha, maskColor);
break;
default:
ASSERT(false);
break;
}
if (delegate) {
if (!delegate->continueTask())
return nullptr;
delegate->notifyTaskProgress(
double(frame-fromFrame+1) / double(toFrame-fromFrame+1));
}
}
switch (mapAlgo) {
case RgbMapAlgorithm::RGB5A3: {
// Generate an optimized palette
optimizer.calculate(palette, maskIndex);
break;
}
case RgbMapAlgorithm::OCTREE:
// TODO check calculateWithTransparent flag
if (!octreemap.makePalette(palette, palette->size())) {
// We can use an 8-bit deep octree map, instead of 7-bit of the
// first attempt.
octreemap = OctreeMap();
for (frame_t frame=fromFrame; frame<=toFrame; ++frame) {
render.renderSprite(flat_image.get(), sprite, frame);
octreemap.feedWithImage(flat_image.get(), withAlpha, maskColor , 8);
if (delegate) {
if (!delegate->continueTask())
return nullptr;
delegate->notifyTaskProgress(
double(frame-fromFrame+1) / double(toFrame-fromFrame+1));
}
}
octreemap.makePalette(palette, palette->size(), 8);
}
break;
}
return palette;
}
Image* convert_pixel_format(
const Image* image,
Image* new_image,
PixelFormat pixelFormat,
const Dithering& dithering,
const RgbMap* rgbmap,
const Palette* palette,
bool is_background,
color_t new_mask_color,
rgba_to_graya_func toGray,
TaskDelegate* delegate)
{
if (!new_image)
new_image = Image::create(pixelFormat, image->width(), image->height());
new_image->setMaskColor(new_mask_color);
// RGB -> Indexed with ordered dithering
if (image->pixelFormat() == IMAGE_RGB &&
pixelFormat == IMAGE_INDEXED &&
dithering.algorithm() != DitheringAlgorithm::None) {
std::unique_ptr<DitheringAlgorithmBase> dither;
switch (dithering.algorithm()) {
case DitheringAlgorithm::Ordered:
dither.reset(new OrderedDither2(is_background ? -1: new_mask_color));
break;
case DitheringAlgorithm::Old:
dither.reset(new OrderedDither(is_background ? -1: new_mask_color));
break;
case DitheringAlgorithm::ErrorDiffusion:
dither.reset(new ErrorDiffusionDither(is_background ? -1: new_mask_color));
break;
}
if (dither)
dither_rgb_image_to_indexed(
*dither, dithering,
image, new_image, rgbmap, palette, delegate);
return new_image;
}
// RGB/Indexed -> Gray
if ((image->pixelFormat() == IMAGE_RGB ||
image->pixelFormat() == IMAGE_INDEXED) &&
new_image->pixelFormat() == IMAGE_GRAYSCALE) {
if (!toGray)
toGray = &rgba_to_graya_using_luma;
}
color_t c;
int r, g, b, a;
switch (image->pixelFormat()) {
case IMAGE_RGB: {
const LockImageBits<RgbTraits> srcBits(image);
auto src_it = srcBits.begin(), src_end = srcBits.end();
switch (new_image->pixelFormat()) {
// RGB -> RGB
case IMAGE_RGB:
new_image->copy(image, gfx::Clip(image->bounds()));
break;
// RGB -> Grayscale
case IMAGE_GRAYSCALE: {
LockImageBits<GrayscaleTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
ASSERT(toGray);
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
*dst_it = (*toGray)(*src_it);
}
ASSERT(dst_it == dst_end);
break;
}
// RGB -> Indexed
case IMAGE_INDEXED: {
LockImageBits<IndexedTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
c = *src_it;
r = rgba_getr(c);
g = rgba_getg(c);
b = rgba_getb(c);
a = rgba_geta(c);
if (a == 0)
*dst_it = (new_mask_color == -1? 0 : new_mask_color);
else if (rgbmap)
*dst_it = rgbmap->mapColor(c);
else
*dst_it = palette->findBestfit(r, g, b, a, new_mask_color);
}
ASSERT(dst_it == dst_end);
break;
}
}
break;
}
case IMAGE_GRAYSCALE: {
const LockImageBits<GrayscaleTraits> srcBits(image);
auto src_it = srcBits.begin(), src_end = srcBits.end();
switch (new_image->pixelFormat()) {
// Grayscale -> RGB
case IMAGE_RGB: {
LockImageBits<RgbTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
c = *src_it;
g = graya_getv(c);
*dst_it = rgba(g, g, g, graya_geta(c));
}
ASSERT(dst_it == dst_end);
break;
}
// Grayscale -> Grayscale
case IMAGE_GRAYSCALE:
new_image->copy(image, gfx::Clip(image->bounds()));
break;
// Grayscale -> Indexed
case IMAGE_INDEXED: {
LockImageBits<IndexedTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
c = *src_it;
a = graya_geta(c);
c = graya_getv(c);
if (a == 0)
*dst_it = (new_mask_color == -1? 0 : new_mask_color);
else if (rgbmap)
*dst_it = rgbmap->mapColor(c, c, c, a);
else
*dst_it = palette->findBestfit(c, c, c, a, new_mask_color);
}
ASSERT(dst_it == dst_end);
break;
}
}
break;
}
case IMAGE_INDEXED: {
const LockImageBits<IndexedTraits> srcBits(image);
auto src_it = srcBits.begin(), src_end = srcBits.end();
switch (new_image->pixelFormat()) {
// Indexed -> RGB
case IMAGE_RGB: {
LockImageBits<RgbTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
c = *src_it;
if (!is_background && c == image->maskColor())
*dst_it = rgba(0, 0, 0, 0);
else {
const uint32_t p = palette->getEntry(c);
if (is_background)
*dst_it = rgba(rgba_getr(p), rgba_getg(p), rgba_getb(p), 255);
else
*dst_it = p;
}
}
ASSERT(dst_it == dst_end);
break;
}
// Indexed -> Grayscale
case IMAGE_GRAYSCALE: {
LockImageBits<GrayscaleTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
ASSERT(toGray);
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
c = *src_it;
if (!is_background && c == image->maskColor())
*dst_it = graya(0, 0);
else {
c = palette->getEntry(c);
*dst_it = (*toGray)(c);
}
}
ASSERT(dst_it == dst_end);
break;
}
// Indexed -> Indexed
case IMAGE_INDEXED: {
LockImageBits<IndexedTraits> dstBits(new_image, Image::WriteLock);
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto dst_end = dstBits.end();
#endif
for (; src_it != src_end; ++src_it, ++dst_it) {
ASSERT(dst_it != dst_end);
c = *src_it;
if (!is_background && c == image->maskColor())
*dst_it = new_mask_color;
else {
c = palette->getEntry(c);
r = rgba_getr(c);
g = rgba_getg(c);
b = rgba_getb(c);
a = rgba_geta(c);
if (rgbmap)
*dst_it = rgbmap->mapColor(r, g, b, a);
else
*dst_it = palette->findBestfit(r, g, b, a, new_mask_color);
}
}
ASSERT(dst_it == dst_end);
break;
}
}
break;
}
}
return new_image;
}
//////////////////////////////////////////////////////////////////////
// Creation of optimized palette for RGB images
// by David Capello
void PaletteOptimizer::feedWithImage(const Image* image,
const bool withAlpha)
{
feedWithImage(image, image->bounds(), withAlpha);
}
void PaletteOptimizer::feedWithImage(const Image* image,
const gfx::Rect& bounds,
const bool withAlpha)
{
uint32_t color;
if (withAlpha)
m_withAlpha = true;
ASSERT(image);
switch (image->pixelFormat()) {
case IMAGE_RGB:
{
const LockImageBits<RgbTraits> bits(image, bounds);
auto it = bits.begin(), end = bits.end();
for (; it != end; ++it) {
color = *it;
if (rgba_geta(color) > 0) {
if (!withAlpha)
color |= rgba(0, 0, 0, 255);
m_histogram.addSamples(color, 1);
}
}
}
break;
case IMAGE_GRAYSCALE:
{
const LockImageBits<GrayscaleTraits> bits(image, bounds);
auto it = bits.begin(), end = bits.end();
for (; it != end; ++it) {
color = *it;
if (graya_geta(color) > 0) {
if (!withAlpha)
color = graya(graya_getv(color), 255);
m_histogram.addSamples(rgba(graya_getv(color),
graya_getv(color),
graya_getv(color),
graya_geta(color)), 1);
}
}
}
break;
case IMAGE_INDEXED:
ASSERT(false);
break;
}
}
void PaletteOptimizer::feedWithRgbaColor(color_t color)
{
m_histogram.addSamples(color, 1);
}
void PaletteOptimizer::calculate(Palette* palette, int maskIndex)
{
bool addMask;
if ((palette->size() > 1) &&
(maskIndex >= 0 && maskIndex < palette->size())) {
palette->resize(palette->size()-1);
addMask = true;
}
else
addMask = false;
// If the sprite has a background layer, the first entry can be
// used, in other case the 0 indexed will be the mask color, so it
// will not be used later in the color conversion (from RGB to
// Indexed).
int usedColors = m_histogram.createOptimizedPalette(palette);
if (addMask) {
palette->resize(usedColors+1);
Remap remap(palette->size());
for (int i=0; i<usedColors; ++i)
remap.map(i, i + (i >= maskIndex ? 1: 0));
palette->applyRemap(remap);
if (maskIndex < palette->size())
palette->setEntry(maskIndex, rgba(0, 0, 0, (m_withAlpha ? 0: 255)));
}
else {
palette->resize(std::max(1, usedColors));
}
}
} // namespace render