Skip to content

Commit

Permalink
Merge pull request #38 from skkallayath/develop
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
skkallayath committed Aug 2, 2020
2 parents 7073b0c + a684f17 commit 4a4f55d
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 53 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@
## [1.0.6] - 25 July 2020

* Image Selector Widget Fixes

## [2.0.0] - 02 Aug 2020

* Convolution Filters Functionality Added
* More information about creating subfilters and filters in README
89 changes: 80 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,101 @@ class _MyAppState extends State<MyApp> {
| <img width="1604" alt="Low Pass" src="https://raw.githubusercontent.com/skkallayath/photofilters/master/exampleimages/convolution/Low%20Pass%205x5.jpg"> Low Pass | <img width="1604" alt="High Pass" src="https://raw.githubusercontent.com/skkallayath/photofilters/master/exampleimages/convolution/High%20Pass%203x3.jpg"> High Pass | <img width="1604" alt="Mean" src="https://raw.githubusercontent.com/skkallayath/photofilters/master/exampleimages/convolution/Mean%205x5.jpg"> Mean | |


### Custom filters
## Filters

You can create your own custom filters too
There are two types of filters. `ImageFilter` and `ColorFilter`.

### Image Filter

Image filter applies its subfilters directly to the whole image one by one. It is computationally expensive since the complexity & time increases as the number of subfilters increases.

You can create your own custom image filter as like this:

```dart
var customFilter = new ImageFilter(name: "Custom Filter");
customFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
import 'package:photofilters/filters/image_filters.dart';
var customImageFilter = new ImageFilter(name: "Custom Image Filter");
customImageFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
coloredEdgeDetectionKernel,
));
customFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
customImageFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
gaussian7x7Kernel,
));
customFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
customImageFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
sharpenKernel,
));
customFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
customImageFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
highPass3x3Kernel,
));
customFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
customImageFilter.subFilters.add(ConvolutionSubFilter.fromKernel(
lowPass3x3Kernel,
));
customFilter.subFilters.add(SaturationSubFilter(0.5));
customImageFilter.subFilters.add(SaturationSubFilter(0.5));
```

You can also inherit the ImageFilter class to create another image filter.

```dart
class MyImageFilter extends ImageFilter {
MyImageFilter(): super(name: "My Custom Image Filter") {
this.addSubFilter(ConvolutionSubFilter.fromKernel(sharpenKernel));
}
}
```

### Color Filter

Color filter applies its subfilters to each pixel one by one. It is computationally less expensive than the ImageFilter. It will loop through the image pixels only once irrespective of the number of subfilters.


You can create your own custom color filter as like this:

```dart
import 'package:photofilters/filters/color_filters.dart';
var customColorFilter = new ColorFilter(name: "Custom Color Filter");
customColorFilter.addSubFilter(SaturationSubFilter(0.5));
customColorFilter
.addSubFilters([BrightnessSubFilter(0.5), HueRotationSubFilter(30)]);
```

You can inherit the ColorFilter class too

```dart
class MyColorFilter extends ColorFilter {
MyColorFilter() : super(name: "My Custom Color Filter") {
this.addSubFilter(BrightnessSubFilter(0.8));
this.addSubFilter(HueRotationSubFilter(30));
}
}
```

## Sub filters

There are two types of subfilters. One can be added to the `ColorFilter` and the other can be added to the `ImageFilter`. You can inherit `ColorSubFilter` class to implement the former and you can use the `ImageSubFilter` mixin to implement the latter. You can create a same subfilter that can be used for both Image and Color Filters. The `BrightnessSubFilter` is an example of this.

```dart
class BrightnessSubFilter extends ColorSubFilter with ImageSubFilter {
final num brightness;
BrightnessSubFilter(this.brightness);
///Apply the [BrightnessSubFilter] to an Image.
@override
void apply(Uint8List pixels, int width, int height) =>
image_filter_utils.brightness(pixels, brightness);
///Apply the [BrightnessSubFilter] to a color.
@override
RGBA applyFilter(RGBA color) =>
color_filter_utils.brightness(color, brightness);
}
```


## Getting Started

For help getting started with Flutter, view our online [documentation](https://flutter.io/).
Expand Down
File renamed without changes
10 changes: 9 additions & 1 deletion lib/filters/color_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class ColorSubFilter extends SubFilter {
}

///The [ColorFilter] class to define a Filter which will applied to each color, consists of multiple [SubFilter]s
abstract class ColorFilter extends Filter {
class ColorFilter extends Filter {
List<ColorSubFilter> subFilters;
ColorFilter({String name})
: subFilters = [],
Expand All @@ -33,4 +33,12 @@ abstract class ColorFilter extends Filter {
bytes[i + 3] = color.alpha;
}
}

void addSubFilter(ColorSubFilter subFilter) {
this.subFilters.add(subFilter);
}

void addSubFilters(List<ColorSubFilter> subFilters) {
this.subFilters.addAll(subFilters);
}
}
26 changes: 13 additions & 13 deletions lib/filters/convolution_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import 'package:photofilters/utils/convolution_kernels.dart';

var presetConvolutionFiltersList = [
new ImageFilter(name: "Identity")
..subFilters.add(ConvolutionSubFilter.fromKernel(identityKernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(identityKernel)),
new ImageFilter(name: "Sharpen")
..subFilters.add(new ConvolutionSubFilter.fromKernel(sharpenKernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(sharpenKernel)),
new ImageFilter(name: "Emboss")
..subFilters.add(ConvolutionSubFilter.fromKernel(embossKernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(embossKernel)),
new ImageFilter(name: "Colored Edge Detection")
..subFilters
.add(ConvolutionSubFilter.fromKernel(coloredEdgeDetectionKernel)),
new ImageFilter(name: "Edge Detection Medium")
..subFilters
.add(ConvolutionSubFilter.fromKernel(edgeDetectionMediumKernel)),
new ImageFilter(name: "Edge Detection Hard")
..subFilters.add(ConvolutionSubFilter.fromKernel(edgeDetectionHardKernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(edgeDetectionHardKernel)),
new ImageFilter(name: "Blur")
..subFilters.add(ConvolutionSubFilter.fromKernel(blurKernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(blurKernel)),
new ImageFilter(name: "Guassian 3x3")
..subFilters.add(ConvolutionSubFilter.fromKernel(Guassian3x3Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(Guassian3x3Kernel)),
new ImageFilter(name: "Guassian 5x5")
..subFilters.add(ConvolutionSubFilter.fromKernel(Guassian5x5Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(Guassian5x5Kernel)),
new ImageFilter(name: "Guassian 7x7")
..subFilters.add(ConvolutionSubFilter.fromKernel(Guassian7x7Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(Guassian7x7Kernel)),
new ImageFilter(name: "Mean 3x3")
..subFilters.add(ConvolutionSubFilter.fromKernel(mean3x3Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(mean3x3Kernel)),
new ImageFilter(name: "Mean 5x5")
..subFilters.add(ConvolutionSubFilter.fromKernel(mean5x5Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(mean5x5Kernel)),
new ImageFilter(name: "Low Pass 3x3")
..subFilters.add(ConvolutionSubFilter.fromKernel(lowPass3x3Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(lowPass3x3Kernel)),
new ImageFilter(name: "Low Pass 5x5")
..subFilters.add(ConvolutionSubFilter.fromKernel(lowPass5x5Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(lowPass5x5Kernel)),
new ImageFilter(name: "High Pass 3x3")
..subFilters.add(ConvolutionSubFilter.fromKernel(highPass3x3Kernel)),
..addSubFilter(ConvolutionSubFilter.fromKernel(highPass3x3Kernel)),
];
8 changes: 8 additions & 0 deletions lib/filters/image_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class ImageFilter extends Filter {
subFilter.apply(pixels, width, height);
}
}

void addSubFilter(ImageSubFilter subFilter) {
this.subFilters.add(subFilter);
}

void addSubFilters(List<ImageSubFilter> subFilters) {
this.subFilters.addAll(subFilters);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: photofilters
description: A flutter package for applying various types of filters to an image. You can create your own filters and subfilters too.
version: 1.0.6
version: 2.0.0
homepage: https://github.com/skkallayath/photofilters

environment:
Expand Down
29 changes: 0 additions & 29 deletions test/custom_convolution_test.dart

This file was deleted.

40 changes: 40 additions & 0 deletions test/custom_filters_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:photofilters/filters/color_filters.dart';
import 'package:photofilters/filters/image_filters.dart';
import 'package:photofilters/filters/subfilters.dart';
import 'package:photofilters/utils/convolution_kernels.dart';

import 'test_utils.dart';

void main() {
test("Custom Image filter", () {
var customFilter = new ImageFilter(name: "Custom Image Filter");
customFilter.addSubFilter(ConvolutionSubFilter.fromKernel(
coloredEdgeDetectionKernel,
));
customFilter.addSubFilters([
ConvolutionSubFilter.fromKernel(
Guassian7x7Kernel,
),
ConvolutionSubFilter.fromKernel(
sharpenKernel,
),
ConvolutionSubFilter.fromKernel(
highPass3x3Kernel,
)
]);
customFilter.addSubFilter(ConvolutionSubFilter.fromKernel(
lowPass3x3Kernel,
));
customFilter.addSubFilter(SaturationSubFilter(0.5));
applyFilterOnFile(customFilter, "res/bird.jpg", "out/custom_image.jpg");
});

test("Custom Color filter", () {
var customFilter = new ColorFilter(name: "Custom Color Filter");
customFilter.addSubFilter(SaturationSubFilter(0.5));
customFilter
.addSubFilters([BrightnessSubFilter(0.5), HueRotationSubFilter(30)]);
applyFilterOnFile(customFilter, "res/bird.jpg", "out/custom_color.jpg");
});
}

0 comments on commit 4a4f55d

Please sign in to comment.