Move cropping code to C++