-2

I have this for-loop:

y1 = 0; y2 = 3264; x1 = 0; x2 = 4928; uc = 1; vc = 1; scale = 1;

for (var y = y1; y < y2; y++) {
    for (var x = x1; x < x2; x++) {
        sumR = 0;
        sumG = 0;
        sumB = 0;
        i = 0;

        for (var v = -vc; v <= vc; v++) {
            for (var u = -uc; u <= uc; u++) {
                if (kernel[i] != 0) {
                    var tempX = x + u < 0 ? 0 : x + u;
                    var tempY = y + v < 0 ? 0 : y + v;
                    tempX = tempX >= width ? width - 1 : tempX;
                    tempY = tempY >= height ? height - 1 : tempY;

                    sumR += pixels.data[((tempY * (pixels.width*4)) + (tempX * 4)) + 0] * kernel[i];
                    sumG += pixels.data[((tempY * (pixels.width*4)) + (tempX * 4)) + 1] * kernel[i];
                    sumB += pixels.data[((tempY * (pixels.width*4)) + (tempX * 4)) + 2] * kernel[i];
                }
                i++;
            }
        }
        tempArray.push(sumR * scale, sumG * scale, sumB * scale, 255);
    }
    console.log(y + "|" + y2);
}

So basically it's about image processing, the loop stops at y = 3115 without any error, everything after the loop isn't computed it just "crashes" there. Do you guys have any ideas how this could happen? Can there be a problem with memory?

UPDATE: I think I made this abit unclear: if I use this algorithm for a image with size y2 = 1000 and x2 = 1000 everything is working fine. But if the images get bigger it just stopps working, there is no errormessage in the console!

4

2 回答 2

0
  • uc + vc = 1; is a invalid statement,
  • uc and vc don't seem to be defined anywhere,
  • scale isn't defined anywhere.

  • The second x1 here should probably be x2:

    x1 = 0; x1 = 4928;
    //Should probably be:
    x1 = 0; x2 = 4928;
    

This pretty much comes down to: "debug your code".

于 2012-11-28T08:35:42.217 回答
0

Okay finally I found the problem,

I initialized the array this way: var tempArray = []; above the for-loops, next step was that I tried to initialize it this way : var tempArray = new Array(width * height * 4)

The browser just stopped at that position now and didn't even enter the for-loops. So I guess the Array is just to big to create.

Solution: I am using a Typed Array now and everything is working:

var tempArray = new Uint8ClampedArray(width * height * 4);
于 2012-11-30T12:36:36.937 回答