In my project (and I am sure many others), I need to copy a subset (R2) of one bitmap image (R1), into a region (R3) in another, where the destination region can be any size (stretching and scaling where necessary).I am sure this is a solved problem but I cannot find any examples; what is the most straightforward algorithm for performing this operation? (Linear interpolation would be sufficient for filling in the pixels when the source is scaled upwards, if that is the quickest (computationally) method)
What is the algorithm to copy a region of one bitmap, into a region in another?
algorithms
Here's C-like pseudocode. You can figure how to handle the edge cases (e.g. clipping, mirroring, etc). I'll leave it to you to also figure out how to make this use integer or fixed point math if performance is an issue.struct {    bitmap bmp;    float x, y, width, height;} xfer_param;scaled_xfer(xfer_param src, xfer_param det){    float src_dx = dst.width / src.width;    float src_dy = dst.height / src.height;    float src_maxx = src.x + src.width;    float src_maxy = src.y + src.height;    float dst_maxx = dst.x + dst.width;    float dst_maxy = dst.y + dst.height;    float src_cury = src.y;    for (float y = dst.y; y < dst_maxy; y++)    {        float src_curx = src.x;           for (float x = dst.x; x < dst_maxx; x++)        {            // Point sampling - you can also impl as bilinear or other            dst.bmp[x,y] = src.bmp[src_curx, src_cury];            src_curx += src_dx;        }        src_cury += src_dy;    }}To do sub-pixel interpolation, you can sample the 4 pixels around the floating-point src_curx, src_cury, and take a weighted average. One of the highest quality methods is to effectively take a sample rectangle in the source image that corresponds to a single pixel in the dest image. Then you just average all the values in that and write the destination pixel. However, that rect will likely not fall on clean pixel boundaries, so you will want to take the clipped edge pixels and weight them accordingly.