Tile the windowskin instead of stretching

I have narrowed the code that creates the borders of a windowskin down to this:

        bitmap.blt(skin, p+m, 0+0, p-m*2, m, m, 0, w-m*2, m);        bitmap.blt(skin, p+m, 0+q-m, p-m*2, m, m, h-m, w-m*2, m);        bitmap.blt(skin, p+0, 0+m, m, p-m*2, 0, m, m, h-m*2);        bitmap.blt(skin, p+q-m, 0+m, m, p-m*2, w-m, m, m, h-m*2);But my head is spinning at all the variables there.

Context:

    if (w > 0 && h > 0 && this._windowskin) {        var skin = this._windowskin;        var p = 96;        var q = 96;        bitmap.blt(skin, p+m, 0+0, p-m*2, m, m, 0, w-m*2, m);        bitmap.blt(skin, p+m, 0+q-m, p-m*2, m, m, h-m, w-m*2, m);        bitmap.blt(skin, p+0, 0+m, m, p-m*2, 0, m, m, h-m*2);        bitmap.blt(skin, p+q-m, 0+m, m, p-m*2, w-m, m, m, h-m*2);        bitmap.blt(skin, p+0, 0+0, m, m, 0, 0, m, m);               //frame corner        bitmap.blt(skin, p+q-m, 0+0, m, m, w-m, 0, m, m);          //frame corner        bitmap.blt(skin, p+0, 0+q-m, m, m, 0, h-m, m, m);         //frame corner        bitmap.blt(skin, p+q-m, 0+q-m, m, m, w-m, h-m, m, m);    //frame corner    }What do you reckon I need to change here to tile the frame image instead of stretching to fill?

var m is just an integer, 24; var p and q are both the integer 96. h and w are actually variable.

Bitmap.prototype.blt = function(source, sx, sy, sw, sh, dx, dy, dw, dh)
sx sy sw sh - must be the source. dx dy dw dh must be the designated area to fill.

Is there actually a way to do this or will I need to make an entirely new function?

Original thread: https://forums.rpgmakerweb.com/threads/tile-the-windowskin-instead-of-stretching.48061/