Skip to content

Commit

Permalink
Updated the code to throw better exceptions on bad arguments
Browse files Browse the repository at this point in the history
git-svn-id: https://android-motion-detection.googlecode.com/svn/trunk@17 996b93ed-3e4d-4b21-1044-74bd19560d1d
  • Loading branch information
[email protected] committed Aug 31, 2011
1 parent 764c359 commit 40efdb2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/com/jwetherell/motion_detection/detection/Comparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public void setDebugMode(int m) {

// compare two images.
public Comparison compare(State s1, State s2) {
if (s1==null || s2==null) return null;
if (s1.getWidth()!=s2.getWidth() || s1.getHeight()!=s2.getHeight()) return null;
if (s1==null || s2==null)
throw new NullPointerException();
if (s1.getWidth()!=s2.getWidth() || s1.getHeight()!=s2.getHeight())
throw new IllegalArgumentException();

// number of boxes
int xBoxes = comparex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Comparison {
private boolean different = false;

public Comparison(State s1, State s2, int[][] variance, boolean different) {
if (variance==null) throw new NullPointerException();
if (variance==null || s1==null || s2==null) throw new NullPointerException();

this.s1 = s1;
this.s2 = s2;
Expand All @@ -30,6 +30,8 @@ public Comparison(State s1, State s2, int[][] variance, boolean different) {
}

public void getChangeIndicator(int[] data, int width, int height, Comparer comparer) {
if (data==null || comparer==null) throw new NullPointerException();

int xBoxes = comparer.getCompareX();
if (xBoxes > s1.getWidth()) xBoxes = s1.getWidth();
int yBoxes = comparer.getCompareY();
Expand Down
17 changes: 12 additions & 5 deletions src/com/jwetherell/motion_detection/detection/MotionDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public static int[] getPrevious() {
}

protected static boolean isDifferentComparingState(int[] first, int width, int height) {
if (first==null || mPrevious==null) return false;
if (first==null) throw new NullPointerException();

if (mPrevious==null) return false;
if (first.length != mPrevious.length) return true;
if (mPreviousWidth != width || mPreviousHeight != height) return true;

Expand All @@ -56,7 +58,9 @@ protected static boolean isDifferentComparingState(int[] first, int width, int h
}

protected static boolean isDifferentComparingLuminescence(int[] first, int width, int height) {
if (first==null || mPrevious==null) return false;
if (first==null) throw new NullPointerException();

if (mPrevious==null) return false;
if (first.length != mPrevious.length) return true;
if (mPreviousWidth != width || mPreviousHeight != height) return true;

Expand Down Expand Up @@ -96,7 +100,9 @@ protected static boolean isDifferentComparingLuminescence(int[] first, int width
}

protected static boolean isDifferentComparingRGB(int[] first, int width, int height) {
if (first==null || mPrevious==null) return false;
if (first==null) throw new NullPointerException();

if (mPrevious==null) return false;
if (first.length != mPrevious.length) return true;
if (mPreviousWidth != width || mPreviousHeight != height) return true;

Expand Down Expand Up @@ -136,7 +142,8 @@ protected static boolean isDifferentComparingRGB(int[] first, int width, int hei
}

public static boolean detect(int[] rgb, int width, int height) {
if (rgb==null) return false;
if (rgb==null) throw new NullPointerException();

int[] original = rgb.clone();

// Create the "mPrevious" picture, the one that will be used to check the next frame against.
Expand All @@ -163,4 +170,4 @@ public static boolean detect(int[] rgb, int width, int height) {

return motionDetected;
}
}
}
2 changes: 1 addition & 1 deletion src/com/jwetherell/motion_detection/detection/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public State(int[] data, int width, int height) {
average = 0;
for (int y = 0, xy=0; y < this.height; y++) {
for (int x = 0; x < this.width; x++, xy++) {
average += data[xy];;
average += data[xy];
}
}
average = (average / (this.width * this.height));
Expand Down

0 comments on commit 40efdb2

Please sign in to comment.