+/**
+ * Copy a rectangular area of a bitmap on another bitmap.
+ *
+ * Blitting is a common copy operation involving two bitmaps.
+ * A rectangular area of the source bitmap is copied bit-wise
+ * to a different position in the destination bitmap.
+ *
+ * \note Using the same bitmap for \a src and \a dst is unsupported.
+ *
+ * \param dst Bitmap where the operation writes
+ *
+ */
+void gfx_blit(Bitmap *dst, Rect *rect, Bitmap *src, coord_t srcx, coord_t srcy)
+{
+ coord_t dxmin, dymin, dxmax, dymax;
+ coord_t dx, dy, sx, sy;
+
+ /*
+ * Clip coordinates inside dst->cr and src->width/height.
+ */
+ dxmin = rect->xmin;
+ if (dxmin < dst->cr.xmin)
+ {
+ srcx += dst->cr.xmin - dxmin;
+ dxmin = dst->cr.xmin;
+ }
+ dymin = rect->ymin;
+ if (dymin < dst->cr.ymin)
+ {
+ srcy += dst->cr.ymin - dymin;
+ dymin = dst->cr.ymin;
+ }
+ dxmax = MIN(MIN(rect->xmax, rect->xmin + src->width), dst->cr.xmax);
+ dymax = MIN(MIN(rect->ymax, rect->ymin + src->height), dst->cr.ymax);
+
+ /* TODO: make it not as dog slow as this */
+ for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
+ for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
+ BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
+}