- result += sizeOfBlackWhiteBlackRun(fromX, fromY, fromX - (toX - fromX), fromY - (toY - fromY));
+
+ // Now count other way -- don't run off image though of course
+ int otherToX = fromX - (toX - fromX);
+ if (otherToX < 0) {
+ otherToX = 0;
+ } else if (otherToX >= image.getWidth()) {
+ otherToX = image.getWidth();
+ }
+ int otherToY = fromY - (toY - fromY);
+ if (otherToY < 0) {
+ otherToY = 0;
+ } else if (otherToY >= image.getHeight()) {
+ otherToY = image.getHeight();
+ }
+ result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);