#272 Resize Image

open
nobody
None
2013-05-17
2013-05-17
stabud
No

Library FbGfx needs to function resize images. Almost ready code can be found in wikipedia: http://ru.wikipedia.org/wiki/%D0%91%D0%B8%D0%BB%D0%B8%D0%BD%D0%B5%D0%B9%D0%BD%D0%B0%D1%8F_%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D0%BF%D0%BE%D0%BB%D1%8F%D1%86%D0%B8%D1%8F

#include <stdio.h>
#include <math.h>
#include <sys types.h="">

void resample(int oldw, int oldh, int neww, int newh, u_int a[oldh][oldw], u_int b[newh][neww])
{
    int i, j;
    int h, w;
    float t;
    float u;
    float tmp;
    float d1, d2, d3, d4;
    u_int p1, p2, p3, p4;       /* Окрестные пикселы */

    u_char red, green, blue;

    for (j = 0; j < newh; j++) {
            tmp = (float) (j) / (float) (newh - 1) * (oldh - 1);
            h = (int) floor(tmp);
            if (h < 0) {
                h = 0;
            } else {
                if (h >= oldh - 1) {
                    h = oldh - 2;
                }
            }
            u = tmp - h;

        for (i = 0; i < neww; i++) {

            tmp = (float) (i) / (float) (neww - 1) * (oldw - 1);
            w = (int) floor(tmp);
            if (w < 0) {
                w = 0;
            } else {
                if (w >= oldw - 1) {
                    w = oldw - 2;
                }
            }
            t = tmp - w;

            /* Коэффициенты */
            d1 = (1 - t) * (1 - u);
            d2 = t * (1 - u);
            d3 = t * u;
            d4 = (1 - t) * u;

            /* Окрестные пиксели: a[i][j] */
            p1 = a[h][w];
            p2 = a[h][w + 1];
            p3 = a[h + 1][w + 1];
            p4 = a[h + 1][w];

            /* Компоненты */
            blue = (u_char) p1 *d1 + (u_char) p2 *d2 + (u_char) p3 *d3 + (u_char) p4 *d4;
            green = (u_char) (p1 >> 8) * d1 + (u_char) (p2 >> 8) * d2 + (u_char) (p3 >> 8) * d3 + (u_char) (p4 >> 8) * d4;
            red = (u_char) (p1 >> 16) * d1 + (u_char) (p2 >> 16) * d2 + (u_char) (p3 >> 16) * d3 + (u_char) (p4 >> 16) * d4;

            /* Новый пиксел из R G B  */
            b[j][i] = ((u_int32_t) red << 16) | ((u_int32_t) green << 8) | (blue);
        }
    }
}

Discussion