0

I'm recently working on bicubic interpolation for college term-project.

I looked up some other codes in web and run it.

But 'every' codes has a same problem of indexing.

I thought there must be fault of me, but I can't find any.

inline unsigned char getpixel(unsigned char* in,
    int src_width, int src_height, int y, int x, int channel)
{
    if (x < src_width && y < src_height)
        return in[(y * 3 * src_width) + (3 * x) + channel];

    return 0;
}

void bicubicresize(unsigned char *in, unsigned char *out, int src_width, int src_height, int dest_width, int dest_height)
{

    const float tx = float(src_width) / dest_width;
    const float ty = float(src_height) / dest_height;
    const int channels = 3;
    const int row_stride = dest_width * channels;

    unsigned char C[5] = { 0 };

    for (int i = 0; i < dest_height; ++i)
    {
        for (int j = 0; j < dest_width; ++j)
        {
            const int x = int(tx * j);
            const int y = int(ty * i);
            const float dx = tx * j - x;
            const float dy = ty * i - y;

            for (int k = 0; k < 3; ++k)
            {
                for (int jj = 0; jj < 4; ++jj)
                {
                    const int z = y - 1 + jj;
                    unsigned char a0 = getpixel(in, src_width, src_height, z, x, k);
                    unsigned char d0 = getpixel(in, src_width, src_height, z, x - 1, k) - a0;
                    unsigned char d2 = getpixel(in, src_width, src_height, z, x + 1, k) - a0;
                    unsigned char d3 = getpixel(in, src_width, src_height, z, x + 2, k) - a0;
                    unsigned char a1 = -1.0 / 3 * d0 + d2 - 1.0 / 6 * d3;
                    unsigned char a2 = 1.0 / 2 * d0 + 1.0 / 2 * d2;
                    unsigned char a3 = -1.0 / 6 * d0 - 1.0 / 2 * d2 + 1.0 / 6 * d3;
                    C[jj] = a0 + a1 * dx + a2 * dx * dx + a3 * dx * dx * dx;

                    d0 = C[0] - C[1];
                    d2 = C[2] - C[1];
                    d3 = C[3] - C[1];
                    a0 = C[1];
                    a1 = -1.0 / 3 * d0 + d2 - 1.0 / 6 * d3;
                    a2 = 1.0 / 2 * d0 + 1.0 / 2 * d2;
                    a3 = -1.0 / 6 * d0 - 1.0 / 2 * d2 + 1.0 / 6 * d3;
                    out[i * row_stride + j * channels + k] = a0 + a1 * dy + a2 * dy * dy + a3 * dy * dy * dy;
                }
            }
        }
    }
}

If you look at the bicubicresize code carefully, in first loop, variable 'z' will be '-1'.

That will leads referencing negative index in image; out-of memory. and this show memory error in runtime.

I found other codes too, but they also use same kind of indexing.

What's wrong with me? or indexing? I'm so confused right now.

Help me please!

pjh
  • 123
  • 1
  • 7
  • You should run your program in a debugger and step through line by line to find the source of the error. – πάντα ῥεῖ Nov 26 '15 at 13:54
  • imho you should tell us from where you got the code (instead of just claiming that it wrong, which imho is something that is best discuss with the author but not in public) – 463035818_is_not_an_ai Nov 26 '15 at 14:11
  • Sorry, I missed some rules before upload the question. It's worked right now because I added some condition to this code. codes are came from, https://code.google.com/a/eclipselabs.org/p/bicubic-interpolation-image-processing/source/browse/trunk/libimage.c and http://stackoverflow.com/questions/15176972/bi-cubic-interpolation-algorithm-for-image-scaling – pjh Dec 03 '15 at 12:58

0 Answers0