Skip to content

Commit

Permalink
Merge pull request #652 from refotografia/main
Browse files Browse the repository at this point in the history
Added modes to contrast filter
  • Loading branch information
brendan-duncan committed May 22, 2024
2 parents a43a391 + dae22d0 commit 7fa05e9
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions lib/src/filter/contrast.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:math';
import 'dart:typed_data';

import '../color/channel.dart';
Expand All @@ -7,31 +8,50 @@ import '../util/math_util.dart';
num? _lastContrast;
late Uint8List _contrast;

enum ContrastMode { proportional, scurve }

/// Set the [contrast] level for the image [src].
///
/// [contrast] values below 100 will decrees the contrast of the image,
/// and values above 100 will increase the contrast. A contrast of of 100
/// [contrast] values below 100 will decrease the contrast of the image,
/// and values above 100 will increase the contrast. A contrast of 100
/// will have no affect.
Image contrast(Image src,
{required num contrast,
Image? mask,
Channel maskChannel = Channel.luminance}) {
Channel maskChannel = Channel.luminance,
ContrastMode mode = ContrastMode.proportional}) {
if (contrast == 100.0) {
return src;
}
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
if (contrast != _lastContrast) {
_lastContrast = contrast;

contrast = contrast / 100.0;
contrast = contrast * contrast;
_contrast = Uint8List(256);
for (var i = 0; i < 256; ++i) {
_contrast[i] = (((((i / 255.0) - 0.5) * contrast) + 0.5) * 255.0)
.clamp(0, 255)
.toInt();
if (mode == ContrastMode.proportional) {
if (contrast != _lastContrast) {
_lastContrast = contrast;

contrast = contrast / 100.0;
contrast = contrast * contrast;
_contrast = Uint8List(256);
for (var i = 0; i < 256; ++i) {
_contrast[i] = (((((i / 255.0) - 0.5) * contrast) + 0.5) * 255.0)
.clamp(0, 255)
.toInt();
}
} else {
if (contrast != _lastContrast) {
_lastContrast = contrast;
/// 0.12 is an arbitrary adjustment to use 100 as mid point
contrast = (contrast / 100.0) - 0.12;
_contrast = Uint8List(256);
for (var i = 0; i < 256; ++i) {
_contrast[i] =
(((tan(((i / 128) - 1) * contrast) + 1.0) / 2.0) * 255.0)
.clamp(0, 255)
.toInt();
}
}
}
}

Expand Down

0 comments on commit 7fa05e9

Please sign in to comment.