mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
SharedGraphics: Add Painter::draw_scaled_bitmap().
It's just a simple nearest-neighbor scale with alpha blending but it gets the job done.
This commit is contained in:
parent
e4498194c2
commit
86570d3b1a
Notes:
sideshowbarker
2024-07-19 14:58:24 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/86570d3b1ac
2 changed files with 39 additions and 0 deletions
|
@ -321,6 +321,44 @@ void Painter::blit(const Point& position, const GraphicsBitmap& source, const Re
|
|||
}
|
||||
}
|
||||
|
||||
void Painter::draw_scaled_bitmap(const Rect& a_dst_rect, const GraphicsBitmap& source, const Rect& src_rect)
|
||||
{
|
||||
auto dst_rect = a_dst_rect;
|
||||
if (dst_rect.size() == src_rect.size())
|
||||
return blit(dst_rect.location(), source, src_rect);
|
||||
|
||||
auto safe_src_rect = Rect::intersection(src_rect, source.rect());
|
||||
ASSERT(source.rect().contains(safe_src_rect));
|
||||
dst_rect.move_by(state().translation);
|
||||
auto clipped_rect = Rect::intersection(dst_rect, clip_rect());
|
||||
if (clipped_rect.is_empty())
|
||||
return;
|
||||
|
||||
float hscale = (float)src_rect.width() / (float)dst_rect.width();
|
||||
float vscale = (float)src_rect.height() / (float)dst_rect.height();
|
||||
|
||||
for (int y = dst_rect.top(); y <= dst_rect.bottom(); ++y) {
|
||||
if (y < clipped_rect.top() || y > clipped_rect.bottom())
|
||||
continue;
|
||||
auto* scanline = (Color*)m_target->scanline(y);
|
||||
for (int x = dst_rect.left(); x <= dst_rect.right(); ++x) {
|
||||
if (x < clipped_rect.left() || x >= clipped_rect.right())
|
||||
continue;
|
||||
|
||||
auto scaled_x = (float)(x - dst_rect.x()) * hscale;
|
||||
auto scaled_y = (float)(y - dst_rect.y()) * vscale;
|
||||
auto src_pixel = Color::from_rgba(source.scanline((int)scaled_y)[(int)scaled_x]);
|
||||
|
||||
if (!src_pixel.alpha())
|
||||
continue;
|
||||
if (src_pixel.alpha() == 0xff)
|
||||
scanline[x] = src_pixel;
|
||||
else
|
||||
scanline[x] = scanline[x].blend(scanline[x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, Color color)
|
||||
{
|
||||
draw_glyph(point, ch, font(), color);
|
||||
|
|
|
@ -32,6 +32,7 @@ public:
|
|||
void set_pixel(const Point&, Color);
|
||||
void draw_line(const Point&, const Point&, Color);
|
||||
void draw_focus_rect(const Rect&);
|
||||
void draw_scaled_bitmap(const Rect& dst_rect, const GraphicsBitmap&, const Rect& src_rect);
|
||||
void blit(const Point&, const GraphicsBitmap&, const Rect& src_rect);
|
||||
void blit_with_opacity(const Point&, const GraphicsBitmap&, const Rect& src_rect, float opacity);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue