forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blend_image.cpp
84 lines (72 loc) · 3.02 KB
/
blend_image.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
// Aseprite Document Library
// Copyright (c) 2024 Igara Studio S.A.
//
// 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 "doc/blend_image.h"
#include "doc/blend_internals.h"
#include "doc/image_impl.h"
namespace doc {
template<typename DstTraits,
typename SrcTraits>
void blend_image_templ(Image* dst,
const Image* src,
const gfx::Clip& area,
const Palette* pal,
const int opacity,
const BlendMode blendMode)
{
if constexpr (DstTraits::color_mode == ColorMode::INDEXED ||
SrcTraits::color_mode == ColorMode::INDEXED) {
ASSERT(pal != nullptr);
if (pal == nullptr)
return;
}
BlenderHelper<DstTraits, SrcTraits> blender(dst, src, pal, blendMode, true);
LockImageBits<DstTraits> dstBits(dst);
const LockImageBits<SrcTraits> srcBits(src);
auto dstIt = dstBits.begin_area(area.dstBounds());
auto srcIt = srcBits.begin_area(area.srcBounds());
auto dstEnd = dstBits.end_area(area.dstBounds());
for (; dstIt < dstEnd; ++dstIt, ++srcIt)
*dstIt = blender(*dstIt, *srcIt, opacity);
}
void blend_image(Image* dst,
const Image* src,
gfx::Clip area,
const Palette* pal,
const int opacity,
const BlendMode blendMode)
{
if (!area.clip(dst->width(), dst->height(), src->width(), src->height()))
return;
switch (dst->pixelFormat()) {
case IMAGE_RGB:
switch (src->pixelFormat()) {
case IMAGE_RGB: return blend_image_templ<RgbTraits, RgbTraits >(dst, src, area, pal, opacity, blendMode);
case IMAGE_GRAYSCALE: return blend_image_templ<RgbTraits, GrayscaleTraits>(dst, src, area, pal, opacity, blendMode);
case IMAGE_INDEXED: return blend_image_templ<RgbTraits, IndexedTraits >(dst, src, area, pal, opacity, blendMode);
}
break;
case IMAGE_GRAYSCALE:
switch (src->pixelFormat()) {
case IMAGE_RGB: return blend_image_templ<GrayscaleTraits, RgbTraits >(dst, src, area, pal, opacity, blendMode);
case IMAGE_GRAYSCALE: return blend_image_templ<GrayscaleTraits, GrayscaleTraits>(dst, src, area, pal, opacity, blendMode);
case IMAGE_INDEXED: return blend_image_templ<GrayscaleTraits, IndexedTraits >(dst, src, area, pal, opacity, blendMode);
}
break;
case IMAGE_INDEXED:
switch (src->pixelFormat()) {
case IMAGE_RGB: return blend_image_templ<IndexedTraits, RgbTraits >(dst, src, area, pal, opacity, blendMode);
case IMAGE_GRAYSCALE: return blend_image_templ<IndexedTraits, GrayscaleTraits>(dst, src, area, pal, opacity, blendMode);
case IMAGE_INDEXED: return blend_image_templ<IndexedTraits, IndexedTraits >(dst, src, area, pal, opacity, blendMode);
}
break;
case IMAGE_TILEMAP:
return dst->copy(src, area);
}
}
} // namespace doc