+ return out;
+}
+
+static RrPixel32* scale_rect(RrPixel32 *fullsource,
+ gint w, gint h, gint dw, gint dh)
+{
+ RrPixel32 *out, *dest;
+ RrPixel32 *source = fullsource;
+ RrPixel32 *oldsource = NULL;
+ RrPixel32 *prev_source = NULL;
+ gint num_pixels;
+ gint int_part;
+ gint fract_part;
+ gint err = 0;
+
+ while (dw <= (w >> 1) && dh <= (h >> 1)) {
+ source = scale_half(source, w, h);
+ w >>= 1; h >>= 1;
+ g_free(oldsource);
+ oldsource = source;
+ }
+
+ num_pixels = dh;
+ int_part = (h / dh) * w;
+ fract_part = h % dh;
+
+ out = dest = g_new(RrPixel32, dw * dh);
+
+ while (num_pixels-- > 0) {
+ if (source == prev_source) {
+ memcpy(dest, dest - dw, dw * sizeof(RrPixel32));
+ } else {
+ scale_line(dest, source, w, dw);
+ prev_source = source;
+ }
+ dest += dw;
+ source += int_part;
+ err += fract_part;
+ if (err >= dh) {
+ err -= dh;
+ source += w;
+ }
+ }
+
+ g_free(oldsource);
+
+ return out;
+}
+
+void RrImageDraw(RrPixel32 *target, RrTextureRGBA *rgba,
+ gint target_w, gint target_h,
+ RrRect *area)
+{
+ RrPixel32 *dest;
+ RrPixel32 *source;
+ gint sw, sh, dw, dh;
+ gint col, num_pixels;
+
+ sw = rgba->width;
+ sh = rgba->height;
+
+ /* keep the ratio */
+ dw = area->width;
+ dh = (int)(dw * ((double)sh / sw));
+ if (dh > area->height) {
+ dh = area->height;
+ dw = (int)(dh * ((double)sw / sh));
+ }
+
+ if (sw != dw || sh != dh) {
+ /*if (!(rgba->cache && dw == rgba->cwidth && dh == rgba->cheight))*/ {
+ g_free(rgba->cache);
+ rgba->cache = scale_rect(rgba->data, sw, sh, dw, dh);
+ rgba->cwidth = dw;
+ rgba->cheight = dh;
+ }
+ source = rgba->cache;
+ } else {
+ source = rgba->data;
+ }
+
+ /* copy source -> dest, and apply the alpha channel */
+ col = 0;
+ num_pixels = dw * dh;
+ dest = target + area->x + target_w * area->y;
+ while (num_pixels-- > 0) {
+ guchar alpha, r, g, b, bgr, bgg, bgb;