diff --git a/rtdata/languages/default b/rtdata/languages/default index e295762ad..31229b41a 100644 --- a/rtdata/languages/default +++ b/rtdata/languages/default @@ -654,6 +654,7 @@ TP_RESIZE_FULLSIZE;Full Image Size: TP_RESIZE_H;H: TP_RESIZE_HEIGHT;Height TP_RESIZE_LABEL;Resize +TP_RESIZE_LANCZOS;Lanczos TP_RESIZE_METHOD;Method: TP_RESIZE_NEAREST;Nearest TP_RESIZE_SCALE;Scale diff --git a/rtengine/ipresize.cc b/rtengine/ipresize.cc index aebe96f2a..8fb044a2e 100644 --- a/rtengine/ipresize.cc +++ b/rtengine/ipresize.cc @@ -35,7 +35,7 @@ namespace rtengine { #define CLIP(a) ((a)>0?((a)(b)?((a)<(c)?(a):(c)):(b)) -double Lanc(double x, double a) +inline double Lanc(double x, double a) { if (x * x < 1e-6) return 1.0; @@ -47,110 +47,234 @@ double Lanc(double x, double a) } } +void Lanczos1(Image16* src, Image16* dst, double scale) +{ + const double delta = 1.0 / scale; + const double a = 3.0; + const double sc = std::min(scale, 1.0); + const int support = (int)(2.0 * a / sc) + 1; + + Image16 * tmp = new Image16(src->width, dst->height); + + #pragma omp parallel for + for (int i = 0; i < tmp->height; i++) { + // y coord of the center of pixel on src image + double y0 = (i + 0.5) * delta - 0.5; + + // weights for interpolation in y direction + double w[support]; + + // sum of weights used for normalization + double ww = 0.0; + + int ii0 = std::max(0, (int)floor(y0 - a / sc) + 1); + int ii1 = std::min(src->height, (int)floor(y0 + a / sc) + 1); + + // calculate weights + for (int ii = ii0; ii < ii1; ii++) { + int k = ii - ii0; + double z = sc * (y0 - ii); + w[k] = Lanc(z, a); + ww += w[k]; + } + + // normalize weights + for (int k = 0; k < support; k++) { + w[k] /= ww; + } + + for (int j = 0; j < tmp->width; j++) { + + double r = 0.0, g = 0.0, b = 0.0; + + for (int ii = ii0; ii < ii1; ii++) { + int k = ii - ii0; + + r += w[k] * src->r[ii][j]; + g += w[k] * src->g[ii][j]; + b += w[k] * src->b[ii][j]; + } + + tmp->r[i][j] = CLIP((int)r); + tmp->g[i][j] = CLIP((int)g); + tmp->b[i][j] = CLIP((int)b); + } + } + + #pragma omp parallel for + for (int j = 0; j < dst->width; j++) { + // x coord of the center of pixel on src image + double x0 = (j + 0.5) * delta - 0.5; + + // weights for interpolation in y direction + double w[support]; + + // sum of weights used for normalization + double ww = 0.0; + + int jj0 = std::max(0, (int)floor(x0 - a / sc) + 1); + int jj1 = std::min(tmp->width, (int)floor(x0 + a / sc) + 1); + + // calculate weights + for (int jj = jj0; jj < jj1; jj++) { + int k = jj - jj0; + double z = sc * (x0 - jj); + w[k] = Lanc(z, a); + ww += w[k]; + } + + // normalize weights + for (int k = 0; k < support; k++) { + w[k] /= ww; + } + + for (int i = 0; i < dst->height; i++) { + + double r = 0.0, g = 0.0, b = 0.0; + + for (int jj = jj0; jj < jj1; jj++) { + int k = jj - jj0; + + r += w[k] * tmp->r[i][jj]; + g += w[k] * tmp->g[i][jj]; + b += w[k] * tmp->b[i][jj]; + } + + dst->r[i][j] = CLIP((int)r); + dst->g[i][j] = CLIP((int)g); + dst->b[i][j] = CLIP((int)b); + } + } + + delete tmp; +} + +void Lanczos2(Image16* src, Image16* dst, double scale) +{ + const double delta = 1.0 / scale; + const double a = 3.0; + const double sc = std::min(scale, 1.0); + const int support = (int)(2.0 * a / sc) + 1; + + double * wwh = new double[support * dst->width]; + int * jj0 = new int[dst->width]; + int * jj1 = new int[dst->width]; + double * lr = new double[src->width]; + double * lg = new double[src->width]; + double * lb = new double[src->width]; + + // Phase 1: precompute coefficients for horisontal interpolation + + for (int j = 0; j < dst->width; j++) { + + // x coord of the center of pixel on src image + double x0 = (j + 0.5) * delta - 0.5; + + // weights for interpolation in horisontal direction + double * w = wwh + j * support; + + // sum of weights used for normalization + double ws = 0.0; + + jj0[j] = std::max(0, (int)floor(x0 - a / sc) + 1); + jj1[j] = std::min(src->width, (int)floor(x0 + a / sc) + 1); + + // calculate weights + for (int jj = jj0[j]; jj < jj1[j]; jj++) { + int k = jj - jj0[j]; + double z = sc * (x0 - jj); + w[k] = Lanc(z, a); + ws += w[k]; + } + + // normalize weights + for (int k = 0; k < support; k++) { + w[k] /= ws; + } + } + + // phase2: do actual interpolation + + for (int i = 0; i < dst->height; i++) { + + // y coord of the center of pixel on src image + double y0 = (i + 0.5) * delta - 0.5; + + // weights for interpolation in y direction + double w[support]; + + // sum of weights used for normalization + double ws= 0.0; + + int ii0 = std::max(0, (int)floor(y0 - a / sc) + 1); + int ii1 = std::min(src->height, (int)floor(y0 + a / sc) + 1); + + // calculate weights + for (int ii = ii0; ii < ii1; ii++) { + int k = ii - ii0; + double z = sc * (y0 - ii); + w[k] = Lanc(z, a); + ws += w[k]; + } + + // normalize weights + for (int k = 0; k < support; k++) { + w[k] /= ws; + } + + // Do vertical interpolation. Store results. + for (int j = 0; j < src->width; j++) { + + double r = 0.0, g = 0.0, b = 0.0; + + for (int ii = ii0; ii < ii1; ii++) { + int k = ii - ii0; + + r += w[k] * src->r[ii][j]; + g += w[k] * src->g[ii][j]; + b += w[k] * src->b[ii][j]; + } + + lr[j] = r; + lg[j] = g; + lb[j] = b; + } + + //Do horisontal interpolation + for(int j = 0; j < dst->width; j++) { + + double * wh = wwh + support * j; + + double r = 0.0, g = 0.0, b = 0.0; + + for (int jj = jj0[j]; jj < jj1[j]; jj++) { + int k = jj - jj0[j]; + + r += wh[k] * lr[jj]; + g += wh[k] * lg[jj]; + b += wh[k] * lb[jj]; + } + + dst->r[i][j] = CLIP((int)r); + dst->g[i][j] = CLIP((int)g); + dst->b[i][j] = CLIP((int)b); + } + } + + delete[] wwh; + delete[] jj0; + delete[] jj1; + delete[] lr; + delete[] lg; + delete[] lb; +} + void ImProcFunctions::resize (Image16* src, Image16* dst) { time_t t1 = clock(); if(params->resize.method == "Lanczos") { - - const double delta = 1.0 / params->resize.scale; - const double a = 3.0; - const double sc = std::min(params->resize.scale, 1.0); - const int support = (int)(2.0 * a / sc) + 1; - - Image16 * tmp = new Image16(src->width, dst->height); - - //#pragma omp parallel for - for (int i = 0; i < tmp->height; i++) { - // y coord of the center of pixel on src image - double y0 = (i + 0.5) * delta - 0.5; - - // weights for interpolation in y direction - double w[support]; - - // sum of weights used for normalization - double ww = 0.0; - - int ii0 = std::max(0, (int)floor(y0 - a / sc) + 1); - int ii1 = std::min(src->height, (int)floor(y0 + a / sc) + 1); - - // calculate weights - for (int ii = ii0; ii < ii1; ii++) { - int k = ii - ii0; - double z = sc * (y0 - ii); - w[k] = Lanc(z, a); - ww += w[k]; - } - - // normalize weights - for (int k = 0; k < support; k++) { - w[k] /= ww; - } - - for (int j = 0; j < tmp->width; j++) { - - double r = 0.0, g = 0.0, b = 0.0; - - for (int ii = ii0; ii < ii1; ii++) { - int k = ii - ii0; - - r += w[k] * src->r[ii][j]; - g += w[k] * src->g[ii][j]; - b += w[k] * src->b[ii][j]; - } - - tmp->r[i][j] = CLIP((int)r); - tmp->g[i][j] = CLIP((int)g); - tmp->b[i][j] = CLIP((int)b); - } - } - - //#pragma omp parallel for - for (int j = 0; j < dst->width; j++) { - // x coord of the center of pixel on src image - double x0 = (j + 0.5) * delta - 0.5; - - // weights for interpolation in y direction - double w[support]; - - // sum of weights used for normalization - double ww = 0.0; - - int jj0 = std::max(0, (int)floor(x0 - a / sc) + 1); - int jj1 = std::min(tmp->width, (int)floor(x0 + a / sc) + 1); - - // calculate weights - for (int jj = jj0; jj < jj1; jj++) { - int k = jj - jj0; - double z = sc * (x0 - jj); - w[k] = Lanc(z, a); - ww += w[k]; - } - - // normalize weights - for (int k = 0; k < support; k++) { - w[k] /= ww; - } - - for (int i = 0; i < dst->height; i++) { - - double r = 0.0, g = 0.0, b = 0.0; - - for (int jj = jj0; jj < jj1; jj++) { - int k = jj - jj0; - - r += w[k] * tmp->r[i][jj]; - g += w[k] * tmp->g[i][jj]; - b += w[k] * tmp->b[i][jj]; - } - - dst->r[i][j] = CLIP((int)r); - dst->g[i][j] = CLIP((int)g); - dst->b[i][j] = CLIP((int)b); - } - } - - delete tmp; + Lanczos2(src, dst, params->resize.scale); } else if(params->resize.method == "Downscale (Better)") { // small-scale algorithm by Ilia @@ -403,6 +527,10 @@ void ImProcFunctions::resize (Image16* src, Image16* dst) { } } } + + time_t t2 = clock(); + std::cout << "Resize: " << params->resize.method << ": " + << (double)(t2 - t1) / CLOCKS_PER_SEC << std::endl; } } diff --git a/rtgui/resize.cc b/rtgui/resize.cc index 48abe579a..9d9193263 100644 --- a/rtgui/resize.cc +++ b/rtgui/resize.cc @@ -39,6 +39,7 @@ Resize::Resize () : maxw(100000), maxh(100000) { method->append_text (M("TP_RESIZE_BICUBICSH")); method->append_text (M("TP_RESIZE_DOWNSCALEB")); method->append_text (M("TP_RESIZE_DOWNSCALEF")); + method->append_text (M("TP_RESIZE_LANCZOS")); method->set_active (0); combos->attach (*Gtk::manage (new Gtk::Label (M("TP_RESIZE_METHOD"))), 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 2, 2); @@ -130,6 +131,8 @@ void Resize::read (const ProcParams* pp, const ParamsEdited* pedited) { method->set_active (5); else if (pp->resize.method == "Downscale (Faster)") method->set_active (6); + else if (pp->resize.method == "Lanczos") + method->set_active (7); wDirty = false; hDirty = false; @@ -170,6 +173,8 @@ void Resize::write (ProcParams* pp, ParamsEdited* pedited) { pp->resize.method = "Downscale (Better)"; else if (method->get_active_row_number() == 6) pp->resize.method = "Downscale (Faster)"; + else if (method->get_active_row_number() == 7) + pp->resize.method = "Lanczos"; pp->resize.dataspec = spec->get_active_row_number(); pp->resize.width = round (w->get_value ());