Skip to content

Commit

Permalink
Filter functions should auto convert palette images
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed May 13, 2024
1 parent 0d9921e commit d2968da
Show file tree
Hide file tree
Showing 32 changed files with 108 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/src/filter/adjust_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Image adjustColor(Image src,
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}

contrast = contrast?.clamp(0, 2);
saturation = saturation?.clamp(0, 2);
gamma = gamma?.clamp(0, 1000);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/filter/billboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Image billboard(Image src,
Channel maskChannel = Channel.luminance}) {
const rs = 0.2025; // pow(0.45, 2.0);

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}

for (final frame in src.frames) {
final w = frame.width;
final h = frame.height;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/bleach_bypass.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Image bleachBypass(Image src,
const luminanceR = 0.2125;
const luminanceG = 0.7154;
const luminanceB = 0.0721;
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
for (final p in frame) {
final r = p.rNormalized;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/bulge_distortion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Image bulgeDistortion(Image src,
Interpolation interpolation = Interpolation.nearest,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final orig = frame.clone(noAnimation: true);
final w = frame.width;
Expand Down
7 changes: 6 additions & 1 deletion lib/src/filter/bump_to_normal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import '../image/image.dart';
/// height and 1 a high value. The optional [strength] parameter allows to set
/// the strength of the normal image.
Image bumpToNormal(Image src, {num strength = 2.0}) {
final dest = Image.from(src);
Image dest;
if (src.hasPalette) {
dest = src.convert(numChannels: src.numChannels);
} else {
dest = Image.from(src);
}

final mx = src.maxChannelValue;
for (final frame in src.frames) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/chromatic_aberration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import '../util/math_util.dart';
/// Apply chromatic aberration filter to the image.
Image chromaticAberration(Image src,
{int shift = 5, Image? mask, Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final orig = frame.clone(noAnimation: true);
final w = frame.width - 1;
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/color_halftone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Image colorHalftone(Image src,
Image? mask,
Channel maskChannel = Channel.luminance}) {
angle = angle * 0.0174533;

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
num pattern(int x, int y, int cx, int cy, num angle) {
final scale = 3.14159 / size;
final s = sin(angle);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/color_offset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Image colorOffset(Image src,
num alpha = 0,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
for (final p in frame) {
final msk = mask?.getPixel(p.x, p.y).getChannelNormalized(maskChannel);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/contrast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Image contrast(Image src,
if (contrast == 100.0) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
if (contrast != _lastContrast) {
_lastContrast = contrast;

Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/convolution.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Image convolution(Image src,
num amount = 1,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final tmp = Image.from(src);
for (final frame in src.frames) {
final tmpFrame = tmp.frames[frame.frameIndex];
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/copy_image_channels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Image copyImageChannels(Image src,
Channel? alpha,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final dx = from.width / src.width;
final dy = from.height / src.height;
final fromPixel = from.getPixel(0, 0);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/dot_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Image dotScreen(Image src,
num amount = 1,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
angle = angle * 0.0174533;
final s = sin(angle);
final c = cos(angle);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/edge_glow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Image edgeGlow(Image src,
if (amount == 0.0) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final orig = Image.from(frame, noAnimation: true);
final width = frame.width;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/gamma.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Image gamma(Image src,
{required num gamma,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
for (final p in frame) {
final msk = mask?.getPixel(p.x, p.y).getChannelNormalized(maskChannel);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/grayscale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import '../util/math_util.dart';
/// Convert the image to grayscale.
Image grayscale(Image src,
{num amount = 1, Image? mask, Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
if (frame.hasPalette) {
final p = frame.palette!;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/hexagon_pixelate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Image hexagonPixelate(Image src,
num amount = 1,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final w = frame.width - 1;
final h = frame.height - 1;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/invert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import '../util/math_util.dart';
/// Invert the colors of the [src] image.
Image invert(Image src,
{Image? mask, Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final max = src.maxChannelValue;
for (final frame in src.frames) {
if (src.hasPalette) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/luminance_threshold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Image luminanceThreshold(Image src,
num amount = 1,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
for (final p in frame) {
final y =
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/monochrome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Image monochrome(Image src,
if (amount == 0) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final nr = color?.rNormalized ?? 0.45;
final ng = color?.gNormalized ?? 0.6;
final nb = color?.bNormalized ?? 0.3;
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/noise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Image noise(Image image, num sigma,
if (nSigma == 0.0 && type != NoiseType.poisson) {
return image;
}

if (image.hasPalette) {
image = image.convert(numChannels: image.numChannels);
}
if (nSigma < 0.0 || type == NoiseType.saltAndPepper) {
final mM = minMax(image);
m = mM[0];
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/normalize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Image normalize(Image src,
if (mn == mx) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final fm = mn.toDouble();
final fM = mx.toDouble();

Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/pixelate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Image pixelate(Image src,
if (size <= 1) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final w = frame.width;
final h = frame.height;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/remap_colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Image remapColors(Image src,
Channel green = Channel.green,
Channel blue = Channel.blue,
Channel alpha = Channel.alpha}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final List<num> l = [0, 0, 0, 0, 0];
for (final frame in src.frames) {
for (final p in frame) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/scale_rgba.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Image scaleRgba(Image src,
final dg = scale.gNormalized;
final db = scale.bNormalized;
final da = scale.aNormalized;
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
for (final p in frame) {
final msk = mask?.getPixel(p.x, p.y).getChannelNormalized(maskChannel);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/separable_convolution.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Image separableConvolution(Image src,
{required SeparableKernel kernel,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final tmp = Image.from(src);
// Apply the filter horizontally
kernel
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/sepia.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Image sepia(Image src,
if (amount == 0) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
for (final p in frame) {
final r = p.rNormalized;
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/sketch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Image sketch(Image src,
if (amount == 0) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final width = frame.width;
final height = frame.height;
Expand Down
4 changes: 3 additions & 1 deletion lib/src/filter/sobel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Image sobel(Image src,
if (amount == 0.0) {
return src;
}

if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final orig = Image.from(frame, noAnimation: true);
final width = frame.width;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/solarize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Image solarize(Image src,
{required int threshold, SolarizeMode mode = SolarizeMode.highlights}) {
final max = src.maxChannelValue;
final thresholdRange = (max * (threshold / 255)).toInt();
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
if (src.hasPalette) {
final p = frame.palette!;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/stretch_distortion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Image stretchDistortion(Image src,
Interpolation interpolation = Interpolation.nearest,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
for (final frame in src.frames) {
final orig = frame.clone(noAnimation: true);
final w = frame.width - 1;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/filter/vignette.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Image vignette(Image src,
Color? color,
Image? mask,
Channel maskChannel = Channel.luminance}) {
if (src.hasPalette) {
src = src.convert(numChannels: src.numChannels);
}
final h = src.height - 1;
final w = src.width - 1;
final cr = color?.rNormalized ?? 0;
Expand Down
10 changes: 10 additions & 0 deletions test/formats/ico_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import '../_test_util.dart';
void main() {
group('Format', () {
group('ico', () {
test('encode palette', () async {
var img = await decodePngFile('test/_data/png/buck_8.png');
img = copyResize(img!, width: 256);
img = vignette(img);
final ico = IcoEncoder().encode(img);
File('$testOutputPath/ico/buck_8.ico')
..createSync(recursive: true)
..writeAsBytesSync(ico);
});

test('encode', () {
final image = Image(width: 64, height: 64)
..clear(ColorRgb8(100, 200, 255));
Expand Down

0 comments on commit d2968da

Please sign in to comment.