Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BiAkima? #10

Open
zvezdochiot opened this issue Dec 31, 2022 · 0 comments
Open

BiAkima? #10

zvezdochiot opened this issue Dec 31, 2022 · 0 comments

Comments

@zvezdochiot
Copy link

zvezdochiot commented Dec 31, 2022

Hi @shawlleyw .

Besides BiCubic (4x4) there is an implementation of BiAkima (6x6). Wouldn't it be more interesting to test a more time-consuming resize besides BiCubic.

Implementation in Qt:

int SelectChannelPixel(QRgb pix, int channel)
{
    int value[5] = {qRed(pix), qGreen(pix), qBlue(pix), qAlpha(pix), 0};
    return value[channel];
}

QRgb InterpolateBiAkima (QImage img, float y, float x)
{
    int i, j, d, dn, xi, yi, xf, yf;
    float dx, dy, zp, zf, a, b;
    float Cc, C[6], m[6], t[2];
    int Ci, pt[4];
    int height = img.height();
    int width = img.width();;
    QRgb *row, imgpix;

    yi = (int)y;
    dy = y - yi;
    yi = (yi < 0) ? 0 : (yi < height) ? yi : (height - 1);
    xi = (int)x;
    dx = x - xi;
    xi = (xi < 0) ? 0 : (xi < width) ? xi : (width - 1);
    dn = (img.hasAlphaChannel()) ? 4 : 3;
    for(d = 0; d < dn; d++)
    {
        if (dy > 0.0f)
        {
            for(i = -2; i < 4; i++)
            {
                yf = (int)y + i;
                yf = (yf < 0) ? 0 : (yf < height) ? yf : (height - 1);
                row = (QRgb*)img.constScanLine(yf);
                if (dx > 0.0f)
                {
                    zp = 0.0f;
                    for(j = -2; j < 4; j++)
                    {
                        xf = (int)x + j;
                        xf = (xf < 0) ? 0 : (xf < width) ? xf : (width - 1);
                        zf = SelectChannelPixel(row[xf], d);
                        m[j + 2] = zf - zp;
                        zp = zf;
                    }
                    for(j = 0; j < 2; j++)
                    {
                        a = (m[j + 3] > m[j + 4]) ? (m[j + 3] - m[j + 4]) : (m[j + 4] - m[j + 3]);
                        b = (m[j + 2] > m[j + 1]) ? (m[j + 2] - m[j + 1]) : (m[j + 1] - m[j + 2]);
                        t[j] = ((a + b) > 0) ? ((a * m[j + 2] + b * m[j + 3]) / (a + b)) : (0.5f * (m[j + 2] + m[j + 3]));
                    }
                    zf = SelectChannelPixel(row[xi], d);
                    C[i + 2] = zf + (t[0] + ((m[3] + m[3] + m[3] - t[0] - t[0] - t[1]) + (t[0] + t[1] - m[3] - m[3]) * dx) * dx) * dx;
                }
                else
                {
                    xf = xi;
                    C[i + 2] = SelectChannelPixel(row[xf], d);
                }
            }
            zp = 0.0f;
            for(i = -2; i < 4; i++)
            {
                m[i + 2] = C[i + 2] - zp;
                zp = C[i + 2];
            }
            for(i = 0; i < 2; i++)
            {
                a = (m[i + 3] > m[i + 4]) ? (m[i + 3] - m[i + 4]) : (m[i + 4] - m[i + 3]);
                b = (m[i + 2] > m[i + 1]) ? (m[i + 2] - m[i + 1]) : (m[i + 1] - m[i + 2]);
                t[i] = ((a + b) > 0) ? ((a * m[i + 2] + b * m[i + 3]) / (a + b)) : (0.5f * (m[i + 2] + m[i + 3]));
            }
            Cc = C[2] + (t[0] + ((m[3] + m[3] + m[3] - t[0] - t[0] - t[1]) + (t[0] + t[1] - m[3] - m[3]) * dy) * dy) * dy;
        }
        else
        {
            yf = yi;
            row = (QRgb*)img.constScanLine(yf);
            if (dx > 0.0f)
            {
                zp = 0.0f;
                for(j = -2; j < 4; j++)
                {
                    xf = (int)x + j;
                    xf = (xf < 0) ? 0 : (xf < width) ? xf : (width - 1);
                    zf = SelectChannelPixel(row[xf], d);
                    m[j + 2] = zf - zp;
                    zp = zf;
                }
                for(j = 0; j < 2; j++)
                {
                    a = (m[j + 3] > m[j + 4]) ? (m[j + 3] - m[j + 4]) : (m[j + 4] - m[j + 3]);
                    b = (m[j + 2] > m[j + 1]) ? (m[j + 2] - m[j + 1]) : (m[j + 1] - m[j + 2]);
                    t[j] = ((a + b) > 0) ? ((a * m[j + 2] + b * m[j + 3]) / (a + b)) : (0.5f * (m[j + 2] + m[j + 3]));
                }
                zf = SelectChannelPixel(row[xi], d);
                Cc = zf + (t[0] + ((m[3] + m[3] + m[3] - t[0] - t[0] - t[1]) + (t[0] + t[1] - m[3] - m[3]) * dx) * dx) * dx;
            }
            else
            {
                xf = xi;
                Cc = SelectChannelPixel(row[xf], d);
            }
        }
        Ci = (int)(Cc + 0.5f);
        pt[d] = (Ci < 0) ? 0 : (Ci > 255) ? 255 : Ci;
    }
    imgpix = (dn > 3) ? qRgba(pt[0], pt[1], pt[2],  pt[3]) : qRgb(pt[0], pt[1], pt[2]);

    return imgpix;
}

Good luck.

PS: Compare BiAkima and other: Scaler biAkima demo.

See also: stb-image-resize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant