Skip to content

Commit

Permalink
Add BinaryQuantizer
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed May 11, 2024
1 parent 2c652ee commit 61e6ef3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export 'src/transform/copy_resize_crop_square.dart';
export 'src/transform/copy_rotate.dart';
export 'src/transform/flip.dart';
export 'src/transform/trim.dart';
export 'src/util/binary_quantizer.dart';
export 'src/util/clip_line.dart';
export 'src/util/color_util.dart';
export 'src/util/file_access.dart';
Expand All @@ -238,4 +239,5 @@ export 'src/util/neural_quantizer.dart';
export 'src/util/octree_quantizer.dart';
export 'src/util/output_buffer.dart';
export 'src/util/point.dart';
export 'src/util/quantizer.dart';
export 'src/util/random.dart';
4 changes: 3 additions & 1 deletion lib/src/filter/dither_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ Image ditherImage(Image image,
for (var i = i0; i != i1; i += direction) {
final x1 = ds[i][1].toInt();
final y1 = ds[i][2].toInt();
if ((x1 + x) >= 0 && (x1 + x) < width && (y1 + y) >= 0 &&
if ((x1 + x) >= 0 &&
(x1 + x) < width &&
(y1 + y) >= 0 &&
(y1 + y) < height) {
final d = ds[i][0];
final nx = x + x1;
Expand Down
7 changes: 5 additions & 2 deletions lib/src/filter/quantize.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import '../image/image.dart';
import '../util/binary_quantizer.dart';
import '../util/neural_quantizer.dart';
import '../util/octree_quantizer.dart';
import '../util/quantizer.dart';
import 'dither_image.dart';

enum QuantizeMethod { neuralNet, octree }
enum QuantizeMethod { neuralNet, octree, binary }

/// Quantize the number of colors in image to 256.
Image quantize(Image src,
Expand All @@ -16,8 +17,10 @@ Image quantize(Image src,

if (method == QuantizeMethod.octree || numberOfColors < 4) {
quantizer = OctreeQuantizer(src, numberOfColors: numberOfColors);
} else {
} else if (method == QuantizeMethod.octree) {
quantizer = NeuralQuantizer(src, numberOfColors: numberOfColors);
} else {
quantizer = BinaryQuantizer();
}

return ditherImage(src,
Expand Down
9 changes: 7 additions & 2 deletions lib/src/formats/gif_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:typed_data';

import '../filter/dither_image.dart';
import '../image/image.dart';
import '../util/binary_quantizer.dart';
import '../util/image_exception.dart';
import '../util/neural_quantizer.dart';
import '../util/octree_quantizer.dart';
Expand Down Expand Up @@ -39,8 +40,10 @@ class GifEncoder extends Encoder {
if (quantizerType == QuantizerType.neural) {
_lastColorMap = NeuralQuantizer(image,
numberOfColors: numColors, samplingFactor: samplingFactor);
} else {
} else if (quantizerType == QuantizerType.octree) {
_lastColorMap = OctreeQuantizer(image, numberOfColors: numColors);
} else if (quantizerType == QuantizerType.binary) {
_lastColorMap = BinaryQuantizer();
}

_lastImage = ditherImage(image,
Expand Down Expand Up @@ -71,8 +74,10 @@ class GifEncoder extends Encoder {
if (quantizerType == QuantizerType.neural) {
_lastColorMap = NeuralQuantizer(image,
numberOfColors: numColors, samplingFactor: samplingFactor);
} else {
} else if (quantizerType == QuantizerType.octree) {
_lastColorMap = OctreeQuantizer(image, numberOfColors: numColors);
} else if (quantizerType == QuantizerType.binary) {
_lastColorMap = BinaryQuantizer();
}

_lastImage = ditherImage(image,
Expand Down
31 changes: 31 additions & 0 deletions lib/src/util/binary_quantizer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import '../color/color.dart';
import '../color/color_uint8.dart';
import '../image/palette.dart';
import '../image/palette_uint8.dart';
import '../util/color_util.dart';
import 'quantizer.dart';

class BinaryQuantizer extends Quantizer {
final Palette _palette;

BinaryQuantizer() : _palette = PaletteUint8(2, 3) {
_palette.setRgb(1, 255, 255, 255);
}

@override
Palette get palette => _palette;

@override
Color getQuantizedColor(Color c) => c.luminanceNormalized < 0.5
? ColorRgb8(_palette.getRed(0) as int, _palette.getGreen(0) as int,
_palette.getBlue(0) as int)
: ColorRgb8(_palette.getRed(1) as int, _palette.getGreen(1) as int,
_palette.getBlue(1) as int);

@override
int getColorIndex(Color c) => c.luminanceNormalized < 0.5 ? 0 : 1;

@override
int getColorIndexRgb(int r, int g, int b) =>
getLuminanceRgb(r, g, b) < 0.5 ? 0 : 1;
}
2 changes: 1 addition & 1 deletion lib/src/util/quantizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '../color/color.dart';
import '../image/image.dart';
import '../image/palette.dart';

enum QuantizerType { octree, neural }
enum QuantizerType { octree, neural, binary }

/// Abstract class for color quantizers, which reduce the total number of colors
/// used by an image to a given maximum, used to convert images to palette
Expand Down
Binary file added test/_data/png/david.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions test/filter/quantize_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ void main() {
..writeAsBytesSync(encodePng(q1));

final i2 = decodePng(bytes)!;
final q2 =
quantize(grayscale(i2), numberOfColors: 2,
dither: DitherKernel.floydSteinberg);
final q2 = quantize(grayscale(i2),
numberOfColors: 2, dither: DitherKernel.floydSteinberg);
File('$testOutputPath/filter/quantize_neural_dither.png')
..createSync(recursive: true)
..writeAsBytesSync(encodePng(q2));

final i3 = decodePng(File('test/_data/png/david.png').readAsBytesSync())!;
final q3 = quantize(i3,
method: QuantizeMethod.binary, dither: DitherKernel.floydSteinberg);
File('$testOutputPath/filter/quantize_binary.png')
..createSync(recursive: true)
..writeAsBytesSync(encodePng(q3));
});
});
}

0 comments on commit 61e6ef3

Please sign in to comment.