Pixel perfect collision detection

I want to translate a C++ function I found on the net. Here is the C++ code:
// Full object-to-object pixel-level collision detector:

short int Sprite_Collide(sprite_ptr object1, sprite_ptr object2) {
 
    int left1, left2, over_left;
    int right1, right2, over_right;
    int top1, top2, over_top;
    int bottom1, bottom2, over_bottom;
    int over_width, over_height;
    int i, j;
    unsigned char *pixel1, *pixel2;
 
    left1 = object1->x;
    left2 = object2->x;
    right1 = object1->x + object1->width;
    right2 = object2->x + object2->width;
    top1 = object1->y;
    top2 = object2->y;
    bottom1 = object1->y + object1->height;
    bottom2 = object2->y + object2->height;
 
 
    // Trivial rejections:
    if (bottom1 < top2) return(0);
    if (top1 > bottom2) return(0);
   
    if (right1 < left2) return(0);
    if (left1 > right2) return(0);
 
 
    // Ok, compute the rectangle of overlap:
    if (bottom1 > bottom2) over_bottom = bottom2;
    else over_bottom = bottom1;
 
    if (top1 < top2) over_top = top2;
    else over_top = top1;
 
    if (right1 > right2) over_right = right2;
    else over_right = right1;
 
    if (left1 < left2) over_left = left2;
    else over_left = left1;
 
 
    // Now compute starting offsets into both objects' bitmaps:
    i = ((over_top - object1\1->y) * object1->width) + over_left;
    pixel1 = object1->frames[object1->curr_frame] + i;
 
    j = ((over_top - object2->y) * object2->width) + over_left;
    pixel2 = object2->frames[object2->curr_frame] + j;
 
   
    // Now start scanning the whole rectangle of overlap,
    // checking the corresponding pixel of each object's
    // bitmap to see if they're both non-zero:
 
    for (i=0; i < over_height; I++) {
        for (j=0; j < over_width; j++) {
            if (*pixel1 > 0) && (*pixel2 > 0) return(1);
            pixel1++;
            pixel2++;
        }
        pixel1 += (object1->width - over_width);
        pixel2 += (object2->width - over_width);
    }
 
 
    // Worst case!  We scanned through the whole darn rectangle of overlap
    // and couldn't find a single colliding pixel!
 
    return(0);
 
};

I understand most of the code. I get lost at line 45 and downwards.
Some help?
PS I am using BufferedImage, and the getRGB methods are extremely slow. I am thinking to store all the colors in an int array, that is initialized before the game starts.

Is it safe to assume the sprites won’t be scaled in anyway? As in their drawn size matches the image size? Or do you want help with sprites possibly being scaled as well…

I must ask, why are you doing it this way? is this a learning experience? cause it would be a lot easier to just get some pre-existing code thats already in java.

The spriates wont be scaled in any way after they are loaded for the first time.

I cant find any pixel perfect code written in java, so I have to translate this C++ code.

check out this thread:

http://www.gamedev.net/topic/329033-pixel-perfect-collision-detection/

See my implementation. https://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/util/GUtil.java#70

Your implementation is identical to the this one:
http://www.gamedev.net/topic/329033-pixel-perfect-collision-detection/#entry3131661
However, the one above is faster yours, I think.
Why dont you have return true rather than bool = true? It is just useless cycles.

May I ask how exact your method is? The one I just linked, is always two pixels “late”.

@P0jahn I have implemented the method by based on that thread with some changes. I didn’t use return true so it checks all the pixels and so works perfectly. I too noticed the late of the original one. But mine works well and takes 1 to 5 millis to check collision by bsing on the resolutions.