warp_mbyte_projective_nneighbour
Tcl_Obj* imageObj
Tcl_Obj* forwardObj
/*
* Warp the image using the specified transform. The result is made large
* enough to contain all of the warped image, and will contain meta data about
* the location of the actual (0,0) origin point relative to the physical top
* left corner of the result. This last is required because translations in
* the transform may move result pixels to negative positions which we cannot
* express with the regular memory grid.
*/
crimp_image* image;
crimp_image* forward;
crimp_image* backward;
crimp_image* result;
int x, y, xt, yt, origx, origy, xi, yi, outside, c;
double xf, yf;
crimp_input_any (imageObj, image);
CRIMP_ASSERT_NOTIMGTYPE (image, float);
CRIMP_ASSERT_NOTIMGTYPE (image, grey16);
CRIMP_ASSERT_NOTIMGTYPE (image, grey32);
crimp_input (forwardObj, forward, double);
if (!crimp_require_dim (forward, 3, 3)) {
Tcl_SetResult(interp, "bad matrix dimensions, expected 3x3", TCL_STATIC);
return TCL_ERROR;
}
backward = crimp_la_invert_matrix_3x3 (forward);
if (!backward) {
Tcl_SetResult(interp, "Unable to invert singular matrix", TCL_STATIC);
return TCL_ERROR;
}
/*
* Determine size of the result, and the location of the origin point inside
* based on the four corners of the input image and the forward transformation.
*/
result = crimp_geo_warp_init (image, forward, &origx, &origy);
for (y = 0, yt = origy; y < crimp_h (result); y++, yt++) {
for (x = 0, xt = origx; x < crimp_w (result); x++, xt++) {
xf = xt;
yf = yt;
crimp_geo_warp_point (backward, &xf, &yf);
/*
* Choose the nearest neighbour in x and y to the sampling location as
* the source of the pixel. Use black for when we moved outside the
* boundaries of the input.
*/
xi = xf;
yi = yf;
if ((xf - xi) >= 0.5) xi++;
if ((yf - yi) >= 0.5) yi++;
outside = (xi < 0) || (xi >= crimp_w (image)) || (yi < 0) || (yi >= crimp_h (image));
for (c = 0; c < image->itype->channels; c++) {
CH (result, c, x, y) = outside ? BLACK : CH (image, c, xi, yi);
}
}
}
crimp_del (backward);
Tcl_SetObjResult(interp, crimp_new_image_obj (result));
return TCL_OK;
/* vim: set sts=4 sw=4 tw=80 et ft=c: */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/