Skip to content

Commit 86d4101

Browse files
Clean-up solarize code
1 parent ddc4cae commit 86d4101

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

doc/filters.md

+9
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ Image sobel(Image src, { num amount = 1, Image? mask, Channel maskChannel = Chan
328328

329329
![sobel](images/filter/sobel.png)
330330

331+
### [solarize](https://pub.dev/documentation/image/latest/image/solarize.html)
332+
333+
```dart
334+
enum SolarizeMode { highlights, shadows }
335+
Image solarize(Image src, { required int threshold, SolarizeMode mode = SolarizeMode.highlights })
336+
```
337+
338+
![solarize](images/filter/solarize_highlights.png)
339+
331340
### [stretchDistortion](https://pub.dev/documentation/image/latest/image/stretchDistortion.html)
332341

333342
```dart
106 KB
Loading
106 KB
Loading

lib/image.dart

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export 'src/filter/sepia.dart';
7777
export 'src/filter/sketch.dart';
7878
export 'src/filter/smooth.dart';
7979
export 'src/filter/sobel.dart';
80+
export 'src/filter/solarize.dart';
8081
export 'src/filter/stretch_distortion.dart';
8182
export 'src/filter/vignette.dart';
8283
export 'src/font/arial_14.dart';

lib/src/filter/solarize.dart

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import '../image/image.dart';
22
import '../util/min_max.dart';
33

4-
enum Modes {
5-
highlights,
6-
shadows
7-
}
8-
/// Solarize the colors of the [src] image - Started from invert.dart file.
9-
Image solarize(Image src, {required int threshold, required int md}) {
10-
/// threshold should be int from 1 to 254; mode should either '0' or '1'
11-
/// mode '0' is normal solarization, bright objetcs become black
12-
/// mode '1' will solarize the shadows like a Man Ray photograph
4+
enum SolarizeMode { highlights, shadows }
5+
6+
/// Solarize the colors of the [src] image.
7+
/// {threshold} should be int from 1 to 254. If {mode} is
8+
/// SolarizeMode.highlights, bright objects become black, otherwise it will
9+
/// solarize shadows.
10+
Image solarize(Image src,
11+
{required int threshold, SolarizeMode mode = SolarizeMode.highlights}) {
1312
final max = src.maxChannelValue;
14-
final trld = (max * (threshold / 255)).toInt();
15-
final mode = Modes.values[md].toString();
13+
final thresholdRange = (max * (threshold / 255)).toInt();
1614
for (final frame in src.frames) {
1715
if (src.hasPalette) {
1816
final p = frame.palette!;
1917
final numColors = p.numColors;
2018
for (var i = 0; i < numColors; ++i) {
21-
if (mode == "highlights") {
22-
if (p.getGreen(i) > trld) {
19+
if (mode == SolarizeMode.highlights) {
20+
if (p.getGreen(i) > thresholdRange) {
2321
final r = max - p.getRed(i);
2422
final g = max - p.getGreen(i);
2523
final b = max - p.getBlue(i);
@@ -31,7 +29,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
3129
p.setRgb(i, r, g, b);
3230
}
3331
} else {
34-
if (p.getGreen(i) < trld) {
32+
if (p.getGreen(i) < thresholdRange) {
3533
final r = max - p.getRed(i);
3634
final g = max - p.getGreen(i);
3735
final b = max - p.getBlue(i);
@@ -47,8 +45,8 @@ Image solarize(Image src, {required int threshold, required int md}) {
4745
} else {
4846
if (max != 0.0) {
4947
for (final p in frame) {
50-
if (mode == "highlights") {
51-
if (p.g > trld) {
48+
if (mode == SolarizeMode.highlights) {
49+
if (p.g > thresholdRange) {
5250
p
5351
..r = max - p.r
5452
..g = max - p.g
@@ -60,7 +58,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
6058
..b = p.b;
6159
}
6260
} else {
63-
if (p.g < trld) {
61+
if (p.g < thresholdRange) {
6462
p
6563
..r = max - p.r
6664
..g = max - p.g
@@ -77,8 +75,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
7775
}
7876
}
7977

80-
/// I used code from normalize here with the original
81-
/// max value and zero to improve contrast
78+
/// max value and zero are used to improve contrast
8279
const num a = 0;
8380
final num b = max;
8481

@@ -94,7 +91,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
9491
final fM = mx.toDouble();
9592

9693
if (mn != a || mx != b) {
97-
for (var frame in src.frames) {
94+
for (final frame in src.frames) {
9895
for (final p in frame) {
9996
p
10097
..r = (p.r - fm) / (fM - fm) * (b - a) + a

test/filter/solarize_test.dart

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'dart:io';
2+
import 'package:image/image.dart';
3+
import 'package:test/test.dart';
4+
5+
import '../_test_util.dart';
6+
7+
void main() {
8+
group('Filter', () {
9+
test('solarize highlights', () {
10+
final bytes = File('test/_data/png/buck_24.png').readAsBytesSync();
11+
final i0 = decodePng(bytes)!;
12+
solarize(i0, threshold: 100);
13+
File('$testOutputPath/filter/solarize_highlights.png')
14+
..createSync(recursive: true)
15+
..writeAsBytesSync(encodePng(i0));
16+
});
17+
18+
test('solarize shadows', () {
19+
final bytes = File('test/_data/png/buck_24.png').readAsBytesSync();
20+
final i0 = decodePng(bytes)!;
21+
solarize(i0, threshold: 100, mode: SolarizeMode.shadows);
22+
File('$testOutputPath/filter/solarize_shadows.png')
23+
..createSync(recursive: true)
24+
..writeAsBytesSync(encodePng(i0));
25+
});
26+
});
27+
}

0 commit comments

Comments
 (0)