contrast.dart 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import 'dart:typed_data';
  2. import 'base.dart';
  3. /// Vid对比度处理器
  4. class VidContrastProcessor extends VidFrameProcessor {
  5. VidContrastProcessor(this.contrast) {
  6. _setActualContrast();
  7. }
  8. /// 对比度 [-99~99]
  9. final int contrast;
  10. late final double _actualContrast;
  11. void _setActualContrast() {
  12. double value = contrast.toDouble();
  13. if (value < -100) value = -100;
  14. if (value > 100) value = 100;
  15. value = (100.0 + value) / 100.0;
  16. value *= value;
  17. _actualContrast = value;
  18. }
  19. @override
  20. Uint8List process(Uint8List bytes, int position) {
  21. final i = position;
  22. final _contrast = _actualContrast;
  23. double b = bytes[i].toDouble();
  24. double g = bytes[i + 1].toDouble();
  25. double r = bytes[i + 2].toDouble();
  26. //process b
  27. b = b / 255.0;
  28. b -= 0.5;
  29. b *= _contrast;
  30. b += 0.5;
  31. b *= 255;
  32. if (b < 0) b = 0;
  33. if (b > 255) b = 255;
  34. //process g
  35. g = g / 255.0;
  36. g -= 0.5;
  37. g *= _contrast;
  38. g += 0.5;
  39. g *= 255;
  40. if (g < 0) g = 0;
  41. if (g > 255) g = 255;
  42. //process r
  43. r = r / 255.0;
  44. r -= 0.5;
  45. r *= _contrast;
  46. r += 0.5;
  47. r *= 255;
  48. if (r < 0) r = 0;
  49. if (r > 255) r = 255;
  50. bytes[i] = b.toInt();
  51. bytes[i + 1] = g.toInt();
  52. bytes[i + 2] = r.toInt();
  53. return bytes;
  54. }
  55. @override
  56. String toString() => 'VidContrastProcessor: $contrast';
  57. }