- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package punkbuster.scraper.pattern;
- import java.awt.Color;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- *
- * @author Andreas Böck
- */
- public class RectangleMatcher {
- public Rectangle match(List<Rectangle> m1, Rectangle m2) throws Exception{
- //List<Integer> score = new ArrayList<Integer>();
- // System.out.println(m1.size());
- for (int i=0; i<m1.size();i++){
- Integer tempScore = new Integer(0);
- if (m1.get(i).getHeight() != m2.getHeight() || m1.get(i).getWidth()!= m2.getWidth()){
- throw new Exception("Scraping error! Compared Rectangles are not of same size.");
- }
- if (m1.get(i).getHeight() <=0 || m1.get(i).getWidth() <=0 ){
- throw new Exception("Scraping error! Rectangles must be of size greater 0.");
- }
- else{
- for ( int x=0; x<m2.getWidth();x++){
- for (int y=0; y<m2.getHeight();y++){
- tempScore +=(calcPoints(m1.get(i).getPixel(x, y),m2.getPixel(x, y)));
- }
- }
- }
- m1.get(i).setScore(tempScore);
- // System.out.println("Score von rect "+i+ " ist "+tempScore);
- }
- Collections.sort(m1);
- // System.out.println("Score+ "+m1.get(0).getScore());
- if (m1.size()>0) {
- return m1.get(0);
- } //Return Colour with best (lowest) score.
- else {
- return null;
- }
- }
- private Integer calcPoints(Color c1, Color c2){
- Integer pts = new Integer(0);
- pts += new Integer(absoluteValue(c1.getRed() - c2.getRed()));
- pts += new Integer(absoluteValue(c1.getBlue() - c2.getBlue()));
- pts += new Integer(absoluteValue(c1.getGreen() - c2.getGreen()));
- return pts;
- }
- private int absoluteValue(int e){
- if (e<0) {return -e;}
- else{
- return e;
- }
- }
- }
