[gephex-devel] [Bug 83] New: bad memory accesses in libscale

Sven Schnelle schnelle at kabelleipzig.de
Mon Feb 14 00:45:08 CET 2005


<#secure method=pgpmime mode=sign>

Hi,

bugzilla-daemon at sonnenland.kexbox.org writes:

> https://bugs.gephex.org/show_bug.cgi?id=83
>
>            Summary: bad memory accesses in libscale
> [..]
> When mirrorx is turned on in libscale, memory one past the end of the destination buffer
> is written (current is set to one past the line instead of to the last pixel of the line).

Trying to fix this problem, i've written my own version of ls_scale32m()
and libscale32m_adjust:

-----------------------------8<----------------------------------------
void ls_scale32m(uint_32* dst, int dwidth, int dheight,
                 const uint_32* src, int swidth, int sheight,
                 int mirrorx, int mirrory)
{
    int x,y;
    int dy;
    uint_32 x_a, y_a;
    uint_32 A,B;
    const uint_32* src_base;

    assert (dwidth >= 0 && dheight >= 0);

    if (dwidth == swidth && dheight == sheight && !mirrorx && !mirrory)
        memcpy(dst, src, dwidth*dheight*sizeof(uint_32));
    else { 
  
        A = (uint_32) (65536 * (double)swidth  / (double)dwidth);
        B = (uint_32) (65536 * (double)sheight / (double)dheight);
  
        for (y = 0, y_a = 0; y < dheight; y++, y_a += B) {
            src_base = src + (y_a >> 16) * swidth;
            if(mirrory)
                dy = dheight - 1 - y;
            else
                dy = y;
            x_a = 0;
            if(mirrorx)
                for (x = dwidth - 1; x >= 0; x--, x_a += A) 
                    dst[dy*dwidth+x] = src_base[x_a >> 16];
            else
                for (x = 0; x < dwidth; x++, x_a += A)
                    dst[dy*dwidth+x] = src_base[x_a >> 16];
        }
    }
}

void ls_scale32m_adjust(uint_32* dst, int dwidth, int dheight,
                        const uint_32* src, int swidth, int sheight,
                        int mirrorx, int mirrory, const ls_adjust_pal pal)
{
    int x,y,dy;
    uint_32 x_a, y_a;
    uint_32 A,B;
    const uint_32* src_base;

    assert (dwidth >= 0 && dheight >= 0);

    A = (uint_32) (65536 * (double)swidth  / (double)dwidth);
    B = (uint_32) (65536 * (double)sheight / (double)dheight);
    for (y = 0, y_a = 0; y < dheight; y++, y_a += B) {
        src_base = src + (y_a >> 16) * swidth;
        if(mirrory)
            dy = dheight - 1 - y;
        else
            dy = y;
        x_a = 0;
        if(mirrorx)
            for (x = dwidth - 1; x >= 0; x--, x_a += A)
                apply_pal(&dst[dy*dwidth+x], &src_base[x_a >> 16], pal);
        else
            for (x = 0; x < dwidth; x++, x_a += A)
                apply_pal(&dst[dy*dwidth+x], &src_base[x_a >> 16], pal);
    }
}

-----------------------------8<----------------------------------------

Maybe this approach has some disadvantages, and introduces some new
bugs, but it works for me[tm] :)

Sven.
-- 
main(){while(!fork())sleep(1);}



More information about the gephex-devel mailing list