Repository navigation
Expand file tree
/
Copy pathWholeScaleRows.cpp
More file actions
129 lines (118 loc) · 4.46 KB
/
Copy pathWholeScaleRows.cpp
File metadata and controls
129 lines (118 loc) · 4.46 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
#include "WholeScaleRows.h"
#include <algorithm>
#include "PixelFormat.h"
namespace QuadBlit
{
namespace
{
using DekiPixel::Div255;
using DekiPixel::SrcKind;
// One target row of a source row drawn at scaleX: columns [startX, endX).
template <SrcKind SK>
void WholeScaleRow(const uint8_t* srcRow, int32_t bpp, bool hasAlpha, int32_t scaleX, uint32_t* row, int32_t destX,
int32_t startX, int32_t endX)
{
int32_t sx = (startX - destX) / scaleX;
int32_t x = startX;
while (x < endX)
{
// The target columns source column sx covers, inside the clip.
const int32_t blockEnd = std::min(endX, destX + (sx + 1) * scaleX);
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
DekiPixel::ReadSrcPixel<SK>(srcRow + static_cast<size_t>(sx) * bpp, hasAlpha, r, g, b, a);
if (a == 255)
{
const uint32_t opaque =
(0xFFu << 24) | (static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | b;
for (; x < blockEnd; ++x)
{
row[x] = opaque;
}
}
else if (a != 0)
{
// As the pipeline's plain composite: the target keeps no alpha.
const uint32_t invA = 255u - a;
for (; x < blockEnd; ++x)
{
const uint32_t v = row[x];
const uint32_t bgR = (v >> 16) & 0xFF;
const uint32_t bgG = (v >> 8) & 0xFF;
const uint32_t bgB = v & 0xFF;
row[x] = (0xFFu << 24) | (static_cast<uint32_t>(Div255(r * a + bgR * invA)) << 16) |
(static_cast<uint32_t>(Div255(g * a + bgG * invA)) << 8) | Div255(b * a + bgB * invA);
}
}
x = blockEnd;
++sx;
}
}
template <SrcKind SK>
void WholeScaleRows(const Source& source, uint32_t* target, int32_t targetWidth, int32_t destX, int32_t destY,
int32_t scaleX, int32_t scaleY, int32_t startX, int32_t startY, int32_t endX, int32_t endY)
{
const int32_t bpp = source.bytesPerPixel;
const int32_t stride = source.stride > 0 ? source.stride : source.width * bpp;
for (int32_t y = startY; y < endY; ++y)
{
const uint8_t* srcRow = source.pixels + static_cast<size_t>((y - destY) / scaleY) * stride;
WholeScaleRow<SK>(srcRow, bpp, source.hasAlpha, scaleX, target + static_cast<size_t>(y) * targetWidth, destX,
startX, endX);
}
}
SrcKind KindOfSource(const Source& s)
{
if (s.isRGB565)
{
return s.bytesPerPixel >= 3 ? SrcKind::RGB565A8 : SrcKind::RGB565;
}
if (s.bytesPerPixel == 4)
{
return SrcKind::RGBA8888;
}
if (s.bytesPerPixel == 3)
{
return SrcKind::RGB888;
}
return SrcKind::ALPHA8;
}
} // namespace
bool WholeScaleRows_ARGB8888(const Source& source, uint32_t* target, int32_t targetWidth, int32_t destX, int32_t destY,
int32_t destWidth, int32_t destHeight, int32_t startX, int32_t startY, int32_t endX,
int32_t endY)
{
const int32_t scaleX = WholeScale(source.width, destWidth);
const int32_t scaleY = WholeScale(source.height, destHeight);
if (scaleX == 0 || scaleY == 0)
{
return false;
}
switch (KindOfSource(source))
{
case SrcKind::RGBA8888:
WholeScaleRows<SrcKind::RGBA8888>(source, target, targetWidth, destX, destY, scaleX, scaleY, startX, startY,
endX, endY);
break;
case SrcKind::RGB888:
WholeScaleRows<SrcKind::RGB888>(source, target, targetWidth, destX, destY, scaleX, scaleY, startX, startY,
endX, endY);
break;
case SrcKind::RGB565:
WholeScaleRows<SrcKind::RGB565>(source, target, targetWidth, destX, destY, scaleX, scaleY, startX, startY,
endX, endY);
break;
case SrcKind::RGB565A8:
WholeScaleRows<SrcKind::RGB565A8>(source, target, targetWidth, destX, destY, scaleX, scaleY, startX, startY,
endX, endY);
break;
case SrcKind::ALPHA8:
WholeScaleRows<SrcKind::ALPHA8>(source, target, targetWidth, destX, destY, scaleX, scaleY, startX, startY,
endX, endY);
break;
}
return true;
}
} // namespace QuadBlit