mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-15 12:34:30 +00:00
sw_engine image: code refactoring
+++
This commit is contained in:
parent
93da844d94
commit
e195ac8b30
3 changed files with 31 additions and 37 deletions
|
@ -224,11 +224,11 @@ struct SwImage
|
||||||
SwRleData* rle = nullptr;
|
SwRleData* rle = nullptr;
|
||||||
uint32_t* data = nullptr;
|
uint32_t* data = nullptr;
|
||||||
uint32_t w, h, stride;
|
uint32_t w, h, stride;
|
||||||
int32_t x = 0; //shift x
|
int32_t ox = 0; //offset x
|
||||||
int32_t y = 0; //shift y
|
int32_t oy = 0; //offset y
|
||||||
float scale;
|
float scale;
|
||||||
|
|
||||||
bool transformed = false;
|
bool direct = false; //draw image directly (with offset)
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SwBlender
|
struct SwBlender
|
||||||
|
|
|
@ -26,6 +26,13 @@
|
||||||
/* Internal Class Implementation */
|
/* Internal Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
|
static inline bool _onlyShifted(const Matrix* m)
|
||||||
|
{
|
||||||
|
if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool _genOutline(SwImage* image, const Matrix* transform, SwMpool* mpool, unsigned tid)
|
static bool _genOutline(SwImage* image, const Matrix* transform, SwMpool* mpool, unsigned tid)
|
||||||
{
|
{
|
||||||
image->outline = mpoolReqOutline(mpool, tid);
|
image->outline = mpoolReqOutline(mpool, tid);
|
||||||
|
@ -67,40 +74,27 @@ static bool _genOutline(SwImage* image, const Matrix* transform, SwMpool* mpool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline bool _onlyShifted(const Matrix* m)
|
|
||||||
{
|
|
||||||
if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* External Class Implementation */
|
/* External Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
bool imagePrepare(SwImage* image, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
|
bool imagePrepare(SwImage* image, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid)
|
||||||
{
|
{
|
||||||
image->transformed = !_onlyShifted(transform);
|
image->direct = _onlyShifted(transform);
|
||||||
bool fastTrack;
|
|
||||||
|
|
||||||
|
//Fast track: Non-transformed image but just shifted.
|
||||||
|
if (image->direct) {
|
||||||
|
image->ox = -static_cast<uint32_t>(round(transform->e13));
|
||||||
|
image->oy = -static_cast<uint32_t>(round(transform->e23));
|
||||||
//Figure out the scale factor by transform matrix
|
//Figure out the scale factor by transform matrix
|
||||||
if (image->transformed) {
|
} else {
|
||||||
auto scaleX = sqrtf((transform->e11 * transform->e11) + (transform->e21 * transform->e21));
|
auto scaleX = sqrtf((transform->e11 * transform->e11) + (transform->e21 * transform->e21));
|
||||||
auto scaleY = sqrtf((transform->e22 * transform->e22) + (transform->e12 * transform->e12));
|
auto scaleY = sqrtf((transform->e22 * transform->e22) + (transform->e12 * transform->e12));
|
||||||
//TODO:If the x and y axis scale is different, a separate interpolation algorithm for each axis should be applied.
|
|
||||||
image->scale = (fabsf(scaleX - scaleY) > 0.01f) ? 1.0f : scaleX;
|
image->scale = (fabsf(scaleX - scaleY) > 0.01f) ? 1.0f : scaleX;
|
||||||
fastTrack = false;
|
|
||||||
//Fast track: Non-transformed image but just shifted.
|
|
||||||
} else {
|
|
||||||
image->scale = 1.0f;
|
|
||||||
image->x = -static_cast<uint32_t>(round(transform->e13));
|
|
||||||
image->y = -static_cast<uint32_t>(round(transform->e23));
|
|
||||||
fastTrack = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_genOutline(image, transform, mpool, tid)) return false;
|
if (!_genOutline(image, transform, mpool, tid)) return false;
|
||||||
return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, fastTrack);
|
return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, image->direct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -597,7 +597,7 @@ static bool _rasterDirectMaskedRleImage(SwSurface* surface, const SwImage* image
|
||||||
for (uint32_t i = 0; i < image->rle->size; ++i, ++span) {
|
for (uint32_t i = 0; i < image->rle->size; ++i, ++span) {
|
||||||
auto dst = &surface->buffer[span->y * surface->stride + span->x];
|
auto dst = &surface->buffer[span->y * surface->stride + span->x];
|
||||||
auto cmp = &cbuffer[span->y * surface->stride + span->x];
|
auto cmp = &cbuffer[span->y * surface->stride + span->x];
|
||||||
auto img = image->data + (span->y + image->y) * image->stride + (span->x + image->x);
|
auto img = image->data + (span->y + image->oy) * image->stride + (span->x + image->ox);
|
||||||
auto alpha = _multiplyAlpha(span->coverage, opacity);
|
auto alpha = _multiplyAlpha(span->coverage, opacity);
|
||||||
if (alpha == 255) {
|
if (alpha == 255) {
|
||||||
for (uint32_t x = 0; x < span->len; ++x, ++dst, ++cmp, ++img) {
|
for (uint32_t x = 0; x < span->len; ++x, ++dst, ++cmp, ++img) {
|
||||||
|
@ -621,7 +621,7 @@ static bool __rasterDirectTranslucentRleImage(SwSurface* surface, const SwImage*
|
||||||
|
|
||||||
for (uint32_t i = 0; i < image->rle->size; ++i, ++span) {
|
for (uint32_t i = 0; i < image->rle->size; ++i, ++span) {
|
||||||
auto dst = &surface->buffer[span->y * surface->stride + span->x];
|
auto dst = &surface->buffer[span->y * surface->stride + span->x];
|
||||||
auto img = image->data + (span->y + image->y) * image->stride + (span->x + image->x);
|
auto img = image->data + (span->y + image->oy) * image->stride + (span->x + image->ox);
|
||||||
auto alpha = _multiplyAlpha(span->coverage, opacity);
|
auto alpha = _multiplyAlpha(span->coverage, opacity);
|
||||||
for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) {
|
for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) {
|
||||||
auto src = ALPHA_BLEND(*img, alpha);
|
auto src = ALPHA_BLEND(*img, alpha);
|
||||||
|
@ -651,7 +651,7 @@ static bool _rasterDirectSolidRleImage(SwSurface* surface, const SwImage* image)
|
||||||
|
|
||||||
for (uint32_t i = 0; i < image->rle->size; ++i, ++span) {
|
for (uint32_t i = 0; i < image->rle->size; ++i, ++span) {
|
||||||
auto dst = &surface->buffer[span->y * surface->stride + span->x];
|
auto dst = &surface->buffer[span->y * surface->stride + span->x];
|
||||||
auto img = image->data + (span->y + image->y) * image->stride + (span->x + image->x);
|
auto img = image->data + (span->y + image->oy) * image->stride + (span->x + image->ox);
|
||||||
if (span->coverage == 255) {
|
if (span->coverage == 255) {
|
||||||
for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) {
|
for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) {
|
||||||
*dst = *img;
|
*dst = *img;
|
||||||
|
@ -978,7 +978,7 @@ static bool _rasterDirectMaskedImage(SwSurface* surface, const SwImage* image, u
|
||||||
|
|
||||||
TVGLOG("SW_ENGINE", "Direct Masked Image");
|
TVGLOG("SW_ENGINE", "Direct Masked Image");
|
||||||
|
|
||||||
auto sbuffer = image->data + (region.min.y + image->y) * image->stride + (region.min.x + image->x);
|
auto sbuffer = image->data + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox);
|
||||||
auto cbuffer = surface->compositor->image.data + (region.min.y * surface->stride) + region.min.x; //compositor buffer
|
auto cbuffer = surface->compositor->image.data + (region.min.y * surface->stride) + region.min.x; //compositor buffer
|
||||||
|
|
||||||
for (uint32_t y = 0; y < h2; ++y) {
|
for (uint32_t y = 0; y < h2; ++y) {
|
||||||
|
@ -1000,7 +1000,7 @@ static bool _rasterDirectMaskedImage(SwSurface* surface, const SwImage* image, u
|
||||||
static bool __rasterDirectTranslucentImage(SwSurface* surface, const SwImage* image, uint32_t opacity, const SwBBox& region)
|
static bool __rasterDirectTranslucentImage(SwSurface* surface, const SwImage* image, uint32_t opacity, const SwBBox& region)
|
||||||
{
|
{
|
||||||
auto dbuffer = &surface->buffer[region.min.y * surface->stride + region.min.x];
|
auto dbuffer = &surface->buffer[region.min.y * surface->stride + region.min.x];
|
||||||
auto sbuffer = image->data + (region.min.y + image->y) * image->stride + (region.min.x + image->x);
|
auto sbuffer = image->data + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox);
|
||||||
|
|
||||||
for (auto y = region.min.y; y < region.max.y; ++y) {
|
for (auto y = region.min.y; y < region.max.y; ++y) {
|
||||||
auto dst = dbuffer;
|
auto dst = dbuffer;
|
||||||
|
@ -1032,7 +1032,7 @@ static bool _rasterDirectTranslucentImage(SwSurface* surface, const SwImage* ima
|
||||||
static bool _rasterDirectSolidImage(SwSurface* surface, const SwImage* image, const SwBBox& region)
|
static bool _rasterDirectSolidImage(SwSurface* surface, const SwImage* image, const SwBBox& region)
|
||||||
{
|
{
|
||||||
auto dbuffer = &surface->buffer[region.min.y * surface->stride + region.min.x];
|
auto dbuffer = &surface->buffer[region.min.y * surface->stride + region.min.x];
|
||||||
auto sbuffer = image->data + (region.min.y + image->y) * image->stride + (region.min.x + image->x);
|
auto sbuffer = image->data + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox);
|
||||||
|
|
||||||
for (auto y = region.min.y; y < region.max.y; ++y) {
|
for (auto y = region.min.y; y < region.max.y; ++y) {
|
||||||
auto dst = dbuffer;
|
auto dst = dbuffer;
|
||||||
|
@ -1592,21 +1592,21 @@ bool rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, co
|
||||||
|
|
||||||
//Clipped Image
|
//Clipped Image
|
||||||
if (image->rle) {
|
if (image->rle) {
|
||||||
if (image->transformed) {
|
if (image->direct) {
|
||||||
if (translucent) return _rasterTransformedTranslucentRleImage(surface, image, opacity, &itransform, halfScale);
|
|
||||||
else return _rasterTransformedSolidRleImage(surface, image, &itransform, halfScale);
|
|
||||||
} else {
|
|
||||||
if (translucent) return _rasterDirectTranslucentRleImage(surface, image, opacity);
|
if (translucent) return _rasterDirectTranslucentRleImage(surface, image, opacity);
|
||||||
else return _rasterDirectSolidRleImage(surface, image);
|
else return _rasterDirectSolidRleImage(surface, image);
|
||||||
|
} else {
|
||||||
|
if (translucent) return _rasterTransformedTranslucentRleImage(surface, image, opacity, &itransform, halfScale);
|
||||||
|
else return _rasterTransformedSolidRleImage(surface, image, &itransform, halfScale);
|
||||||
}
|
}
|
||||||
//Whole Image
|
//Whole Image
|
||||||
} else {
|
} else {
|
||||||
if (image->transformed) {
|
if (image->direct) {
|
||||||
if (translucent) return _rasterTransformedTranslucentImage(surface, image, opacity, bbox, &itransform, halfScale);
|
|
||||||
else return _rasterTransformedSolidImage(surface, image, bbox, &itransform, halfScale);
|
|
||||||
} else {
|
|
||||||
if (translucent) return _rasterDirectTranslucentImage(surface, image, opacity, bbox);
|
if (translucent) return _rasterDirectTranslucentImage(surface, image, opacity, bbox);
|
||||||
else return _rasterDirectSolidImage(surface, image, bbox);
|
else return _rasterDirectSolidImage(surface, image, bbox);
|
||||||
|
} else {
|
||||||
|
if (translucent) return _rasterTransformedTranslucentImage(surface, image, opacity, bbox, &itransform, halfScale);
|
||||||
|
else return _rasterTransformedSolidImage(surface, image, bbox, &itransform, halfScale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue