First some questions to better clarify your problem:
- what kind of interpolation you want: linear/cubic/other ?
- What are the points constrains? for example will there be alway just single region encapsulated by these control points or there could be also points inside?
For the simple linear interpolation and arbitrary (but at least 3 points not on a single line) I would try this:
Triangulate control points area
To non overlapping triangles covering whole defined area.
render triangles
So just rasterize see Algorithm to fill triangle and all the sublinks. You should interpolate also the R,G,B
along with the coordinates.
Create a 2 copies of gradient and extrapolate one with H and second with V lines
So scan all the H-horizontal lines of the gradient and if found 2 known pixels far enough from each other (for example quarter or half of gradient size) then extrapolate the whole line unknown colors. So if found known endpoints (Red) are (x0,y,r0,g0,b0),(x1,y,r1,g1,b1)
then set all unknown colors in the same line as:
r = r0+(r1-r0)*(x-x0)/(x1-x0)
g = g0+(g1-g0)*(x-x0)/(x1-x0)
b = b0+(b1-b0)*(x-x0)/(x1-x0)
Similarly do the same in the copy of gradient for V-vertical lines now. So the points are now (x,y0,r0,g0,b0),(x,y1,r1,g1,b1)` and extrapolation:
r = r0+(r1-r0)*(y-y0)/(y1-y0)
g = g0+(g1-g0)*(y-y0)/(y1-y0)
b = b0+(b1-b0)*(y-y0)/(y1-y0)
After this compare both copies and if unknown point is computed in both set it as average of both colors in the target gradient image. Loop this whole process (#3) until no new gradient pixel is added.
use single extrapolated color for the rest
depending on how you define the control points some areas will have only 1 extrapolated color (either from H or V lines but not both) so use only the single computed color for those (after #3 is done).
Here an example of what I mean by all this:
If you want something simple instead (but not exact) then you can bleed the known control points colors (with smooth filters) to neighboring pixels until the whole gradient is filled and saturated.
- fill unknown gradient pixels with predefined color meaning not computed
set each pixel to average of its computed neighbors
you may do this in separate image to avoid shifting.
set control points back to original color
loop #2 until area filled/saturated/or predefined number of iterations
[Edit1] second solution
Ok I put it together in C++ with your points/colors and gradient size here is how it looks (I bleed 100 times with 4-neighbors bleeding without weights):
The image on the left is input matrix where I encoded into alpha channel (highest 8 bits) if the pixel is reference point, computed or yet undefined. The image on the right is after applying the bleeding 100 times. The bleed is simple just take any non reference point and recompute it as average of all usable pixels around and itself (ignoring any undefined colors).
Here the C++ code you can ignore the GDI stuff for rendering (beware my gradient map has x
coordinate first you got y
!)
//---------------------------------------------------------------------------
const int mxs=7,mys=7,msz=16; // gradient resolution x,y and square size for render
DWORD map[mxs][mys]; // gradient matrix ... undefined color is >= 0xFF000000
// 0x00?????? - reference color
// 0xFF?????? - uncomputed color
// 0xFE?????? - bleeded color
//---------------------------------------------------------------------------
void map_clear() // set all pixels as uncomputed (white with alpha=255)
{
int x,y;
for (x=0;x<mxs;x++)
for (y=0;y<mys;y++)
map[x][y]=0xFFFFFFFF;
}
void map_bleed() // bleed computed colors
{
int x,y,r,g,b,n;
DWORD tmp[mxs][mys],c;
for (x=0;x<mxs;x++)
for (y=0;y<mys;y++)
{
c=map[x][y];
n=0; r=0; g=0; b=0; if (DWORD(c&0xFF000000)==0) { tmp[x][y]=c; continue; } if (DWORD(c&0xFF000000)!=0xFF000000) { r+=c&255; g+=(c>>8)&255; b+=(c>>16)&255; n++; }
x++; if ((x>=0)&&(x<mxs)&&(y>=0)&&(y<mys)) c=map[x][y]; else c=0xFF000000; if (DWORD(c&0xFF000000)!=0xFF000000) { r+=c&255; g+=(c>>8)&255; b+=(c>>16)&255; n++; }
x--; y--; if ((x>=0)&&(x<mxs)&&(y>=0)&&(y<mys)) c=map[x][y]; else c=0xFF000000; if (DWORD(c&0xFF000000)!=0xFF000000) { r+=c&255; g+=(c>>8)&255; b+=(c>>16)&255; n++; }
x--; y++; if ((x>=0)&&(x<mxs)&&(y>=0)&&(y<mys)) c=map[x][y]; else c=0xFF000000; if (DWORD(c&0xFF000000)!=0xFF000000) { r+=c&255; g+=(c>>8)&255; b+=(c>>16)&255; n++; }
x++; y++; if ((x>=0)&&(x<mxs)&&(y>=0)&&(y<mys)) c=map[x][y]; else c=0xFF000000; if (DWORD(c&0xFF000000)!=0xFF000000) { r+=c&255; g+=(c>>8)&255; b+=(c>>16)&255; n++; }
y--; if (!n) { tmp[x][y]=0xFFFFFFFF; continue; }
c=((r/n)|((g/n)<<8)|((b/n)<<16))&0x00FFFFFF;
tmp[x][y]=c;
}
// copy tmp back to map
for (x=0;x<mxs;x++)
for (y=0;y<mys;y++)
map[x][y]=tmp[x][y];
}
void map_draw(TCanvas *can,int x0,int y0) // just renders actual gradient map onto canvas (can ignore this)
{
int x,y,xx,yy;
for (x=0,xx=x0;x<mxs;x++,xx+=msz)
for (y=0,yy=y0;y<mys;y++,yy+=msz)
{
can->Pen->Color=clBlack;
can->Brush->Color=map[x][y]&0x00FFFFFF;
can->Rectangle(xx,yy,xx+msz,yy+msz);
}
}
//---------------------------------------------------------------------------
And here the usage (your example):
// clear backbuffer
bmp->Canvas->Brush->Color=clBlack;
bmp->Canvas->FillRect(TRect(0,0,xs,ys));
// init your gradient with reference points
map_clear();
// x y R G B
map[6][0] = (239)|(238<<8)|(185<<16);
map[1][1] = (120)|(131<<8)|(125<<16);
map[6][4] = (184)|(191<<8)|(171<<16);
map[2][6] = (150)|(168<<8)|(158<<16);
map[5][6] = (166)|(180<<8)|(166<<16);
map_draw(bmp->Canvas,msz,msz); // render result (left)
// bleed
for (int i=0;i<100;i++) map_bleed();
map_draw(bmp->Canvas,(mxs+2)*msz,msz); // render result (right)
// refresh window with backbufer (anti-flickering)
Main->Canvas->Draw(0,0,bmp);
Again you can ignore all the rendering stuff. The number of bleeds should be 2x bigger then pixels in diagonal so bleeding covers all the pixels. The more iterations the more saturated result I try 100
just for example and the result looks good .. so I did not play with it anymore...
[Edit2] and here the algorithm for the second approach
add flags to interpolated matrix
You need to know if the pixel is reference,undefined
or interpolated
. You can encode this to alpha channel, or use mask (separate 2D matrix).
bleed/smooth matrix
basically for each non reference
pixel compute its new value as average of all non undefined
pixels around (4/8 neighbors) and at its position. Do not use undefined
pixels and store the computed value to temporary matrix (not messing up next pixels otherwise the bleeding/smoothing would shift the pixels usually diagonally). This way undefined pixels areas will shrink by 1 pixel. After whole matrix is done copy the content of temporary matrix to the original one (or swap pointers).
loop #2 until result is saturated or specific count of iterations
Number of counts should be at leas 2x bigger then the number of diagonal pixels to propagate reference pixel into whole matrix. The saturation check can be done in #2 while copying the temp array into original one (can do abs difference between frames and if zero or near it stop).