summaryrefslogtreecommitdiff
path: root/SharedGraphics
diff options
context:
space:
mode:
Diffstat (limited to 'SharedGraphics')
-rw-r--r--SharedGraphics/Painter.cpp38
-rw-r--r--SharedGraphics/Painter.h1
2 files changed, 39 insertions, 0 deletions
diff --git a/SharedGraphics/Painter.cpp b/SharedGraphics/Painter.cpp
index 35479090ec..02a94ec938 100644
--- a/SharedGraphics/Painter.cpp
+++ b/SharedGraphics/Painter.cpp
@@ -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);
diff --git a/SharedGraphics/Painter.h b/SharedGraphics/Painter.h
index fd2b2ca44a..a9485c5e1f 100644
--- a/SharedGraphics/Painter.h
+++ b/SharedGraphics/Painter.h
@@ -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);