There's possibly a better algorithm than walking every pixel, but if raw speed is of importance, a good graphics library in C is probably best. The only "library" that I can think of that has this functionality built-in and has good perl bindings is the "autocrop" function in
Gimp.