diff --git a/TIASshot/Cameras/GainCalc.cs b/TIASshot/Cameras/GainCalc.cs
index 1ba799a..9854e5d 100644
--- a/TIASshot/Cameras/GainCalc.cs
+++ b/TIASshot/Cameras/GainCalc.cs
@@ -28,7 +28,15 @@
/// 設定可能な最大値
/// 範囲内に収めた新しいゲイン設定値
internal static int CalcGainValue(int currentValue, float ratio, int rangeMin, int rangeMax) {
- return (int)(0.5 + currentValue * ratio);
+ // 計測値・目標値がともに 0 のとき比率は NaN になり更新量を決められないため現在値を維持する
+ if (float.IsNaN(ratio)) return currentValue;
+
+ // float→int キャストは範囲外・非有限値(Infinity)で未定義値になるため,
+ // キャスト前に double のまま範囲判定する
+ double value = 0.5 + (double)(currentValue * ratio);
+ if (value < rangeMin) return rangeMin;
+ if (value > rangeMax) return rangeMax;
+ return (int)value;
}
}
}
diff --git a/TIASshot/Cameras/IScam.cs b/TIASshot/Cameras/IScam.cs
index da3b09a..9e125f2 100644
--- a/TIASshot/Cameras/IScam.cs
+++ b/TIASshot/Cameras/IScam.cs
@@ -123,9 +123,18 @@
///
/// 白パッチの計測値
protected override void ApplyWhiteBalance(Scalar whitePatch) {
- _whiteBalanceBlue.Value = (int)(0.5 + _whiteBalanceBlue.Value * GetRatio((float)whitePatch.Val0, Config.GetFloat("Calib/Reference/B")));
- _whiteBalanceGreen.Value = (int)(0.5 + _whiteBalanceGreen.Value * GetRatio((float)whitePatch.Val1, Config.GetFloat("Calib/Reference/G")));
- _whiteBalanceRed.Value = (int)(0.5 + _whiteBalanceRed.Value * GetRatio((float)whitePatch.Val2, Config.GetFloat("Calib/Reference/R")));
+ _whiteBalanceBlue.Value = GainCalc.CalcGainValue(
+ _whiteBalanceBlue.Value,
+ GetRatio((float)whitePatch.Val0, Config.GetFloat("Calib/Reference/B")),
+ _whiteBalanceBlue.RangeMin, _whiteBalanceBlue.RangeMax);
+ _whiteBalanceGreen.Value = GainCalc.CalcGainValue(
+ _whiteBalanceGreen.Value,
+ GetRatio((float)whitePatch.Val1, Config.GetFloat("Calib/Reference/G")),
+ _whiteBalanceGreen.RangeMin, _whiteBalanceGreen.RangeMax);
+ _whiteBalanceRed.Value = GainCalc.CalcGainValue(
+ _whiteBalanceRed.Value,
+ GetRatio((float)whitePatch.Val2, Config.GetFloat("Calib/Reference/R")),
+ _whiteBalanceRed.RangeMin, _whiteBalanceRed.RangeMax);
}
///