forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.cpp
1434 lines (1244 loc) · 41.4 KB
/
render.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Aseprite Render Library
// Copyright (C) 2019-2024 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/render.h"
#include "doc/blend_internals.h"
#include "doc/blend_mode.h"
#include "doc/doc.h"
#include "doc/image_impl.h"
#include "doc/layer_tilemap.h"
#include "doc/playback.h"
#include "doc/render_plan.h"
#include "doc/tileset.h"
#include "doc/tilesets.h"
#include "gfx/clip.h"
#include "gfx/region.h"
#include <cmath>
#define TRACE_RENDER_CEL(...) // TRACE
namespace render {
namespace {
//////////////////////////////////////////////////////////////////////
// Scaled composite
template<class DstTraits, class SrcTraits>
void composite_image_without_scale(
Image* dst, const Image* src, const Palette* pal,
const gfx::ClipF& areaF,
const int opacity,
const BlendMode blendMode,
const double sx,
const double sy,
const bool newBlend,
const tile_flags) // Ignored
{
ASSERT(dst);
ASSERT(src);
ASSERT(DstTraits::pixel_format == dst->pixelFormat());
ASSERT(SrcTraits::pixel_format == src->pixelFormat());
BlenderHelper<DstTraits, SrcTraits> blender(dst, src, pal, blendMode, newBlend);
gfx::Clip area(areaF);
if (!area.clip(dst->width(), dst->height(),
src->width(), src->height()))
return;
gfx::Rect srcBounds = area.srcBounds();
gfx::Rect dstBounds = area.dstBounds();
int bottom = dstBounds.y2()-1;
ASSERT(!srcBounds.isEmpty());
// Lock all necessary bits
const LockImageBits<SrcTraits> srcBits(src, srcBounds);
LockImageBits<DstTraits> dstBits(dst, dstBounds);
auto src_it = srcBits.begin();
#ifdef _DEBUG
auto src_end = srcBits.end();
#endif
typename LockImageBits<DstTraits>::iterator dst_it, dst_end;
// For each line to draw of the source image...
dstBounds.h = 1;
for (int y=0; y<srcBounds.h; ++y) {
dst_it = dstBits.begin_area(dstBounds);
dst_end = dstBits.end_area(dstBounds);
for (int x=0; x<srcBounds.w; ++x) {
ASSERT(src_it >= srcBits.begin() && src_it < src_end);
ASSERT(dst_it >= dstBits.begin() && dst_it < dst_end);
*dst_it = blender(*dst_it, *src_it, opacity);
++src_it;
++dst_it;
}
if (++dstBounds.y > bottom)
break;
}
}
template<class DstTraits, class SrcTraits>
void composite_image_scale_up(
Image* dst, const Image* src, const Palette* pal,
const gfx::ClipF& areaF,
const int opacity,
const BlendMode blendMode,
const double sx,
const double sy,
const bool newBlend,
const tile_flags) // Ignored
{
ASSERT(dst);
ASSERT(src);
ASSERT(DstTraits::pixel_format == dst->pixelFormat());
ASSERT(SrcTraits::pixel_format == src->pixelFormat());
gfx::Clip area(areaF);
if (!area.clip(dst->width(), dst->height(),
int(sx*double(src->width())),
int(sy*double(src->height()))))
return;
BlenderHelper<DstTraits, SrcTraits> blender(dst, src, pal, blendMode, newBlend);
int px_x, px_y;
int px_w = int(sx);
int px_h = int(sy);
// We've received crash reports about these values being 0 when it's
// called from Render::renderImage() when the projection is scaled
// to the cel bounds (this can happen only when a reference layer is
// scaled, but when a reference layer is visible we shouldn't be
// here, we should be using the composite_image_general(), see the
// "finegrain" var in Render::getImageComposition()).
ASSERT(px_w > 0);
ASSERT(px_h > 0);
if (px_w <= 0 || px_h <= 0)
return;
int first_px_w = px_w - (area.src.x % px_w);
int first_px_h = px_h - (area.src.y % px_h);
gfx::Rect srcBounds = area.srcBounds();
srcBounds.w = (srcBounds.x+srcBounds.w)/px_w - srcBounds.x/px_w;
srcBounds.h = (srcBounds.y+srcBounds.h)/px_h - srcBounds.y/px_h;
srcBounds.x /= px_w;
srcBounds.y /= px_h;
if ((area.src.x+area.size.w) % px_w > 0) ++srcBounds.w;
if ((area.src.y+area.size.h) % px_h > 0) ++srcBounds.h;
if (srcBounds.isEmpty())
return;
gfx::Rect dstBounds = area.dstBounds();
int bottom = dstBounds.y2()-1;
int line_h;
// the scanline variable is used to blend src/dst pixels one time for each pixel
typedef std::vector<typename DstTraits::pixel_t> Scanline;
Scanline scanline(srcBounds.w);
typename Scanline::iterator scanline_it;
#ifdef _DEBUG
typename Scanline::iterator scanline_end = scanline.end();
#endif
// Lock all necessary bits
const LockImageBits<SrcTraits> srcBits(src, srcBounds);
LockImageBits<DstTraits> dstBits(dst, dstBounds);
typename LockImageBits<SrcTraits>::const_iterator src_it = srcBits.begin();
#ifdef _DEBUG
typename LockImageBits<SrcTraits>::const_iterator src_end = srcBits.end();
#endif
typename LockImageBits<DstTraits>::iterator dst_it, dst_end;
// For each line to draw of the source image...
dstBounds.h = 1;
for (int y=0; y<srcBounds.h; ++y) {
dst_it = dstBits.begin_area(dstBounds);
dst_end = dstBits.end_area(dstBounds);
// Read 'src' and 'dst' and blend them, put the result in `scanline'
scanline_it = scanline.begin();
for (int x=0; x<srcBounds.w; ++x) {
ASSERT(src_it >= srcBits.begin() && src_it < src_end);
ASSERT(dst_it >= dstBits.begin() && dst_it < dst_end);
ASSERT(scanline_it >= scanline.begin() && scanline_it < scanline_end);
*scanline_it = blender(*dst_it, *src_it, opacity);
++src_it;
int delta;
if (x == 0)
delta = first_px_w;
else
delta = px_w;
while (dst_it != dst_end && delta-- > 0)
++dst_it;
++scanline_it;
}
// Get the 'height' of the line to be painted in 'dst'
if ((y == 0) && (first_px_h > 0))
line_h = first_px_h;
else
line_h = px_h;
// Draw the line in 'dst'
for (px_y=0; px_y<line_h; ++px_y) {
dst_it = dstBits.begin_area(dstBounds);
dst_end = dstBits.end_area(dstBounds);
scanline_it = scanline.begin();
int x = 0;
// first pixel
for (px_x=0; px_x<first_px_w; ++px_x) {
ASSERT(scanline_it != scanline_end);
ASSERT(dst_it != dst_end);
*dst_it = *scanline_it;
++dst_it;
if (dst_it == dst_end)
goto done_with_line;
}
++scanline_it;
++x;
// the rest of the line
for (; x<srcBounds.w; ++x) {
for (px_x=0; px_x<px_w; ++px_x) {
ASSERT(dst_it != dst_end);
*dst_it = *scanline_it;
++dst_it;
if (dst_it == dst_end)
goto done_with_line;
}
++scanline_it;
}
done_with_line:;
if (++dstBounds.y > bottom)
goto done_with_blit;
}
}
done_with_blit:;
}
template<class DstTraits, class SrcTraits>
void composite_image_scale_down(
Image* dst, const Image* src, const Palette* pal,
const gfx::ClipF& areaF,
const int opacity,
const BlendMode blendMode,
const double sx,
const double sy,
const bool newBlend,
const tile_flags) // Ignored
{
ASSERT(dst);
ASSERT(src);
ASSERT(DstTraits::pixel_format == dst->pixelFormat());
ASSERT(SrcTraits::pixel_format == src->pixelFormat());
gfx::Clip area(areaF);
if (!area.clip(dst->width(), dst->height(),
int(sx*double(src->width())),
int(sy*double(src->height()))))
return;
BlenderHelper<DstTraits, SrcTraits> blender(dst, src, pal, blendMode, newBlend);
int step_w = int(1.0 / sx);
int step_h = int(1.0 / sy);
if (step_w < 1 || step_h < 1)
return;
gfx::Rect srcBounds = area.srcBounds();
srcBounds.w = (srcBounds.x+srcBounds.w)*step_w - srcBounds.x*step_w;
srcBounds.h = (srcBounds.y+srcBounds.h)*step_h - srcBounds.y*step_h;
srcBounds.x *= step_w;
srcBounds.y *= step_h;
if (srcBounds.isEmpty())
return;
gfx::Rect dstBounds = area.dstBounds();
// Lock all necessary bits
const LockImageBits<SrcTraits> srcBits(src, srcBounds);
LockImageBits<DstTraits> dstBits(dst, dstBounds);
auto src_it = srcBits.begin();
auto dst_it = dstBits.begin();
#ifdef _DEBUG
auto src_end = srcBits.end();
auto dst_end = dstBits.end();
#endif
// Adjust to src_it for each line
int adjust_per_line = (dstBounds.w*step_w)*(step_h-1);
// For each line to draw of the source image...
for (int y=0; y<dstBounds.h; ++y) {
for (int x=0; x<dstBounds.w; ++x) {
ASSERT(src_it >= srcBits.begin() && src_it < src_end);
ASSERT(dst_it >= dstBits.begin() && dst_it < dst_end);
*dst_it = blender(*dst_it, *src_it, opacity);
// Skip columns
src_it += step_w;
++dst_it;
}
// Skip rows
src_it += adjust_per_line;
}
}
template<class DstTraits, class SrcTraits>
void composite_image_general(
Image* dst, const Image* src, const Palette* pal,
const gfx::ClipF& areaF,
const int opacity,
const BlendMode blendMode,
const double sx,
const double sy,
const bool newBlend,
const tile_flags) // Ignored
{
ASSERT(dst);
ASSERT(src);
ASSERT(DstTraits::pixel_format == dst->pixelFormat());
ASSERT(SrcTraits::pixel_format == src->pixelFormat());
gfx::ClipF area(areaF);
if (!area.clip(dst->width(), dst->height(),
sx*src->width(), sy*src->height()))
return;
BlenderHelper<DstTraits, SrcTraits> blender(dst, src, pal, blendMode, newBlend);
gfx::Rect dstBounds(
area.dstBounds().x, area.dstBounds().y,
int(std::ceil(area.dstBounds().w)),
int(std::ceil(area.dstBounds().h)));
gfx::RectF srcBounds = area.srcBounds();
dstBounds &= dst->bounds();
int dstY = dstBounds.y;
double srcXStart = srcBounds.x / sx;
double srcXDelta = 1.0 / sx;
int srcWidth = src->width();
for (int y=0; y<dstBounds.h; ++y, ++dstY) {
int srcY = int((srcBounds.y+double(y)) / sy);
double srcX = srcXStart;
int oldSrcX;
// Out of bounds
if (srcY >= src->height())
break;
ASSERT(srcY >= 0 && srcY < src->height());
ASSERT(dstY >= 0 && dstY < dst->height());
auto dstPtr = get_pixel_address_fast<DstTraits>(dst, dstBounds.x, dstY);
auto srcPtr = get_pixel_address_fast<SrcTraits>(src, int(srcX), srcY);
#if _DEBUG
int dstX = dstBounds.x;
#endif
for (int x=0; x<dstBounds.w; ++dstPtr) {
ASSERT(dstX >= 0 && dstX < dst->width());
ASSERT(srcX >= 0 && srcX < src->width());
*dstPtr = blender(*dstPtr, *srcPtr, opacity);
++x;
oldSrcX = int(srcX);
srcX = srcXStart + srcXDelta*x;
// Out of bounds
if (srcX >= srcWidth)
break;
srcPtr += int(srcX - oldSrcX);
#if _DEBUG
++dstX;
#endif
}
}
}
template<class DstTraits, class SrcTraits>
void composite_image_general_with_tile_flags(
Image* dst, const Image* src, const Palette* pal,
const gfx::ClipF& areaF,
const int opacity,
const BlendMode blendMode,
const double sx,
const double sy,
const bool newBlend,
const tile_flags tileFlags)
{
ASSERT(dst);
ASSERT(src);
ASSERT(DstTraits::pixel_format == dst->pixelFormat());
ASSERT(SrcTraits::pixel_format == src->pixelFormat());
gfx::ClipF area(areaF);
if (!area.clip(dst->width(), dst->height(),
sx*src->width(), sy*src->height()))
return;
BlenderHelper<DstTraits, SrcTraits> blender(dst, src, pal, blendMode, newBlend);
gfx::Rect dstBounds(
area.dstBounds().x, area.dstBounds().y,
int(std::ceil(area.dstBounds().w)),
int(std::ceil(area.dstBounds().h)));
gfx::Rect srcBounds = area.srcBounds();
const gfx::Rect srcImgBounds = src->bounds();
const gfx::Size srcMinSize = src->size();
dstBounds &= dst->bounds();
if (tileFlags & tile_f_xflip) {
srcBounds.x = sx*srcImgBounds.x2() - srcBounds.x2();
}
if (tileFlags & tile_f_yflip) {
srcBounds.y = sy*srcImgBounds.y2() - srcBounds.y2();
}
int dstY = dstBounds.y;
for (int y=0; y<dstBounds.h; ++y, ++dstY) {
ASSERT(dstY >= 0 && dstY < dst->height());
auto dstPtr = get_pixel_address_fast<DstTraits>(dst, dstBounds.x, dstY);
#if _DEBUG
int dstX = dstBounds.x;
#endif
for (int x=0; x<dstBounds.w; ++x, ++dstPtr) {
int srcX;
int srcY;
if (tileFlags & tile_f_xflip) {
srcX = (srcBounds.x2()-1-x) / sx;
}
else {
srcX = (srcBounds.x+x) / sx;
}
if (tileFlags & tile_f_yflip) {
srcY = (srcBounds.y2()-1-y) / sy;
}
else {
srcY = (srcBounds.y+y) / sy;
}
gfx::Size minSize;
if (tileFlags & tile_f_dflip) {
std::swap(srcX, srcY);
minSize.w = minSize.h = std::min(srcMinSize.w, srcMinSize.h);
}
else {
minSize = srcMinSize;
}
ASSERT(dstX >= 0 && dstX < dst->width());
if (srcX >= 0 && srcX < minSize.w &&
srcY >= 0 && srcY < minSize.h) {
auto srcPtr = get_pixel_address_fast<SrcTraits>(src, srcX, srcY);
*dstPtr = blender(*dstPtr, *srcPtr, opacity);
}
else {
*dstPtr = 0;
}
#if _DEBUG
++dstX;
#endif
}
}
}
template<class DstTraits, class SrcTraits>
CompositeImageFunc get_fastest_composition_path(const Projection& proj,
const bool finegrain,
const tile_flags tileFlags)
{
if (tileFlags) {
return composite_image_general_with_tile_flags<DstTraits, SrcTraits>;
}
else if (finegrain || !proj.zoom().isSimpleZoomLevel()) {
return composite_image_general<DstTraits, SrcTraits>;
}
else if (proj.applyX(1) == 1 && proj.applyY(1) == 1) {
return composite_image_without_scale<DstTraits, SrcTraits>;
}
else if (proj.scaleX() >= 1.0 && proj.scaleY() >= 1.0) {
return composite_image_scale_up<DstTraits, SrcTraits>;
}
// Slower composite function for special cases with odd zoom and non-square pixel ratio
else if (((proj.removeX(1) > 1) && (proj.removeX(1) & 1)) ||
((proj.removeY(1) > 1) && (proj.removeY(1) & 1))) {
return composite_image_general<DstTraits, SrcTraits>;
}
else {
return composite_image_scale_down<DstTraits, SrcTraits>;
}
}
bool has_visible_reference_layers(const LayerGroup* group)
{
for (const Layer* child : group->layers()) {
if (!child->isVisible())
continue;
if (child->isReference())
return true;
if (child->isGroup() &&
has_visible_reference_layers(static_cast<const LayerGroup*>(child)))
return true;
}
return false;
}
} // anonymous namespace
Render::Render()
: m_flags(0)
, m_nonactiveLayersOpacity(255)
, m_sprite(nullptr)
, m_currentLayer(nullptr)
, m_currentFrame(0)
, m_extraType(ExtraType::NONE)
, m_extraCel(nullptr)
, m_extraImage(nullptr)
, m_newBlendMethod(true)
, m_globalOpacity(255)
, m_selectedLayerForOpacity(nullptr)
, m_selectedLayer(nullptr)
, m_selectedFrame(-1)
, m_previewImage(nullptr)
, m_previewTileset(nullptr)
, m_previewBlendMode(BlendMode::NORMAL)
, m_onionskin(OnionskinType::NONE)
{
}
void Render::setRefLayersVisiblity(const bool visible)
{
if (visible)
m_flags |= Flags::ShowRefLayers;
else
m_flags &= ~Flags::ShowRefLayers;
}
void Render::setNonactiveLayersOpacity(const int opacity)
{
m_nonactiveLayersOpacity = opacity;
}
void Render::setNewBlend(const bool newBlend)
{
m_newBlendMethod = newBlend;
}
void Render::setProjection(const Projection& projection)
{
m_proj = projection;
}
void Render::setBgOptions(const BgOptions& bg)
{
m_bg = bg;
}
void Render::setSelectedLayer(const Layer* layer)
{
m_selectedLayerForOpacity = layer;
}
void Render::setPreviewImage(const Layer* layer,
const frame_t frame,
const Image* image,
const Tileset* tileset,
const gfx::Point& pos,
const BlendMode blendMode)
{
m_selectedLayer = layer;
m_selectedFrame = frame;
m_previewImage = image;
m_previewTileset = tileset;
m_previewPos = pos;
m_previewBlendMode = blendMode;
}
void Render::setExtraImage(
ExtraType type,
const Cel* cel, const Image* image, BlendMode blendMode,
const Layer* currentLayer,
frame_t currentFrame)
{
m_extraType = type;
m_extraCel = cel;
m_extraImage = image;
m_extraBlendMode = blendMode;
m_currentLayer = currentLayer;
m_currentFrame = currentFrame;
}
void Render::removePreviewImage()
{
m_previewImage = nullptr;
m_previewTileset = nullptr;
}
void Render::removeExtraImage()
{
m_extraType = ExtraType::NONE;
m_extraCel = nullptr;
}
void Render::setOnionskin(const OnionskinOptions& options)
{
m_onionskin = options;
}
void Render::disableOnionskin()
{
m_onionskin.type(OnionskinType::NONE);
}
void Render::renderSprite(
Image* dstImage,
const Sprite* sprite,
frame_t frame)
{
renderSprite(
dstImage, sprite, frame,
gfx::ClipF(sprite->bounds()));
}
void Render::renderLayer(
Image* dstImage,
const Layer* layer,
frame_t frame)
{
renderLayer(dstImage, layer, frame,
gfx::Clip(layer->sprite()->bounds()));
}
void Render::renderLayer(
Image* dstImage,
const Layer* layer,
frame_t frame,
const gfx::Clip& area,
BlendMode blendMode)
{
m_sprite = layer->sprite();
CompositeImageFunc compositeImage =
getImageComposition(
(dstImage->pixelFormat() != IMAGE_TILEMAP ? dstImage->pixelFormat():
m_sprite->pixelFormat()),
m_sprite->pixelFormat(), layer);
if (!compositeImage)
return;
m_globalOpacity = 255;
doc::RenderPlan plan;
plan.addLayer(layer, frame);
renderPlan(
plan, dstImage, area,
frame, compositeImage,
true, true, blendMode);
}
void Render::renderSprite(
Image* dstImage,
const Sprite* sprite,
frame_t frame,
const gfx::ClipF& area)
{
m_sprite = sprite;
CompositeImageFunc compositeImage =
getImageComposition(
dstImage->pixelFormat(),
m_sprite->pixelFormat(), sprite->root());
if (!compositeImage)
return;
const LayerImage* bgLayer = m_sprite->backgroundLayer();
color_t bg_color = 0;
if (m_sprite->pixelFormat() == IMAGE_INDEXED) {
switch (dstImage->pixelFormat()) {
case IMAGE_RGB:
case IMAGE_GRAYSCALE:
if (bgLayer && bgLayer->isVisible())
bg_color = m_sprite->palette(frame)->getEntry(m_sprite->transparentColor());
break;
case IMAGE_INDEXED:
bg_color = m_sprite->transparentColor();
break;
}
}
// New Blending Method:
if (m_newBlendMethod) {
// Clear dstImage with the bg_color (if the background is not a
// special background pattern like the checkered background, this
// is enough as a base color).
fill_rect(dstImage, area.dstBounds(), bg_color);
// Draw the Background layer - Onion skin behind the sprite - Transparent Layers
renderSpriteLayers(dstImage, area, frame, compositeImage);
// In case that we need a special background (e.g. like the
// checkered pattern), we can draw the background in a temporal
// image and then merge this temporal image with the dstImage.
if (!isSolidBackground(bgLayer, bg_color)) {
if (!m_tmpBuf)
m_tmpBuf.reset(new doc::ImageBuffer);
ImageRef tmpBackground(Image::create(dstImage->spec(), m_tmpBuf));
renderBackground(tmpBackground.get(), bgLayer, bg_color, area);
// Draws dstImage over the background on each pixel of dstImage
// with opacity is < 255 (the result is left on dstImage itself)
composite_image(dstImage, tmpBackground.get(), sprite->palette(frame),
0, 0, 255, BlendMode::DST_OVER);
}
}
// Old Blending Method:
else {
renderBackground(dstImage, bgLayer, bg_color, area);
renderSpriteLayers(dstImage, area, frame, compositeImage);
}
// Draw onion skin in front of the sprite.
if (m_onionskin.position() == OnionskinPosition::INFRONT)
renderOnionskin(dstImage, area, frame, compositeImage);
// Overlay preview image
if (m_previewImage &&
m_selectedLayer == nullptr &&
m_selectedFrame == frame) {
renderImage(
dstImage,
m_previewImage,
m_sprite->palette(frame),
gfx::Rect(m_previewPos.x, m_previewPos.y,
m_previewImage->width(),
m_previewImage->height()),
area,
getImageComposition(
dstImage->pixelFormat(),
m_previewImage->pixelFormat(),
sprite->root()),
255,
m_previewBlendMode);
}
}
void Render::renderSpriteLayers(Image* dstImage,
const gfx::ClipF& area,
frame_t frame,
CompositeImageFunc compositeImage)
{
doc::RenderPlan plan;
plan.addLayer(m_sprite->root(), frame);
// Draw the background layer.
m_globalOpacity = 255;
renderPlan(plan, dstImage,
area, frame, compositeImage,
true,
false,
BlendMode::UNSPECIFIED);
// Draw onion skin behind the sprite.
if (m_onionskin.position() == OnionskinPosition::BEHIND)
renderOnionskin(dstImage, area, frame, compositeImage);
// Draw the transparent layers.
m_globalOpacity = 255;
renderPlan(plan, dstImage,
area, frame, compositeImage,
false,
true,
BlendMode::UNSPECIFIED);
}
void Render::renderBackground(Image* image,
const Layer* bgLayer,
const color_t bg_color,
const gfx::ClipF& area)
{
if (isSolidBackground(bgLayer, bg_color)) {
fill_rect(image, area.dstBounds(), bg_color);
}
else {
switch (m_bg.type) {
case BgType::CHECKERED:
renderCheckeredBackground(image, area);
if (bgLayer && bgLayer->isVisible() &&
// TODO Review this: bg_color can be an index (not an rgba())
// when sprite and dstImage are indexed
rgba_geta(bg_color) > 0) {
blend_rect(image,
int(area.dst.x),
int(area.dst.y),
int(area.dst.x+area.size.w-1),
int(area.dst.y+area.size.h-1),
bg_color, 255);
}
break;
default:
ASSERT(false); // Invalid case, needsBackground() should
// return false in this case
break;
}
}
}
bool Render::isSolidBackground(
const Layer* bgLayer,
const color_t bg_color) const
{
return
((m_bg.type != BgType::CHECKERED) ||
(bgLayer && bgLayer->isVisible() &&
// TODO Review this: bg_color can be an index (not an rgba())
// when sprite and dstImage are indexed
rgba_geta(bg_color) == 255));
}
void Render::renderOnionskin(
Image* dstImage,
const gfx::Clip& area,
const frame_t frame,
const CompositeImageFunc compositeImage)
{
// Onion-skin feature: Draw previous/next frames with different
// opacity (<255)
if (m_onionskin.type() != OnionskinType::NONE) {
Tag* loop = m_onionskin.loopTag();
Layer* onionLayer = (m_onionskin.layer() ? m_onionskin.layer():
m_sprite->root());
Playback play(
m_sprite,
TagsList(), // TODO add an onionskin option to iterate subtags
frame,
loop ? Playback::PlayInLoop : Playback::PlayAll,
loop);
frame_t prevFrames = (loop ? m_onionskin.prevFrames():
std::min(frame, m_onionskin.prevFrames()));
play.nextFrame(-prevFrames);
for (frame_t frameOut = frame - prevFrames;
frameOut <= frame + m_onionskin.nextFrames();
++frameOut, play.nextFrame()) {
const frame_t frameIn = play.frame();
if (frameIn == frame ||
frameIn < 0 ||
frameIn > m_sprite->lastFrame()) {
continue;
}
if (frameOut < frame) {
m_globalOpacity = m_onionskin.opacityBase() - m_onionskin.opacityStep() * ((frame - frameOut)-1);
}
else {
m_globalOpacity = m_onionskin.opacityBase() - m_onionskin.opacityStep() * ((frameOut - frame)-1);
}
m_globalOpacity = std::clamp(m_globalOpacity, 0, 255);
if (m_globalOpacity > 0) {
BlendMode blendMode = BlendMode::UNSPECIFIED;
if (m_onionskin.type() == OnionskinType::MERGE)
blendMode = BlendMode::NORMAL;
else if (m_onionskin.type() == OnionskinType::RED_BLUE_TINT)
blendMode = (frameOut < frame ? BlendMode::RED_TINT: BlendMode::BLUE_TINT);
doc::RenderPlan plan;
plan.addLayer(onionLayer, frameIn);
renderPlan(
plan, dstImage,
area, frameIn, compositeImage,
// Render background only for "in-front" onion skinning and
// when opacity is < 255
(m_globalOpacity < 255 &&
m_onionskin.position() == OnionskinPosition::INFRONT),
true, blendMode);
}
}
}
}
void Render::renderCheckeredBackground(
Image* image,
const gfx::Clip& area)
{
int x, y, u, v;
int tile_w = m_bg.stripeSize.w;
int tile_h = m_bg.stripeSize.h;
if (m_bg.zoom) {
tile_w = m_proj.zoom().apply(tile_w);
tile_h = m_proj.zoom().apply(tile_h);
}
// Tile size
if (tile_w < 1) tile_w = 1;
if (tile_h < 1) tile_h = 1;
// Tile position (u,v) is the number of tile we start in "area.src" coordinate
u = (area.src.x / tile_w);
v = (area.src.y / tile_h);
// Position where we start drawing the first tile in "image"
int x_start = -(area.src.x % tile_w);
int y_start = -(area.src.y % tile_h);
gfx::Rect dstBounds = area.dstBounds();
// Fix background colors (make them opaque)
ASSERT(m_bg.colorPixelFormat == image->pixelFormat());
switch (m_bg.colorPixelFormat) {
case IMAGE_RGB:
m_bg.color1 |= doc::rgba_a_mask;
m_bg.color2 |= doc::rgba_a_mask;
break;
case IMAGE_GRAYSCALE:
m_bg.color1 |= doc::graya_a_mask;
m_bg.color2 |= doc::graya_a_mask;
break;
}
// Draw checkered background (tile by tile)
int u_start = u;
for (y=y_start-tile_h; y<image->height()+tile_h; y+=tile_h) {
for (x=x_start-tile_w; x<image->width()+tile_w; x+=tile_w) {
gfx::Rect fillRc = dstBounds.createIntersection(gfx::Rect(x, y, tile_w, tile_h));
if (!fillRc.isEmpty())
fill_rect(
image, fillRc.x, fillRc.y, fillRc.x+fillRc.w-1, fillRc.y+fillRc.h-1,
(((u+v))&1)? m_bg.color2: m_bg.color1);
++u;
}
u = u_start;
++v;
}
}
void Render::renderImage(
Image* dst_image,
const Image* src_image,
const Palette* pal,
const int x,
const int y,
const int opacity,
const BlendMode blendMode)
{
const tile_flags tileFlags = 0;
CompositeImageFunc compositeImage =
getImageComposition(
dst_image->pixelFormat(),
src_image->pixelFormat(), nullptr, tileFlags);
if (!compositeImage)
return;
compositeImage(
dst_image, src_image, pal,
gfx::ClipF(x, y, 0, 0,
m_proj.applyX(src_image->width()),
m_proj.applyY(src_image->height())),
opacity, blendMode,
m_proj.scaleX(),
m_proj.scaleY(),
m_newBlendMethod,
tileFlags);
}
void Render::renderPlan(
RenderPlan& plan,
Image* image,
const gfx::Clip& area,
const frame_t frame,
const CompositeImageFunc compositeImage,
const bool render_background,
const bool render_transparent,
const BlendMode blendMode)
{
for (const auto& item : plan.items()) {
const Cel* cel = item.cel;
const Layer* layer = item.layer;