@@ -341,9 +341,9 @@ static int lua_camera_auto(lua_State *L)
341341
342342 // Default white balance settings
343343 double white_balance_speed = 0.5 ;
344- double K = 4166400.0 ; // TODO rename and expose these for user override
345- double t1 = 50 ; // TODO rename and expose these for user override
346- double t2 = 200 ; // TODO rename and expose these for user override
344+ double brightness_constant = 4166400.0 ;
345+ double white_balance_min_activation = 50 ;
346+ double white_balance_max_activation = 200 ;
347347
348348 // Allow user to over-ride these if desired
349349 if (lua_istable (L , 1 ))
@@ -538,20 +538,22 @@ static int lua_camera_auto(lua_State *L)
538538 check_error (i2c_write (CAMERA , 0x350B , 0xFF , analog_gain ).fail );
539539
540540 // Auto white balance based on full scene matrix
541- double max_p = matrix_r / last .red_gain > matrix_g / last .green_gain // TODO rename this
542- ? (matrix_r / last .red_gain > matrix_b / last .blue_gain
543- ? matrix_r / last .red_gain
544- : matrix_b / last .blue_gain )
545- : (matrix_g / last .green_gain > matrix_b / last .blue_gain
546- ? matrix_g / last .green_gain
547- : matrix_b / last .blue_gain );
548-
549- double red_gain = max_p / matrix_r * last .red_gain ;
550- double green_gain = max_p / matrix_g * last .green_gain ;
551- double blue_gain = max_p / matrix_b * last .blue_gain ;
552- double scene_brightness = K * matrix_average /
541+ double max_rgb = matrix_r / last .red_gain > matrix_g / last .green_gain
542+ ? (matrix_r / last .red_gain > matrix_b / last .blue_gain
543+ ? matrix_r / last .red_gain
544+ : matrix_b / last .blue_gain )
545+ : (matrix_g / last .green_gain > matrix_b / last .blue_gain
546+ ? matrix_g / last .green_gain
547+ : matrix_b / last .blue_gain );
548+
549+ double red_gain = max_rgb / matrix_r * last .red_gain ;
550+ double green_gain = max_rgb / matrix_g * last .green_gain ;
551+ double blue_gain = max_rgb / matrix_b * last .blue_gain ;
552+ double scene_brightness = brightness_constant * matrix_average /
553553 (last .shutter * last .analog_gain );
554- double b_speed = (scene_brightness - t1 ) / (t2 - t1 ); // TODO rename this
554+ double blending_factor = (scene_brightness - white_balance_min_activation ) /
555+ (white_balance_max_activation -
556+ white_balance_min_activation );
555557
556558 if (red_gain > 1023.0 )
557559 {
@@ -565,24 +567,24 @@ static int lua_camera_auto(lua_State *L)
565567 {
566568 blue_gain = 1023.0 ;
567569 }
568- if (b_speed > 1.0 )
570+ if (blending_factor > 1.0 )
569571 {
570- b_speed = 1.0 ;
572+ blending_factor = 1.0 ;
571573 }
572- if (b_speed < 0.0 )
574+ if (blending_factor < 0.0 )
573575 {
574- b_speed = 0.0 ;
576+ blending_factor = 0.0 ;
575577 }
576578
577- last .red_gain = b_speed * white_balance_speed *
579+ last .red_gain = blending_factor * white_balance_speed *
578580 (red_gain - last .red_gain ) +
579581 last .red_gain ;
580582
581- last .green_gain = b_speed * white_balance_speed *
583+ last .green_gain = blending_factor * white_balance_speed *
582584 (green_gain - last .green_gain ) +
583585 last .green_gain ;
584586
585- last .blue_gain = b_speed * white_balance_speed *
587+ last .blue_gain = blending_factor * white_balance_speed *
586588 (blue_gain - last .blue_gain ) +
587589 last .blue_gain ;
588590
0 commit comments