00001 #include "FCam/AutoWhiteBalance.h"
00002 #include "FCam/Sensor.h"
00003
00004 #include "Debug.h"
00005
00006
00007 namespace FCam {
00008
00009 void autoWhiteBalance(Shot *s, const Frame &f,
00010 int minWB,
00011 int maxWB,
00012 float smoothness) {
00013 if (!s) return;
00014
00015 if (!f.histogram().valid()) return;
00016
00017
00018 int buckets = f.histogram().buckets();
00019
00020
00021
00022 int rawRGB[] = {0, 0, 0};
00023
00024 for (int b = 0; b < buckets; b++) {
00025
00026
00027 rawRGB[0] += f.histogram()(b, 1)*b;
00028 rawRGB[1] += f.histogram()(b, 0)*b;
00029 rawRGB[2] += f.histogram()(b, 2)*b;
00030 }
00031
00032
00033
00034
00035
00036 float RGB3200[] = {0, 0, 0};
00037 float RGB7000[] = {0, 0, 0};
00038 float d3200[12];
00039 float d7000[12];
00040 f.rawToRGBColorMatrix(3200, d3200);
00041 f.rawToRGBColorMatrix(7000, d7000);
00042
00043 for (int i = 0; i < 3; i++) {
00044 for (int j = 0; j < 3; j++) {
00045 RGB3200[i] += d3200[i*4+j]*rawRGB[j];
00046 RGB7000[i] += d7000[i*4+j]*rawRGB[i];
00047 }
00048 }
00049
00050 float alpha = (RGB3200[2] - RGB3200[0])/(RGB7000[0] - RGB3200[0] + RGB3200[2] - RGB7000[2]);
00051
00052 int wb = int(alpha * (7000-3200) + 3200);
00053
00054 if (wb < minWB) wb = minWB;
00055 if (wb > maxWB) wb = maxWB;
00056
00057 s->whiteBalance = smoothness * s->whiteBalance + (1-smoothness) * wb;
00058 }
00059
00060
00061 }