int cc = 4;
if( cc1 == 3 && cc2 == 3 ) {
cc = 3;
}
// Set up the summations:
KahanSummation tr = new KahanSummation();
KahanSummation tg = new KahanSummation();
KahanSummation tb = new KahanSummation();
KahanSummation ta = new KahanSummation();
BigInteger br = BigInteger.valueOf(0);
BigInteger bg = BigInteger.valueOf(0);
BigInteger bb = BigInteger.valueOf(0);
BigInteger ba = BigInteger.valueOf(0);
// TODO How best can we cope with comparisons across colour-spaces?
// Yes, in that this is what getRBG does, but note that getRGB reduced the depth to 8-bits!
//i1.getColorModel().getColorSpace();
//i1.getData().getSample(x, y, b);
// i1.getData().getSampleModel().getSampleSize();
// FIXME So, should we compare in the common colour-space, or compare the data, or both!?
// TODO Decide on which accumulator to use. BitInteger or BigDecimal is probably best.
for (int i = 0; i < i1.getWidth(); i++) {
for (int j = 0; j < i1.getHeight(); j++) {
final Color c1 = new Color(i1.getRGB(i, j));
final Color c2 = new Color(i2.getRGB(i, j));
final int dr = c1.getRed() - c2.getRed();
final int dg = c1.getGreen() - c2.getGreen();
final int db = c1.getBlue() - c2.getBlue();
final int da = c1.getAlpha() - c2.getAlpha();
tr.add( dr*dr );
tg.add( dg*dg );
tb.add( db*db );
ta.add( da*da );
br = br.add( BigInteger.valueOf(dr*dr));
bg = bg.add( BigInteger.valueOf(dg*dg));
bb = bb.add( BigInteger.valueOf(db*db));
ba = ba.add( BigInteger.valueOf(da*da));
}
}
// Compute the mean square error:
double mse = (tr.getSum() + tg.getSum() + tb.getSum() + ta.getSum()) / (i1.getWidth() * i1.getHeight() * cc);
log.info("Mean square error: " + mse);
log.info("Mean square error 2: " + (br.doubleValue() + bg.doubleValue() + bb.doubleValue() + ba.doubleValue()) / (i1.getWidth() * i1.getHeight() * cc) );
if (mse == 0) {
log.warning("mse == 0 and so psnr will be infinity!");
}
// Also do the BigInteger calculation:
System.out.println("Got: br = "+br+", tr = "+tr.getSum());
System.out.println("Got: bg = "+bg+", tg = "+tg.getSum());
System.out.println("Got: bb = "+bb+", tb = "+tb.getSum());
System.out.println("Got: ba = "+ba+", ta = "+ta.getSum());
BigDecimal bmse = new BigDecimal("0.00");
bmse = bmse.add(new BigDecimal(br));
bmse = bmse.add(new BigDecimal(bg));
bmse = bmse.add(new BigDecimal(bb));
bmse = bmse.add(new BigDecimal(ba));